目录
用Socket API建立简易TCP服务端、客户端
- 用Socket API建立简易TCP服务端
- 建立一个socket
- 绑定接受客户端连接的端口 bind
- 监听网络端口 listen
- 等待接受客户端连接 accept
- 向客户端发送一条数据send
- 关闭socket closesocket
- 用Socket API建立简易TCP客户端
- 建立一个socket
- 连接服务器 connect
- 接收服务器信息 recv
- 关闭socket closesocket
设置工程输出目录和中间目录
$(SolutionDir)../bin/$(Platform)/$(Configuration)\ $(SolutionDir)../temp/$(Platform)/$(Configuration)/$(ProjectName)\
创建基本的Windows网络环境
- 引入头文件,WinSock2.h放在上面,否则出现重定义问题
#include <WinSock2.h> #include <windows.h>
- 或者使用宏 #define WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN #include <windows.h> #include <WinSock2.h>
- 创建基本的windows网络环境程序
#define WIN32_LEAN_AND_MEAN #include <windows.h> #include <WinSock2.h> #pragma comment(lib,"ws2_32.lib") int main() { WORD ver = MAKEWORD(2,2); WSAData dat; WSAStartup(ver, &dat); WSACleanup(); return 0; }
- 上面在代码中添加,为了跨平台,可以选择在项目属性,链接器,输入,附加依赖库中添加,而不写在代码中
- 最终项目调试结果:
Windows服务端程序
#define WIN32_LEAN_AND_MEAN #define _WINSOCK_DEPRECATED_NO_WARNINGS #include <windows.h> #include <WinSock2.h> #include <cstdio> #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); //2,bind 绑定用于接收客户端连接的端口 sockaddr_in sinAddr = {}; sinAddr.sin_family = AF_INET; sinAddr.sin_port = htons(5678); //host to net unsigned short sinAddr.sin_addr.S_un.S_addr = INADDR_ANY; //inet_addr("127.0.0.1") if (SOCKET_ERROR == bind(sock, (sockaddr*)&sinAddr, sizeof(sockaddr_in))) { printf("Bind Error\n"); } else { printf("Bind Success\n"); } //3. listen 监听网络端口 if (SOCKET_ERROR == listen(sock, 5)) { printf("Listen Error\n"); } else { printf("Listen Success\n"); } //4.accept 接收客户端连接 sockaddr_in clientAddr = {}; int clAddrLen = sizeof(sockaddr_in); SOCKET sockAccpt = INVALID_SOCKET; char msgBuff[] = "Hello,I am Sever."; int msgBuffLen = strlen(msgBuff) + 1; while(true) { sockAccpt = accept(sock, (sockaddr*)&clientAddr, &clAddrLen); if (INVALID_SOCKET == sockAccpt) { printf("Accept Error\n"); } else { printf("Accept Success\n"); } printf("新客户端地址:IP = %s \n", inet_ntoa(clientAddr.sin_addr)); // 5.send 向客户端发送数据 send(sockAccpt, msgBuff, msgBuffLen, 0); } // 6.closesocket 关闭套接字 closesocket(sock); closesocket(sockAccpt); WSACleanup(); return 0; }
- 针对于Windows设计的某些错误,如需要定义#define _WINSOCK_DEPRECATED_NO_WARNINGS,也可以通过关闭SDL检查排除
Windows客户端程序
#define WIN32_LEAN_AND_MEAN #define _WINSOCK_DEPRECATED_NO_WARNINGS #include <windows.h> #include <WinSock2.h> #include <cstdio> #pragma comment(lib,"ws2_32.lib") int main() { WORD ver = MAKEWORD(2, 2); WSAData dat; WSAStartup(ver, &dat); //1.建立一个socket SOCKET sockCli = socket(AF_INET, SOCK_STREAM, 0); if (INVALID_SOCKET == sockCli) { printf("Socket Error\n"); } else { printf("Socket Success\n"); } //2. connect连接服务器 sockaddr_in servAddr = {}; servAddr.sin_family = AF_INET; servAddr.sin_port = htons(5678); servAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); int ret = connect(sockCli, (sockaddr*)&servAddr, sizeof(sockaddr_in)); if (SOCKET_ERROR == ret) { printf("Connect Error\n"); } else { printf("Connect Success\n"); } //3.接收服务器信息 recv char recvBuff[256] = {}; int nlen = recv(sockCli, recvBuff, 256, 0); if (nlen > 0) { printf("接收到数据:%s \n", recvBuff); } //4.关闭套接字 closesocket closesocket(sockCli); WSACleanup(); getchar(); return 0; }
- 结果显示
- 上述服务器与客户端通信过程小结: