从零构建自己的远控•TCP构建socket通信(6)
也是先tcp模拟简单通信后面封装成类
服务器
// ConsoleApplication1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // #include <stdio.h> #include <winsock2.h> #include <WS2tcpip.h> #pragma comment(lib,"ws2_32.lib") #include <string.h> #include <map> //int _stdcall DWORD WINAPI RecvThread(LPVOID lpParameter)//接收消息线程 { SOCKET sTCP = (SOCKET)lpParameter; char szBuffer[256];//缓冲区 代表每次从从缓存区拿256个字节处理 while (1) { int nLen; //先接收长度 recv(sTCP, (char*)&nLen, sizeof(nLen), 0);//先拿长度 //注意客户端发送包过大 缓冲区需要 for循环接收数据 int nRet = recv(sTCP, szBuffer, nLen, 0);//告诉从包里拿多长数据 if (nRet <= 0) { printf("WSAStartup error%08X", WSAGetLastError()); break; } szBuffer[nRet] = '\0'; printf("buf:%s len:%d\n", szBuffer, nRet); } //nagle算法 //关闭初始化 closesocket(sTCP); return 0; } int main() { WSADATA wsaData; if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { printf("WSAStartup error%08X", WSAGetLastError()); return 0; } //1.初始化套接字 SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //2.绑定端口 sockaddr_in addr; //转换大小尾 addr.sin_family = AF_INET; addr.sin_port = htons(12345); inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr); if (bind(s, (sockaddr*)&addr, sizeof(addr)) != 0) { printf("WSAStartup error%08X", WSAGetLastError()); return 0; } //3.监听 设置队列连接数 listen(s, 100); //4.收客户连接 sockaddr_in caddr; //客户端地址 int nLength = sizeof(caddr); while (true) { SOCKET sTCP = accept(s, (sockaddr*)&caddr, &nLength); //收发数据 返回成功说明已经3次握手连接 char IPdotdec[20]; //存放点分十进制IP地址 inet_ntop(AF_INET, &caddr.sin_addr, IPdotdec, 16);//点阵转换ip printf("ip:%s,port:%d\n", IPdotdec, htons(caddr.sin_port)); CreateThread(NULL,0, RecvThread,(LPVOID)sTCP, 0,NULL);//循环线程 } //nagle算法 //关闭初始化 closesocket(s); //反初始化库 WSACleanup(); }
客户端
// ConsoleApplication2.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // #include <stdio.h> #include <winsock2.h> #include <WS2tcpip.h> #pragma comment(lib,"ws2_32.lib") DWORD WINAPI RecvThread(LPVOID lpParameter)//接收消息线程 { SOCKET s = (SOCKET)lpParameter; char szBuffer[256]; sockaddr_in caddr; //客户端地址 int nLength = sizeof(caddr); while (1) { //接收 返回接收长度 int nRet = recvfrom(s, szBuffer, sizeof(szBuffer), 0, (sockaddr*)&caddr, &nLength); if (nRet == SOCKET_ERROR) { printf("recvfrom error %08X", WSAGetLastError()); break; } szBuffer[nRet] = '\0'; char IPdotdec[20]; //存放点分十进制IP地址 struct in_addr s; // IPv4地址结构体 inet_ntop(AF_INET, &caddr.sin_addr, IPdotdec, 16);//点阵转换ip printf("ip:%s port:%d data:%s bytes:%d\n", IPdotdec, caddr.sin_port, szBuffer, nRet); } } int main() { WSADATA wsaData; if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { printf("WSAStartup error%08X", WSAGetLastError()); return 0; } //1.初始化套接字 SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //2.l连接 sockaddr_in addr; //转换大小尾 addr.sin_family = AF_INET; addr.sin_port = htons(12345); inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr); if (connect(s, (sockaddr*)&addr, sizeof(addr)) == SOCKET_ERROR) { printf("connect error%08X", WSAGetLastError()); return 0; } printf("connect ok"); //3.发数据 char szBuffer[256]; while (1) { scanf_s("%s", szBuffer, sizeof(szBuffer)); int nLen = strlen(szBuffer); fflush(stdin); //发送数据 // int nRet = send(s, szBuffer, strlen(szBuffer), 0); 防止粘包 send(s, (char *)&nLen, sizeof(nLen), 0); int nRet=send(s,szBuffer, nLen, 0); if (nRet <= 0) { printf("connect error%08X", WSAGetLastError()); break; } } //关闭初始化 closesocket(s); //反初始化库 WSACleanup(); }
从此山高路远,纵马扬鞭。愿往后旅途,三冬暖,春不寒,天黑有灯,下雨有伞。此生尽兴,不负勇往。