一、新建空的控制台项目,添加sever.cpp,代码如下
#define WIN32_LEAN_AND_MEAN
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#include <Windows.h>
#include <WinSock2.h>
#include <stdio.h>
#pragma comment(lib,"ws2_32.lib")
#include <vector>
enum CMD
{
CMD_NEW_USER_JOIN,
CMD_LOGIN,
CMD_LOGIN_RST,
CMD_LOGOUT,
CMD_LOGOUT_RST,
};
struct DataHeader
{
short dataLth;
CMD cmd;
};
struct Login : public DataHeader
{
Login()
{
dataLth = sizeof(Login);
cmd = CMD_LOGIN;
}
char usrName[32];
char passWord[32];
};
struct LoginRst : public DataHeader
{
LoginRst()
{
dataLth = sizeof(LoginRst);
cmd = CMD_LOGIN_RST;
}
int rst;
};
struct Logout : public DataHeader
{
Logout()
{
dataLth = sizeof(Logout);
cmd = CMD_LOGOUT;
}
char usrName[32];
};
struct LogoutRst : public DataHeader
{
LogoutRst()
{
dataLth = sizeof(LogoutRst);
cmd = CMD_LOGOUT_RST;
}
int rst;
};
struct NewUserJoin : public DataHeader
{
NewUserJoin()
{
dataLth = sizeof(NewUserJoin);
cmd = CMD_NEW_USER_JOIN;
}
int sock;
};
std::vector<SOCKET> g_clients;
int processor(SOCKET _cSock)
{
char szRecv[4096] = {};
printf("当前<服务端>套接字%d,processor start.... \n",(int)_cSock);
int nLen = recv(_cSock, szRecv, sizeof(DataHeader), 0);
DataHeader* header = (DataHeader*)szRecv;
if (nLen <= 0)
{
printf("当前<服务端>套接字%d,processor end.... ,客户端退出!\n", (int)_cSock);
return -1;
}
//处理请求
switch (header->cmd)
{
case CMD_LOGIN:
{
printf("命令 CMD_LOGIN,客户端!!登录!\n");
recv(_cSock, szRecv + sizeof(DataHeader), header->dataLth - sizeof(DataHeader), 0);
Login *login = (Login*)szRecv;
printf("用户姓名:%s 用户密码:%s\n", login->usrName, login->passWord);
LoginRst inRst;
inRst.rst = 1000;
send(_cSock, (char*)&inRst, sizeof(inRst), 0);
}
break;
case CMD_LOGOUT:
{
printf("命令 CMD_LOGOUT,客户端登出!!!\n");
recv(_cSock, szRecv + sizeof(DataHeader), header->dataLth- sizeof(DataHeader), 0);
Logout *logout = (Logout*)szRecv;
printf("用户姓名:%s", logout->usrName);
LogoutRst outRst;
outRst.rst = -1000;
send(_cSock, (char*)&outRst, sizeof(outRst), 0);
}
break;
}
printf("当前<服务端>套接字%d,processor end.... \n", (int)_cSock);
return 0;
}
int main()
{
WORD ver = MAKEWORD(2, 2);
WSADATA dat;
WSAStartup(ver, &dat);
//1建立套接字
SOCKET _sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
//2绑定用于接受客户端链接的网络端口
sockaddr_in _sin = {};
_sin.sin_family = AF_INET;
_sin.sin_port = htons(4567);//主机转网络字节地址
_sin.sin_addr.S_un.S_addr = INADDR_ANY; //接收
if (bind(_sock, (sockaddr*)&_sin, sizeof(_sin)) == SOCKET_ERROR)
{
printf("绑定网络端口失败!\n");
}
else {
printf("绑定网络端口成功!\n");
}
//3 监听端口
if (SOCKET_ERROR == listen(_sock, 5))
{
printf("监听失败!\n");
}
else{
printf("监听成功!\n");
}
while (true)
{
///printf("the Start\n");
fd_set fdsRead;
fd_set fdsWrite;
fd_set fdsExp;
FD_ZERO(&fdsRead);
FD_ZERO(&fdsWrite);
FD_ZERO(&fdsExp);
FD_SET(_sock, &fdsRead);
FD_SET(_sock, &fdsWrite);
FD_SET(_sock, &fdsExp);//创建3种的fds用来检测是否有链接
//如果g_clients中含有套接字的话,将他添加进来
for (int n = 0; n < (int)g_clients.size(); n++)
{
FD_SET(g_clients[n], &fdsRead);
}
timeval t = { 10,0 };
int ret = select(_sock + 1, &fdsRead, &fdsWrite, &fdsExp, &t);
if (ret < 0)
{
printf("select模型中没用可用套接字,客户端已退出!!!\n");
break;
}
if (FD_ISSET(_sock, &fdsRead))
{
FD_CLR(_sock, &fdsRead);
//等待socket
sockaddr_in _clientAddr = {};
int nAddrLen = sizeof(sockaddr_in);
SOCKET _cSock = INVALID_SOCKET;
_cSock = accept(_sock, (sockaddr*)&_clientAddr, &nAddrLen);
if (_cSock == INVALID_SOCKET)
{
printf("当前是无效客户端套接字!!!\n");
}
for (int n = (int)g_clients.size()-1; n >= 0; n--)
{
NewUserJoin userjoin;
userjoin.sock = (int)g_clients[n];
printf("<服务端向客户端发送newuserjoin = %d>\n",userjoin.sock);
send(g_clients[n], (char*)&userjoin, sizeof(userjoin), 0);
}
g_clients.push_back(_cSock);
printf("新的客户端:socket = %d,IP = %s \n", int(_cSock), inet_ntoa(_clientAddr.sin_addr));
}
for (int n = 0; n <=(int)(fdsRead.fd_count - 1); n++)
{
if (processor(fdsRead.fd_array[n]) == -1)
{
auto iter = find(g_clients.begin(), g_clients.end(), fdsRead.fd_array[n]);
if (iter != g_clients.end())
{
g_clients.erase(iter);
}
}
}//删除退出的套接字
//printf("空闲时间处理其他业务!!!\n");
}
//6 关闭套接字
for (int n = (int)g_clients.size();n >= 0; n--)
{
closesocket(g_clients[n]);
}
closesocket(_sock);
WSACleanup();
getchar();
return 0;
}
二、新建空的控制台项目,新建client.cpp,输入代码
#define WIN32_LEAN_AND_MEAN
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#include <Windows.h>
#include <WinSock2.h>
#include <stdio.h>
#pragma comment(lib,"ws2_32.lib")
enum CMD
{
CMD_NEW_USER_JOIN,
CMD_LOGIN,
CMD_LOGIN_RST,
CMD_LOGOUT,
CMD_LOGOUT_RST,
};
struct DataHeader
{
short dataLth;
CMD cmd;
};
struct Login : public DataHeader
{
Login()
{
dataLth = sizeof(Login);
cmd = CMD_LOGIN;
}
char usrName[32];
char passWord[32];
};
struct LoginRst : public DataHeader
{
LoginRst()
{
dataLth = sizeof(LoginRst);
cmd = CMD_LOGIN_RST;
}
int rst;
};
struct Logout : public DataHeader
{
Logout()
{
dataLth = sizeof(Logout);
cmd = CMD_LOGOUT;
}
char usrName[32];
};
struct LogoutRst : public DataHeader
{
LogoutRst()
{
dataLth = sizeof(LogoutRst);
cmd = CMD_LOGOUT_RST;
}
int rst;
};
struct NewUserJoin : public DataHeader
{
NewUserJoin()
{
dataLth = sizeof(NewUserJoin);
cmd = CMD_NEW_USER_JOIN;
}
int sock;
};
int processor(SOCKET _cSock)
{
printf("当前<客户端>套接字%d,processor start.... \n", (int)_cSock);
char szRecv[4096] = {};
// 5 接收客户端数据
int nLen = (int)recv(_cSock, szRecv, sizeof(DataHeader), 0);
DataHeader* header = (DataHeader*)szRecv;
if (nLen <= 0)
{
printf("客户端退出!\n 当前<客户端>套接字%d,processor end....\n ", (int)_cSock);
return -1;
}
//处理请求
switch (header->cmd)
{
case CMD_LOGIN_RST:
{
printf("命令 CMD_LOGIN_RST,服务器发送登入消息!\n");
recv(_cSock, szRecv + sizeof(DataHeader), header->dataLth - sizeof(DataHeader), 0);
LoginRst* loginrst = (LoginRst*)szRecv;
printf("服务器发送来的登入消息:%d\n", loginrst->rst);
}
break;
case CMD_LOGOUT_RST:
{
printf("命令 CMD_LOGOUT_RST,服务端发来登出消息!\n");
recv(_cSock, szRecv + sizeof(DataHeader), sizeof(Logout) - sizeof(DataHeader), 0);
LogoutRst *logoutrst =(LogoutRst*)szRecv;
printf("服务器发送来的登出消息是:%d\n", logoutrst->rst);
}
break;
case CMD_NEW_USER_JOIN:
{
printf("命令 CMD_NEW_USER_JION,服务器进行群发消息-新用户进入\n");
recv(_cSock, szRecv + sizeof(DataHeader),sizeof(NewUserJoin)-sizeof(DataHeader),0);
NewUserJoin *newuserjoin = (NewUserJoin*)szRecv;
printf("新用户的id是:%d", newuserjoin->sock);
}
break;
}
printf("当前<客户端>套接字%d,processor end.... \n", (int)_cSock);
return 0;
}
int main()
{
WORD ver = MAKEWORD(2, 2);
WSADATA dat;
WSAStartup(ver, &dat);
//1 建立套接字
SOCKET _sock = socket(AF_INET, SOCK_STREAM, 0);
if (INVALID_SOCKET == _sock)
{
printf("客户端创建套接字失败!!!\n");
}
else
{
printf("客户端创建套接字成功,套接字:%d!!!\n",(int)_sock);
}
//2 链接服务器
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 ret = connect(_sock,(sockaddr*)&_sin,sizeof(sockaddr_in));
if (SOCKET_ERROR == ret)
{
printf("服务器连接失败!!!\n");
}
else
{
printf("服务器连接成功!!!\n");
}
while (true)
{
fd_set fdReads;
FD_ZERO(&fdReads);
FD_SET(_sock, &fdReads);
timeval t = { 5 ,0 };
int ret = select(_sock,&fdReads,0,0,&t);
if (ret < 0)
{
printf("select任务结束");
break;
}
if (FD_ISSET(_sock, &fdReads))
{
FD_CLR(_sock, &fdReads);
if (-1 == processor(_sock))
{
printf("结束任务!\n");
}
}
Sleep(10000);
Login login;
strcpy(login.usrName, "<kehu-login>");
strcpy(login.passWord, "<kehu-login-password>");
send(_sock, (char*)&login, sizeof(login), 0);
Logout logout;
strcpy(logout.usrName, "<kehu-logout>");
send(_sock, (char*)&logout, sizeof(logout), 0);
}
//7关闭套接字
closesocket(_sock);
WSACleanup();
getchar();
return 0;
}