C++服务器与java进行socket通信案例
(一)项目概述:
C++服务器是之前编写好的,有对应的C++客户端。因此,服务器的C++代码不能改变。主要解决的问题是:
1.如何用java类来还原服务器中的结构体
2.数据之间的转换
3.socket通信数据统一用字节数组。
(二)首先看一下服务器的代码:(360安全卫士竟然说我的代码是木马...好吧,这个服务器不就是能联网,能获取本地数据么?确实有点像木马,但绝对不是!)
/*主要功能是获得系统硬盘符号及硬盘下的目录和文件*/
[C++ code]
[cpp]
// FileNetServer.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "FileNetServer.h"
#include <winsock.h>
#include <windows.h>
#include "SYS.H"
#pragma comment(lib, "ws2_32")
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
typedef struct
{
int ID;
BYTE lparam[BUF_LEN*2];
}COMMAND;
/*BOOL 4byte HICON 4byte*/
typedef struct
{
char FileName[MAX_PATH];//260byte
int FileLen;
char Time[50];
BOOL IsDir;
BOOL Error;
HICON hIcon;
}FILEINFO;
BOOL DeleteDirectory(char *DirName);
BOOL CapScreen(LPTSTR FileName);
DWORD WINAPI SLisen(LPVOID lparam);
DWORD GetDriverProc(COMMAND command, SOCKET client);
DWORD GetDirInfoProc(COMMAND command, SOCKET client);
DWORD ExecFileProc(COMMAND command, SOCKET client);
DWORD DelFileProc(COMMAND command, SOCKET client);
DWORD FileInfoProc(COMMAND command, SOCKET client);
DWORD CreateDirProc(COMMAND command, SOCKET client);
DWORD DelDirProc(COMMAND command, SOCKET client);
DWORD GetFileProc(COMMAND command, SOCKET client);
DWORD PutFileProc(COMMAND command, SOCKET client);
DWORD GetScreenProc(COMMAND command, SOCKET client);
/////////////////////////////////////////////////////////////////////////////
// The one and only application object
CWinApp theApp;
using namespace std;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
WSADATA wsadata;
SOCKET FileNetServer;
SOCKET client;
SOCKADDR_IN FileNetServeraddr;
SOCKADDR_IN clientaddr;
int port = 17329;
WORD ver = MAKEWORD(2,2);
WSAStartup(ver, &wsadata);
FileNetServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
FileNetServeraddr.sin_family = AF_INET;
FileNetServeraddr.sin_port = htons(port);
FileNetServeraddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
bind(FileNetServer, (SOCKADDR*)&FileNetServeraddr, sizeof(FileNetServeraddr));
listen(FileNetServer, 5);
int len = sizeof(clientaddr);
char s_password[100];
while(true)
{
if(client = accept(FileNetServer, (sockaddr*)&clientaddr, &len))
{
if(send(client, "Password", sizeof("Password"), 0) != SOCKET_ERROR)
{
cout<<"有客户请求连接,正在等待客户输入密码!";
}
ag: if(recv(client, s_password, sizeof(s_password), 0) != SOCKET_ERROR)
{
if(strcmp(s_password, "123") == 0)
{
send(client, "OK", sizeof("OK"), 0);
cout<<"CreateThread is the OK \n";
CreateThread(NULL, NULL, SLisen, (LPVOID)client, NULL, NULL);
}
else
{
cout<<"有用户试图联入,但是未能输入正确密码!/n";
send(client, "NOOK", sizeof("NOOK"), 0);
goto ag;
}
}
}
}
closesocket(FileNetServer);
closesocket(client);
WSACleanup();
return 0;
}
DWORD WINAPI SLisen(LPVOID lparam)
{
SOCKET client = (SOCKET)lparam;
COMMAND command;
while(1)
{
memset((char*)&command, 0, sizeof(command));
if(recv(client, (char*)&command, sizeof(command), 0) == SOCKET_ERROR)
{
cout<<"The Client Socket is Closed\n";
break;
}
else
{ 
补充:软件开发 , C++ ,