server.cpp
#define WIN32_LEAN_AND_MEAN
#include
#include
#include
#include
#include
#pragma comment(lib,"ws2_32.lib")
using namespace std;
struct DataProgram
{
int age;
char name[36];
};
enum CMD
{
CMD_LOGIN = 1,
CMD_LOGIN_RESULT,
CMD_LOGOUT,
CMD_LOGOUT_RESULT,
CMD_NEW_CLIENT,
CMD_ERROR
};
struct DataHeader
{
short cmd;
short dataLength;
};
struct Login : public DataHeader
{
Login()
{
cmd = CMD_LOGIN;
dataLength = sizeof(Login);
}
char username[32];
char passwd[32];
};
struct LoginResult : public DataHeader
{
LoginResult()
{
cmd = CMD_LOGIN_RESULT;
dataLength = sizeof(LoginResult);
}
short result;
};
struct Logout : public DataHeader
{
Logout()
{
cmd = CMD_LOGOUT;
dataLength = sizeof(Logout);
}
char username[32];
};
struct LogoutResult:public DataHeader
{
LogoutResult()
{
cmd = CMD_LOGOUT_RESULT;
dataLength = sizeof(LogoutResult);
}
short result;
};
struct NewClientLogin:public DataHeader
{
NewClientLogin()
{
cmd = CMD_NEW_CLIENT;
dataLength = sizeof(NewClientLogin);
}
short result;
};
vector
int parseData(SOCKET _csock)
{
char szRecv[1024] = {}; //缓冲区
int len = recv(_csock, szRecv, sizeof(DataHeader), 0);
DataHeader * header = (DataHeader*)szRecv;
if (len > 0)
{
printf("cmd=%d datalen=%d\n", header->cmd, header->dataLength);
switch (header->cmd)
{
case CMD_LOGIN:
{
recv(_csock, szRecv + sizeof(DataHeader), header->dataLength - sizeof(DataHeader), 0);
Login *login = (Login *)szRecv;
printf("login
LoginResult loginresult = {};
loginresult.result = 1;
send(_csock, (const char *)&loginresult, sizeof(LoginResult), 0);
return 0;
}
case CMD_LOGOUT:
{
recv(_csock, szRecv + sizeof(DataHeader), header->dataLength - sizeof(DataHeader), 0);
Logout *logout = (Logout *)szRecv;
printf("logout
LogoutResult logoutresult = {};
logoutresult.result = 1;
send(_csock, (const char *)&logoutresult, sizeof(LogoutResult), 0);
return 0;
}
default:
{
header->cmd = CMD_ERROR;
header->dataLength = 0;
printf("cmd error\n");
send(_csock, (const char *)&header, sizeof(header), 0);
return 0;
}
}
}
else
{
printf("logout
closesocket(_csock);
return -1;
}
return 0;
}
int main()
{
WORD ver = MAKEWORD(2, 2);
WSADATA dat;
WSAStartup(ver,&dat);
//SOCK_DGRAM IPPROTO_UDP
SOCKET _sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (_sock == INVALID_SOCKET)
{
printf("socket create failed\n");
}
else
{
printf("socket create sucess\n");
sockaddr_in _sin = {};
_sin.sin_family = AF_INET;
_sin.sin_addr.S_un.S_addr = INADDR_ANY;//inet_addr("127.0.0.1")
//_sin.sin_addr.s_addr = htonl(INADDR_ANY);
_sin.sin_port = htons(4567);
int bindresult = bind(_sock, (sockaddr *)&_sin, sizeof(_sin));
if (bindresult == SOCKET_ERROR)
{
printf("socket bind Failed\n");
}
else
{
printf("socket bind sucess\n");
int listenresult = listen(_sock, 5);
if (listenresult == SOCKET_ERROR)
{
printf("socket listen failed\n");
}
else
{
printf("socket listen sucess\n");
while (true)
{
fd_set fdRead;
fd_set fdWrite;
fd_set fdExcept;
FD_ZERO(&fdRead);
FD_ZERO(&fdWrite);
FD_ZERO(&fdExcept);
FD_SET(_sock, &fdRead);
FD_SET(_sock, &fdWrite);
FD_SET(_sock, &fdExcept);
for (size_t i = 0;i < g_clients.size();i++)
{
FD_SET(g_clients[i], &fdRead);
}
//nfds是一个整数值,是指fd_set中所有文件描述符(_socket)的范围而不是数量
//即是所有文件描述符最大值+1,在windows中这个参数可以写0
timeval t = { 1,0 };
int ret = select(_sock + 1, &fdRead, &fdWrite, &fdExcept, &t);
/*
负值:select错误
正值:某些文件可读写或出错
0:等待超时,没有可读写或错误的文件
*/
if (ret < 0)
{
printf("select任务结束.\n");
break;
}
if (FD_ISSET(_sock,&fdRead))
{
FD_CLR(_sock, &fdRead);
sockaddr_in clientAddr = {};
int nAddrLen = sizeof(clientAddr);
SOCKET _csock = INVALID_SOCKET;
_csock = accept(_sock, (sockaddr *)&clientAddr, &nAddrLen);
if (_csock == INVALID_SOCKET)
{
printf("Error,接受到无效客户端socket....\n");
}
else
{
for (size_t i = 0; i < g_clients.size(); i++)
{
NewClientLogin nlogin;
nlogin.result = 1000;
send(g_clients[i], (const char *)&nlogin, sizeof(NewClientLogin), 0);
}
char str[INET_ADDRSTRLEN];
printf("new client<%lld>:%s-%d-%d\n", _csock, inet_ntop(AF_INET, &clientAddr.sin_addr, str, sizeof(str)),
clientAddr.sin_port,
clientAddr.sin_family);
//inet_ntoa(clientAddr.sin_addr)
g_clients.push_back(_csock);
}
}
for (size_t i = 0; i < fdRead.fd_count; i++)
{
int j = parseData(fdRead.fd_array[i]);
if (j == -1)
{
auto iter = find(g_clients.begin(), g_clients.end(), fdRead.fd_array[i]);
if (iter != g_clients.end())
{
g_clients.erase(iter);
}
}
}
printf("hello world\n");
}
}
}
}
closesocket(_sock);
WSACleanup();
getchar();
return 0;
}
client.cpp
#define WIN32_LEAN_AND_MEAN
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include
#include
#include
#include
#include
#pragma comment(lib,"ws2_32.lib")
struct DataProgram
{
int age;
char name[36];
};
enum CMD
{
CMD_LOGIN = 1,
CMD_LOGIN_RESULT,
CMD_LOGOUT,
CMD_LOGOUT_RESULT,
CMD_NEW_CLIENT,
CMD_ERROR
};
struct DataHeader
{
short cmd;
short dataLength;
};
struct Login : public DataHeader
{
Login()
{
cmd = CMD_LOGIN;
dataLength = sizeof(Login);
}
char username[32];
char passwd[32];
};
struct LoginResult : public DataHeader
{
LoginResult()
{
cmd = CMD_LOGIN_RESULT;
dataLength = sizeof(LoginResult);
}
short result;
};
struct Logout : public DataHeader
{
Logout()
{
cmd = CMD_LOGOUT;
dataLength = sizeof(Logout);
}
char username[32];
};
struct LogoutResult :public DataHeader
{
LogoutResult()
{
cmd = CMD_LOGOUT_RESULT;
dataLength = sizeof(LogoutResult);
}
short result;
};
struct NewClientLogin :public DataHeader
{
NewClientLogin()
{
cmd = CMD_NEW_CLIENT;
dataLength = sizeof(NewClientLogin);
}
short result;
};
int parseData(SOCKET _csock)
{
char szRecv[1024] = {}; //缓冲区
int len = recv(_csock, szRecv, sizeof(DataHeader), 0);
DataHeader * header = (DataHeader*)szRecv;
if (len > 0)
{
printf("cmd=%d datalen=%d\n", header->cmd, header->dataLength);
switch (header->cmd)
{
case CMD_LOGIN_RESULT:
{
recv(_csock, szRecv + sizeof(DataHeader), header->dataLength - sizeof(DataHeader), 0);
LoginResult *loginresult = (LoginResult *)szRecv;
printf("cmd=CMD_LOGIN_RESULT socketid=%lld ret=%d\n", _csock, loginresult->result);
return 0;
}
case CMD_LOGOUT_RESULT:
{
recv(_csock, szRecv + sizeof(DataHeader), header->dataLength - sizeof(DataHeader), 0);
LogoutResult *loginresult = (LogoutResult *)szRecv;
printf("cmd=CMD_LOGOUT_RESULT socketid=%lld ret=%d\n", _csock, loginresult->result);
return 0;
}
case CMD_NEW_CLIENT:
{
recv(_csock, szRecv + sizeof(DataHeader), header->dataLength - sizeof(DataHeader), 0);
NewClientLogin * newclient = (NewClientLogin *)szRecv;
printf("cmd=CMD_LOGIN_RESULT socketid=%lld ret=%d\n", _csock, newclient->result);
return 0;
}
}
}
else
{
printf("与服务器断开连接
closesocket(_csock);
return -1;
}
return 0;
}
int main()
{
WORD ver = MAKEWORD(2, 2);
WSADATA dat;
WSAStartup(ver, &dat);
SOCKET _sock = socket(AF_INET, SOCK_STREAM, 0);
if (_sock == INVALID_SOCKET)
{
printf("socket create Failed");
}
else
{
sockaddr_in _sin = {};
_sin.sin_family = AF_INET;
_sin.sin_port = htons(4567);
_sin.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
int conresult = connect(_sock, (sockaddr*)&_sin, sizeof(_sin));
if (conresult == SOCKET_ERROR)
{
printf("socket connect Failed");
}
else
{
while (true)
{
fd_set fdRead;
FD_ZERO(&fdRead);
FD_SET(_sock, &fdRead);
timeval t = { 1,0 };
int ret = select(_sock + 1, &fdRead, 0, 0, &t);
if (ret < 0)
{
printf("select任务结束1.\n");
break;
}
if (FD_ISSET(_sock,&fdRead))
{
FD_CLR(_sock, &fdRead);
if ( parseData(_sock) == -1)
{
printf("select任务结束2.\n");
break;
}
}
printf("空闲时间处理其他任务\n");
Login login;
strcpy_s(login.username, 32, "sandy");
strcpy_s(login.passwd, 32, "123456");
send(_sock, (const char *)&login,sizeof(Login), 0);
}
}
}
closesocket(_sock);
WSACleanup();
getchar();
return 0;
}
非常不错的服务器编程教程
https://study.163.com/course/introduction/1006470001.htm?share=1&shareId=1023405782&utm_campaign=share&utm_medium=iphoneShare&utm_source=weixin&utm_u=1023405782