#define WIN32_LEAN_AND_MEAN #define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <iostream> #include <WinSock2.h> #include <Windows.h>
#pragma comment(lib,"ws2_32.lib")
#include <vector>
#pragma region 结构体定义
enum CMD { CMD_LOGIN, CMD_LOGOUT, CMD_ERROR, CMD_LOGIN_RESULT, CMD_LOGOUT_RESULT, CMD_NEW_USER_JOIN };
struct DataHeader { short dataLength; short cmd; };
struct Login : public DataHeader { Login() { dataLength = sizeof(Login); cmd = CMD_LOGIN; } char userName[32]; char PassWord[32]; };
struct LoginResult : public DataHeader { LoginResult() { dataLength = sizeof(LoginResult); cmd = CMD_LOGIN_RESULT; result = 0; }
int result; };
struct Logout : public DataHeader { Logout() { dataLength = sizeof(Logout); cmd = CMD_LOGOUT; }
char userName[32]; };
struct LogoutResult : public DataHeader { LogoutResult() { dataLength = sizeof(LogoutResult); cmd = CMD_LOGOUT_RESULT; result = 0; }
int result; };
struct NewUserJoin : public DataHeader { NewUserJoin() { dataLength = sizeof(NewUserJoin); cmd = CMD_NEW_USER_JOIN; sock = 0; }
int sock; };
struct DataPackage { int age; char name[32]; };
#pragma endregion
using namespace std;
vector<SOCKET> g_clients;
int processor(SOCKET _cSock) { char szRecv[4096] = {}; int nLen = recv(_cSock, szRecv, sizeof(DataHeader), 0); DataHeader* header = (DataHeader*)szRecv; if (nLen <= 0) { cout << "客户端< socket = " << _cSock << " >已退出,任务结束..." << endl; return -1; } switch (header->cmd) { case CMD_LOGIN: { recv(_cSock, szRecv + sizeof(DataHeader), header->dataLength - sizeof(DataHeader), 0); Login* login = (Login*)szRecv; cout << "收到客户端<Socket = " << _cSock << ">请求:CMD_LOGIN, 数据长度:" << login->dataLength << " ,userName = " << login->userName << " PassWord = " << login->PassWord << endl; LoginResult ret; send(_cSock, (char*)&ret, sizeof(LoginResult), 0); break; } case CMD_LOGOUT: { recv(_cSock, szRecv + sizeof(DataHeader), header->dataLength - sizeof(DataHeader), 0); Logout* logout = (Logout*)szRecv; cout << "收到客户端<Socket = " << _cSock << ">请求:CMD_LOGOUT, 数据长度:" << logout->dataLength << " ,userName = " << logout->userName << endl; LogoutResult ret; send(_cSock, (char*)&ret, sizeof(ret), 0); break; } default: { DataHeader header = { 0,CMD_ERROR }; send(_cSock, (char*)&header, sizeof(header), 0); } } }
int main() { WORD ver = MAKEWORD(2, 2); WSADATA dat; WSAStartup(ver, &dat); SOCKET _sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 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) { cout << "绑定网络端口失败..." << endl; } else { cout << "绑定网络端口成功..." << endl; } if (SOCKET_ERROR == listen(_sock, 5)) { cout << "监听网络端口失败..." << endl; } else { cout << "监听网络端口成功..." << endl; } while (true) { fd_set fdRead; fd_set fdWrite; fd_set fdExp; FD_ZERO(&fdRead); FD_ZERO(&fdWrite); FD_ZERO(&fdExp); FD_SET(_sock, &fdRead); FD_SET(_sock, &fdWrite); FD_SET(_sock, &fdExp); for (int n = (int)g_clients.size() - 1; n >= 0; n--) { FD_SET(g_clients[n], &fdRead); } timeval t = {0,0}; int ret = select(_sock + 1, &fdRead, &fdWrite, &fdExp, &t); if (ret < 0) { cout << "select任务结束..." << endl; break; } if (FD_ISSET(_sock, &fdRead)) { FD_CLR(_sock, &fdRead); sockaddr_in clientAddr = {}; int nAddrLen = sizeof(sockaddr_in); SOCKET _cSock = INVALID_SOCKET; _cSock = accept(_sock, (sockaddr*)&clientAddr, &nAddrLen); if (_cSock == INVALID_SOCKET) { cout << "接受到无效客户端SOCKET..." << endl; } else { for (int n = (int)g_clients.size() - 1; n >= 0; n--) { NewUserJoin userJoin; send(g_clients[n], (const char*)&userJoin, sizeof(NewUserJoin), 0); } g_clients.push_back(_cSock); cout << "新客户端加入:socket = " << (int)_cSock << ", IP = " << inet_ntoa(clientAddr.sin_addr) << endl; } } for (size_t n = 0; n < fdRead.fd_count; n++) { if (processor(fdRead.fd_array[n]) == -1) { auto iter = find(g_clients.begin(), g_clients.end(), fdRead.fd_array[n]); if (iter != g_clients.end()) { g_clients.erase(iter); } } } cout << "空闲时间处理其他业务 ..." << endl; } for (size_t n = g_clients.size() - 1; n >= 0; n--) { closesocket(g_clients[n]); } WSACleanup(); cout << "服务器已退出,任务结束..." << endl; getchar(); return 0; }
|