建立一个能持续处理的C/S网络程序
程序流程图:
代码演示:
服务器端:
#include<WinSock2.h> #include<Windows.h> #include<stdio.h> #include<iostream> #pragma comment(lib,"ws2_32.lib") int main() { WORD ver = MAKEWORD(2, 2); WSADATA dat; //WinSocket启动 WSAStartup(ver, &dat); //1、建立一个socket SOCKET _sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //AF_INET创建一个IPV4的套接字,SOCK_STREAM面向数据流的,IPPROTO_TCP TCP if (INVALID_SOCKET == _sock) { printf("ERROR:建立失败!\n"); } //2.绑定 sockaddr_in _sin = {}; //创建网络地址 _sin.sin_family = AF_INET; _sin.sin_port = htons(4567); //Host to Network Short _sin.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); // IP地址 if (bind(_sock, (sockaddr *)&_sin, sizeof(_sin)) == SOCKET_ERROR) { printf("ERROR:绑定失败!\n"); } else { printf("TRUE:绑定成功!\n"); } //3.监听网络端口 if (listen(_sock, 5) == SOCKET_ERROR)//第二个参数为最大等待多少人可以同时连接 { printf("ERROR:监听失败!\n"); } else { printf("TRUE:监听成功!\n"); } //4.等待接收客户端连接 sockaddr_in clientAddr = {}; int nAddrLen = sizeof(sockaddr_in); SOCKET _cSOCK = INVALID_SOCKET; _cSOCK = accept(_sock, (sockaddr *)&clientAddr, &nAddrLen); if (_cSOCK == INVALID_SOCKET) { printf("ERROR:无效客户端SOCKET!\n"); } printf("新客户端加入:Socket=%d,IP = %s\n",(int)_cSOCK, inet_ntoa(clientAddr.sin_addr));//inet_ntoa(clientAddr.sin_addr)将接收到的IP地址转化为字符串 char _recvBuff[128] = {}; while (1) { //5.接收客户端新数据 int nLen = recv(_cSOCK, _recvBuff, 128, 0); if (nLen <= 0) { printf("客户端已退出!任务结束!"); break; } printf("收到命令:%s\n",_recvBuff); //6.请求处理 if (0 == strcmp(_recvBuff, "getName")) { //7.向客户端发送一条数据 char msgBuff[] = "Xiao Qiang\n"; send(_cSOCK, msgBuff, strlen(msgBuff) + 1, 0); } else if (0 == strcmp(_recvBuff, "getAge")) { //7.向客户端发送一条数据 char msgBuff[] = "80\n"; send(_cSOCK, msgBuff, strlen(msgBuff) + 1, 0); } else{ //7.向客户端发送一条数据 char msgBuff[] = "???.\n"; send(_cSOCK, msgBuff, strlen(msgBuff) + 1, 0); } } //8.关闭自身的socket closesocket(_sock); //WinSocket关闭 WSACleanup(); system("pause"); return 0; }
客户端:
#include<WinSock2.h> #include<Windows.h> #include<stdio.h> #pragma comment(lib,"ws2_32.lib") int main() { WORD ver = MAKEWORD(2, 2); WSADATA dat; WSAStartup(ver, &dat); //1.建立一个socket SOCKET _sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (INVALID_SOCKET == _sock) { printf("ERROR:建立失败!\n"); } //2.连接服务器 sockaddr_in _sin = {}; //创建网络地址 _sin.sin_family = AF_INET; _sin.sin_port = htons(4567); //Host to Network Short _sin.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");//inet_addr("127.0.0.1"); // IP地址 int ret = connect(_sock, (sockaddr *)&_sin, sizeof(sockaddr_in)); if (SOCKET_ERROR == ret) { printf("ERROR:连接失败!\n"); } else { printf("连接成功!\n"); } while (true) { //3.输入请求命令 char cmdBuff[128] = {}; scanf("%s", cmdBuff); //4.处理请求命令 if (0 == strcmp(cmdBuff, "exit")) { printf("收到exit命令,已退出1"); break; } else{ //5.向服务器发送请求命令 send(_sock, cmdBuff, strlen(cmdBuff) + 1, 0); } //6.接收服务器信息 char recvBuff[256] = {}; int nlen = recv(_sock, recvBuff, 256, 0); if (nlen <= 0) { printf("ERROR:接收失败!\n"); } else { printf("接收到数据:%s\n", recvBuff); } } //7.关闭套接字 closesocket(_sock); //WinSocket启动 WSAStartup(ver, &dat); //WinSocket关闭 WSACleanup(); printf("已退出!"); getchar(); return 0; }