服务端:
//vc的第一套socket,第二套socket,可能存在冲突 #define WIN32_LEAN_AND_MEAN #define _WINSOCK_DEPRECATED_NO_WARNINGS //也可以放到工程属性,预处理中 #include <windows.h> #include <WinSock2.h> #include <stdio.h> #pragma comment(lib, "ws2_32.lib") enum CMD { CMD_LOGIN, CMD_LOGOUT, CMD_ERROR }; struct DataHeader { int dataLength; CMD cmd; }; struct Login { char userName[32]; char passWord[32]; }; struct LoginResult { int ret; }; struct LogOut { char userName[32]; }; struct LogOutResult { int ret; }; int main() { //启动Windows socket 2.x环境 WORD ver = MAKEWORD(2, 2); WSADATA dat; WSAStartup(ver, &dat); //--用Socket API建立简易TCP服务端 //1.建立一个socket SOCKET _sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //2 bind绑定用于接受客户端连接的网络端口 sockaddr_in _sin = {}; _sin.sin_family = AF_INET; _sin.sin_port = htons(4567);//host to net unsigned _sin.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); //使用127.0.0.1可以防止外网访问 //启用本机全部的ip地址可以使用,INADDR_ANY if (SOCKET_ERROR == bind(_sock, (sockaddr*)&_sin, sizeof(sockaddr_in))) { printf("错误,绑定网络端口失败...\n"); } else { printf("绑定网络端口成功...\n"); } //3 listen 监听网络端口 if (SOCKET_ERROR == listen(_sock, 5)) { printf("错误,监听网络端口失败...\n"); } else { printf("监听网络端口成功...\n"); } //4 accept等待接受客户端连接 sockaddr_in clientAddr = {}; int nAddrLen = sizeof(clientAddr); SOCKET _cSock = INVALID_SOCKET; _cSock = accept(_sock, (sockaddr*)&clientAddr, &nAddrLen); if (INVALID_SOCKET == _cSock) { printf("错误,接受到无效客户端socket...\n"); } printf("新客户端加入:IP=%s\n", inet_ntoa(clientAddr.sin_addr)); while (true) { //5 接收客户端数据 DataHeader header = {}; int nLen = recv(_cSock, (char*)&header, sizeof(DataHeader), 0); if (nLen <= 0) { printf("客户端已退出,任务结束。\n"); break; } switch (header.cmd) { case CMD_LOGIN: { Login login = {}; recv(_cSock, (char*)&login, sizeof(Login), 0); printf("logint,user:%s login,password:%s\n", login.userName, login.passWord); //---------------------------- DataHeader header = {}; header.dataLength = sizeof(LoginResult); header.cmd = CMD_LOGIN; LoginResult longinRet = {1}; send(_cSock, (const char*)&header, sizeof(DataHeader), 0); send(_cSock, (const char*)&longinRet, sizeof(LoginResult), 0); } break; case CMD_LOGOUT: { LogOut log_out = {}; recv(_cSock, (char*)&log_out, sizeof(LogOut), 0); printf("logout,user:%s loginout\n", log_out.userName); //---------------------------- //---------------------------- DataHeader header = {}; header.dataLength = sizeof(LogOutResult); header.cmd = CMD_LOGIN; LogOutResult logOutRet = { 1 }; send(_cSock, (const char*)&header, sizeof(DataHeader), 0); send(_cSock, (const char*)&logOutRet, sizeof(LogOutResult), 0); } break; default: { //------- DataHeader header = {}; header.cmd = CMD_ERROR; header.dataLength = 0; send(_cSock, (const char*)&header, sizeof(DataHeader), 0); } break; } } //6 关闭套接字closesocket closesocket(_sock); //清除Windows socket环境 WSACleanup(); printf("已退出。\n"); getchar(); return 0; }
客户端:
#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_LOGIN, CMD_LOGOUT, CMD_ERROR }; struct DataHeader { int dataLength; CMD cmd; }; struct Login { char userName[32]; char passWord[32]; }; struct LoginResult { int ret; }; struct LogOut { char userName[32]; }; struct LogOutResult { int ret; }; int main() { //启动Windows socket 2.x环境 WORD ver = MAKEWORD(2, 2); WSADATA dat; WSAStartup(ver, &dat); //--------------------------- //--用Socket API建立简易TCP客户端 //1 建立一个socket SOCKET _sock = INVALID_SOCKET; _sock = socket(AF_INET, SOCK_STREAM, 0); if (INVALID_SOCKET == _sock) { printf("错误,建立Socket失败...\n"); } else { printf("建立Socket成功...\n"); } //2 连接服务器connect sockaddr_in _sin = {}; _sin.sin_family = AF_INET; _sin.sin_port = htons(4567);//host to net unsigned _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"); closesocket(_sock); //清除Windows socket环境 WSACleanup(); printf("已退出。"); getchar(); return 0; } else { printf("连接服务端成功...\n"); } while (true) { //3输入请求命令 char cmdBuf[128] = {}; scanf_s("%s", cmdBuf, 128); getchar(); //4处理请求 if (0 == strcmp(cmdBuf, "exit")) { printf("收到exit命令,任务结束。\n"); break; } else if(0 == strcmp(cmdBuf, "login")) { DataHeader header = {}; header.dataLength = sizeof(Login); header.cmd = CMD_LOGIN; Login login = { "xx", "xxx" }; send(_sock, (const char*)&header, sizeof(DataHeader), 0); send(_sock, (const char*)&login, sizeof(Login), 0); DataHeader ret_header = {}; LoginResult cmd_ret = {}; recv(_sock, (char*)&ret_header, sizeof(DataHeader), 0); recv(_sock, (char*)&cmd_ret, sizeof(LoginResult), 0); printf("Login result:%d\n", cmd_ret.ret); } else if (0 == strcmp(cmdBuf, "logout")) { DataHeader header = {}; header.dataLength = sizeof(Login); header.cmd = CMD_LOGOUT; LogOut logout = { "xx" }; send(_sock, (const char*)&header, sizeof(DataHeader), 0); send(_sock, (const char*)&logout, sizeof(LogOut), 0); DataHeader ret_header = {}; LogOutResult cmd_ret = {}; recv(_sock, (char*)&ret_header, sizeof(DataHeader), 0); recv(_sock, (char*)&cmd_ret, sizeof(LogOutResult), 0); printf("Logout result:%d\n", cmd_ret.ret); } else { printf("wrong cmd, please input cmd again.\n"); } } //7 关闭套接字closesocket closesocket(_sock); //清除Windows socket环境 WSACleanup(); printf("已退出"); getchar(); return 0; }