注:本代码版权所有!!!转载时请声明源地址:http://blog.csdn.net/nuptboyzhb/article/details/8047619
你可以学习,分享,修改,教学等。但是不得用于商业目的。目前已经发现互联网上大量与本文完全相同的文章,但是却把作者信息删除的干干净净,并且据为己有,打上某培训机构的广告!实属可恶!
最新消息:项目成品连接:http://blog.csdn.net/nuptboyzhb/article/details/8611179
(一)项目概述:
C++服务器是之前编写好的,有对应的C++客户端。因此,服务器的C++代码不能改变。主要解决的问题是:
1.如何用java类来还原服务器中的结构体
2.数据之间的转换
3.socket通信数据统一用字节数组。
(二)首先看一下服务器的代码:(360安全卫士竟然说我的代码是木马...好吧,这个服务器不就是能联网,能获取本地数据么?确实有点像木马,但绝对不是!)
/*主要功能是获得系统硬盘符号及硬盘下的目录和文件*/
[C++ code]
- // 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 ";
- 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 ";
- break;
- }
- else
- {
- cout<<"The recv command is: ID="<<command.ID<<" lparam="<<command.lparam<<endl;
- switch(command.ID)
- {
- case GetDriver:
- GetDriverProc(command, client);
- break;
- case GetDirInfo:
- GetDirInfoProc(command, client);
- break;
- case ExecFile:
- ExecFileProc(command, client);
- break;
- case DelFile:
- DelFileProc(command, client);
- break;
- case FileInfo:
- FileInfoProc(command, client);
- break;
- case CreateDir:
- CreateDirProc(command, client);
- break;
- case DelDir:
- DelDirProc(command, client);
- break;
- case GetFile:
- GetFileProc(command, client);
- break;
- case PutFile:
- PutFileProc(command, client);
- break;
- case GetScreen:
- GetScreenProc(command, client);
- break;
- default:
- cout<<"Can not recongnize the command! ";
- break;
- }
- }
- }
- closesocket(client);
- return 0;
- }
- DWORD GetDriverProc(COMMAND command, SOCKET client)
- {
- cout<<"GetDriver is ok ";
- COMMAND cmd;
- memset((char *)&cmd, 0, sizeof(cmd));
- cmd.ID = GetDriver;
- for(char i='A'; i<'Z'; i++)
- {
- char x[20] = {i, ':'};
- UINT Type = GetDriveType(x);
- if(Type==DRIVE_FIXED || Type==DRIVE_REMOVABLE || Type==DRIVE_CDROM)
- {
- cout<<x<<" ";
- memset((char*)cmd.lparam, 0, sizeof(cmd.lparam));
- strcpy((char*)cmd.lparam, x);
- if(send(client, (char*)&cmd, sizeof(cmd), 0) == SOCKET_ERROR)
- {
- cout<<"Send Driver is Error ";
- }
- }
- }
- return 0;
- }
- DWORD GetDirInfoProc(COMMAND command, SOCKET client)
- {
- cout<<"GetDir is Ok ";
- FILEINFO fi;
- COMMAND cmd;
- memset((char*)&cmd, 0, sizeof(cmd));
- memset((char*)&fi, 0, sizeof(fi));
- strcat((char*)command.lparam, "*.*");
- cout<<(char*)command.lparam<<" ";
- CFileFind file;
- BOOL bContinue = file.FindFile((char*)command.lparam);
- while(bContinue)
- {
- memset((char*)&cmd, 0, sizeof(cmd));
- memset((char*)&fi, 0, sizeof(fi));
- bContinue = file.FindNextFile();
- if(file.IsDirectory())
- {
- fi.IsDir = true;
- }
- strcpy(fi.FileName, file.GetFileName().LockBuffer());
- cout<<fi.FileName<<" ";
- cmd.ID = GetDirInfo;
- memcpy((char*)&cmd.lparam, (char*)&fi, sizeof(fi));
- if(send(client, (char*)&cmd, sizeof(cmd), 0) == SOCKET_ERROR)
- {
- cout<<"Send Dir is Error ";
- }
- }
- return 0;
- }
- DWORD ExecFileProc(COMMAND command, SOCKET client)
- {
- return 0;
- }
- DWORD DelFileProc(COMMAND command, SOCKET client)
- {
- return 0;
- }
- DWORD FileInfoProc(COMMAND command, SOCKET client)
- {
- return 0;
- }
- DWORD CreateDirProc(COMMAND command, SOCKET client)
- {
- return 0;
- }
- DWORD DelDirProc(COMMAND command, SOCKET client)
- {
- return 0;
- }
- BOOL DeleteDirectory(char *DirName)
- {
- return 0;
- }
- DWORD GetFileProc(COMMAND command, SOCKET client)
- {
- return 0;
- }
- DWORD PutFileProc(COMMAND command, SOCKET client)
- {
- return 0;
- }
- DWORD GetScreenProc(COMMAND command, SOCKET client)
- {
- return 0;
- }
- BOOL CapScreen(LPTSTR FileName)
- {
- return 0;
- }
(三)最主要的是客户端如何编写:
当然,编程流程和博客[http://blog.csdn.net/nuptboyzhb/article/details/8041715]是一样的。主要有一下几个类:
1.Client.java //还有main函数的类,你们都知道它是功能。
- /*
- *@author: ZhengHaibo
- *web: blog.csdn.net/nuptboyzhb
- *mail: zhb931706659@126.com
- *2012-9-23 Nanjing njupt
- */
- import java.io.IOException;
- import java.net.InetSocketAddress;
- import java.net.Socket;
- public class Client {
- NetDataTypeTransform mNetDataTypeTransform=new NetDataTypeTransform();
- private static final String IP="127.0.0.1";
- private static final int NetPort=17329;
- private Socket sock;
- public Client(){
- try {
- onCreate();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public void onCreate() throws IOException{
- InetSocketAddress addr = new InetSocketAddress(IP,NetPort); //创建socket
- sock = new Socket();
- sock.connect(addr);//连接服务器
- byte []receive=new byte[9];
- sock.getInputStream().read(receive);
- String tempString=mNetDataTypeTransform.ByteArraytoString(receive, receive.length);
- System.out.println(" Server said:send your "+tempString);
- if(tempString.equals("Password")){
- System.out.println("I can send password 123 !");
- }
- String password="123 ";//注意,别忘了‘