windows系统编程2——内存管理和网络( 四 )


#include "SocketInit.hpp"#define _WINSOCK_DEPRECATED_NO_WARNINGSusing namespace std;int main(){SocketInit socketInit;// 加载动态库// 创建监听套接字SOCKET sClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);if (sClient == SOCKET_ERROR) {printf("监听套接字创建失败!\n");return -1;}sockaddr_in sock_in;sock_in.sin_family = AF_INET;// 协议族sock_in.sin_port = htons(12306);// 端口号sock_in.sin_addr.S_un.S_addr = inet_addr("192.168.3.165");if (connect(sClient, (const sockaddr*)&sock_in, sizeof(sock_in)) == SOCKET_ERROR) {printf("连接服务器失败!\n");return -1;}while (true) {char buff[1024] = { 0 };gets_s(buff, 1024);send(sClient, buff, strlen(buff), 0);}closesocket(sClient);getchar();return 0;}
3.3 多线程服务器连接多客户端
#include "SocketInit.hpp"using namespace std;DWORD WINAPI ThreadProc(LPVOID lp) {SOCKET sClient = *(SOCKET*)lp;while (true) {char buff[1024] = { 0 };intresult = recv(sClient, buff, 1024, 0);if (result > 0) {printf("接收到的数据:%s\n", buff);}else {printf("客户端断开连接!\n");break;}}return NULL;}int main(){SocketInit socketInit;// 加载动态库// 创建监听套接字SOCKET sListen = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);if (sListen == SOCKET_ERROR) {printf("监听套接字创建失败!\n");return -1;}sockaddr_in sock_in;sock_in.sin_family = AF_INET;// 协议族sock_in.sin_port = htons(12306);// 端口号sock_in.sin_addr.S_un.S_addr = INADDR_ANY; // 获取本机的IP地址// 绑定套接字int ret = bind(sListen, (const sockaddr*)&sock_in, sizeof(sockaddr_in));if (ret == SOCKET_ERROR) {printf("绑定套接字失败!\n");closesocket(sListen);return -1;}// 监听if (listen(sListen, 10) == SOCKET_ERROR) {printf("监听失败!\n");closesocket(sListen);return -1;}sockaddr_in clientAddr;int nlen = sizeof(sockaddr_in);while (true) {// 接受客户端的连接SOCKET sClient = accept(sListen, (sockaddr*)&clientAddr, &nlen);if (sClient == SOCKET_ERROR) {printf("接收客户端失败!\n");closesocket(sListen);return -1;}printf("与客户端建立连接...\n");CreateThread(NULL, NULL, ThreadProc, (LPVOID)&sClient, NULL, NULL);}closesocket(sListen);getchar();return 0;}
3.4 服务器
【windows系统编程2——内存管理和网络】#include "SocketInit.hpp"using namespace std;int main(){SocketInit socketInit;// 加载动态库// 创建监听套接字SOCKET sListen = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);if (sListen == SOCKET_ERROR) {printf("监听套接字创建失败!\n");return -1;}sockaddr_in sock_in;sock_in.sin_family = AF_INET;// 协议族sock_in.sin_port = htons(12306);// 端口号sock_in.sin_addr.S_un.S_addr = INADDR_ANY; // 获取本机的IP地址// 绑定套接字int ret = bind(sListen, (const sockaddr*)&sock_in, sizeof(sockaddr_in));if (ret == SOCKET_ERROR) {printf("绑定套接字失败!\n");closesocket(sListen);return -1;}// 监听if (listen(sListen, 10) == SOCKET_ERROR) {printf("监听失败!\n");closesocket(sListen);return -1;}FD_SET fd_read;// 存储SOCKET对象FD_ZERO(&fd_read); // 初始化FD_SET(sListen, &fd_read); // 将sListen套接字放到fd_read集合中while (true) {FD_SET fd_tmp = fd_read;const timeval tv = { 1, 0 };int ret = select(NULL, &fd_tmp, NULL, NULL, &tv);if (ret == 0) {// 如果没有网络事件Sleep(1);continue;}for (int i = 0; i < fd_tmp.fd_count; i++) {if (fd_tmp.fd_array[i] == sListen) { // 如果监听套接字有网络事件 , 证明有客户端正在连接服务器sockaddr_in clientAddr;int nlen = sizeof(sockaddr_in);// 接受客户端的连接SOCKET sClient = accept(sListen, (sockaddr*)&clientAddr, &nlen);if (sClient == SOCKET_ERROR) {printf("接收客户端失败!\n");closesocket(sListen);return -1;}printf("与客户端建立连接: %s ...\n", inet_ntoa(clientAddr.sin_addr));FD_SET(sClient, &fd_read);}else { // 如果监听套接字有网络事件 , 证明客户端正在发送数据 , 服务端接收数据char buff[1024] = { 0 };intresult = recv(fd_tmp.fd_array[i], buff, 1024, 0);if (result > 0) {printf("接收到的数据:%s\n", buff);}else {// 从fd_read中移除当前SOCKETFD_CLR(fd_tmp.fd_array[i], &fd_read);printf("客户端断开连接!\n");break;}}}}closesocket(sListen);getchar();return 0;}