//服务器的代码文件有
/*
message.h
source.h
source.cpp
server.h
server.cpp
*/
//客户端的代码文件有
/*
message.h 和服务器端一样
client.h
client.cpp
*/
//message.h
#pragma once
#include
using namespace std;
#define MAX_PACK_SIZE 10240 //数据包的长度
#define MAX_FILE_NAME_LENGTH 256 //文件名的长度
#define INVALID_MSG -1 //无效的消息
#define MSG_FILE_LENGTH 1 //文件长度
#define MSG_FILE_NAME 2 //文件名
#define MSG_FILE 4 //文件内容
#define MSG_READY 3 //准备好消息
#define MSG_SEND_FILE 5 //发送文件
#define MSG_DOWNLOAD_FILE 6 //
下载文件
#define MSG_COMPLETE 7 //完成信息
class Message
{
public:
struct MsgHead //头消息
{
int msgId; //消息标识
MsgHead(int msg=INVALID_MSG):msgId(msg){};
};
struct MsgFileLength :public MsgHead
{
_int64 fileLength; //文件长度
MsgFileLength():MsgHead(MSG_FILE_LENGTH){}
};
struct MsgFileName:public MsgHead
{
char fileName[MAX_FILE_NAME_LENGTH];
MsgFileName():MsgHead(MSG_FILE_NAME){}
};
struct MsgFile:public MsgHead
{
MsgFile():MsgHead(MSG_FILE){}
};
struct MsgReady:public MsgHead //准备好消息
{
MsgReady():MsgHead(MSG_READY){}
};
struct MsgSendFile:public MsgHead //发送文件消息
{
MsgSendFile():MsgHead(MSG_SEND_FILE){}
};
struct MsgDownLoadFile:public MsgHead //下载文件消息
{
MsgDownLoadFile():MsgHead(MSG_DOWNLOAD_FILE){}
};
struct MsgComplete:public MsgHead
{
MsgComplete():MsgHead(MSG_COMPLETE){}
};
};
//source.h 获取指定文件加下的符合要求的文件
#pragma once
#include
#include
#include
#include
#include
using namespace std; class Source { public: vector
catalogInfo; void GetFiles(string path,string ext,vector
&files);//获取文件 }; //server.h #pragma once #include
#include
#include"message.h" #pragma comment(lib,"Ws2_32.lib") #define PORT 10000 using namespace std; class Server { public: SOCKET sd; _int64 fileLength; char fileName[MAX_FILE_NAME_LENGTH]; bool InitSock(); //初始winsocket SOCKET BindListen(); //绑定监听套接字 SOCKET AcceptConnection(SOCKET sd); //接收客户端 bool ProcessConnection(SOCKET sd); //传送数据 bool ReceiveFile(SOCKET sd); //接收文件内容 bool RecvFileName(SOCKET sd); //接收文件名 bool GetAndSendFileLength(SOCKET sd); //获取文件长度 bool SendFileName(SOCKET sd); //发送文件名 bool SendFile(SOCKET sd); //发送文件 void CloseSocket(); //关闭套接字 }; //source.cpp #pragma once #include
#include
#include
#include
#include"source.h" using namespace std; void Source::GetFiles(string path,string ext,vector
&files) { long hFile=0; //文件句柄 _finddata_t fileInfo; //文件信息 string pathName; if((hFile=_findfirst(pathName.assign(path).append("\\*").c_str(),&fileInfo))!=-1) //判断路径是否有效并获取第一个文件 { do { if(fileInfo.attrib & _A_SUBDIR) //如果是子文件夹 { if(strcmp(fileInfo.name,".")!=0 && strcmp(fileInfo.name,"..")!=0) { GetFiles(pathName.assign(path).append("\\").append(fileInfo.name),ext,files); } } else { string filePath; filePath=pathName.assign(path).append("\\").append(fileInfo.name); char fileDrive[_MAX_DRIVE]; char fileDir[_MAX_DIR]; char fileName[_MAX_FNAME]; char fileExt[_MAX_EXT]; _splitpath(filePath.c_str(),fileDrive,fileDir,fileName,fileExt); //分解路径获取磁盘区路径文件名后缀 if(strcmp(fileExt,ext.c_str())==0) { files.push_back(filePath); } } }while(_findnext(hFile,&fileInfo)==0); _findclose(hFile); } } //server.cpp #pragma once #include
#include
#include
#include
#include"message.h" #include"server.h" #include"s