一个简单的tcp非阻塞connect的客户端

本文详细介绍了如何实现一个简单的TCP非阻塞连接客户端,包括连接建立过程、非阻塞I/O的工作原理以及相关编程技巧。通过实例代码,读者可以理解如何避免传统阻塞式connect导致的等待问题,提升客户端的响应效率。
摘要由CSDN通过智能技术生成
我们知道,tcp客户端要与服务端通信,必须先建立连接,即调用connect函数完成三次握手,而默认情况下connect是阻塞方式的,也就是说调用connect函数会发生阻塞,超时时间可能在75s至几分钟之间。当然同一主机除外,同一主机上调用connect通常会立即成功。

为避免长时间的connect阻塞,可以使用如下非阻塞connect方式来处理:
一、 创建socket,返回套接口描述符
二、 调用fcntl把套接口描述符设置成非阻塞
三、 调用connect开始建立连接
四、 判断连接是否成功建立                
        A: 如果connect返回0,表示连接成功(服务器和客户端在同一台机器上时就有可能发生这种情况)                
        B: 调用select来等待连接建立成功完成                                
    (Berkeley的实现(和Posix.1g)有两条与select和非阻塞IO相关的规则: 
     1.当连接建立成功时,套接口描述符变成可写;
     2.当连接出错时,套接口描述符变成既可读又可写;
     注意:当一个套接口出错时,它会被select调用标记为既可读又可写;)
五、 继续判断select返回值
         如果select返回0,则表示建立连接超时; 我们返回超时错误给用户,同时关闭连接,以防止三路握手操作继续进行下去
         如果select返回大于0的值,则需要检查套接口描述符是否可读或可写;如果套接口描述符可读或可写,则
好的,以下是一个简单非阻塞模式TCP客户端的C++代码实现: 首先,我们需要包含WinSock2.h头文件和Windows.h头文件,并链接ws2_32.lib库文件: ```cpp #include <WinSock2.h> #include <Windows.h> #pragma comment(lib, "ws2_32.lib") ``` 然后,我们需要初始化WinSock2库: ```cpp WSADATA wsaData; int ret = WSAStartup(MAKEWORD(2, 2), &wsaData); if (ret != 0) { printf("WSAStartup failed with error %d\n", ret); return -1; } ``` 接下来,创建一个socket: ```cpp SOCKET clientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (clientSocket == INVALID_SOCKET) { printf("socket failed with error %d\n", WSAGetLastError()); WSACleanup(); return -1; } ``` 然后,将socket设置为非阻塞模式: ```cpp u_long iMode = 1; ret = ioctlsocket(clientSocket, FIONBIO, &iMode); if (ret != NO_ERROR) { printf("ioctlsocket failed with error %d\n", ret); closesocket(clientSocket); WSACleanup(); return -1; } ``` 接下来,定义服务器的地址和端口号: ```cpp const char* serverAddr = "127.0.0.1"; const int serverPort = 12345; ``` 创建一个sockaddr_in结构体,用于存储服务器的地址和端口号: ```cpp sockaddr_in serverAddrIn; serverAddrIn.sin_family = AF_INET; serverAddrIn.sin_addr.s_addr = inet_addr(serverAddr); serverAddrIn.sin_port = htons(serverPort); ``` 然后,连接服务器: ```cpp ret = connect(clientSocket, (sockaddr*)&serverAddrIn, sizeof(serverAddrIn)); if (ret == SOCKET_ERROR) { if (WSAGetLastError() != WSAEWOULDBLOCK) { printf("connect failed with error %d\n", WSAGetLastError()); closesocket(clientSocket); WSACleanup(); return -1; } } ``` 由于是非阻塞模式,所以连接函数可能会返回WSAEWOULDBLOCK错误,这是正常情况。 接下来,我们需要使用select函数来检查连接是否完成: ```cpp fd_set writeSet; FD_ZERO(&writeSet); FD_SET(clientSocket, &writeSet); timeval timeVal; timeVal.tv_sec = 5; timeVal.tv_usec = 0; ret = select(0, NULL, &writeSet, NULL, &timeVal); if (ret == SOCKET_ERROR) { printf("select failed with error %d\n", WSAGetLastError()); closesocket(clientSocket); WSACleanup(); return -1; } else if (ret == 0) { printf("select timed out\n"); closesocket(clientSocket); WSACleanup(); return -1; } ``` 如果连接完成,writeSet中的clientSocket将变为可写状态。 最后,我们可以向服务器发送数据: ```cpp char* sendData = "Hello, server!"; int sendLen = strlen(sendData); ret = send(clientSocket, sendData, sendLen, 0); if (ret == SOCKET_ERROR) { printf("send failed with error %d\n", WSAGetLastError()); closesocket(clientSocket); WSACleanup(); return -1; } ``` 完整的代码如下: ```cpp #include <WinSock2.h> #include <Windows.h> #pragma comment(lib, "ws2_32.lib") int main() { WSADATA wsaData; int ret = WSAStartup(MAKEWORD(2, 2), &wsaData); if (ret != 0) { printf("WSAStartup failed with error %d\n", ret); return -1; } SOCKET clientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (clientSocket == INVALID_SOCKET) { printf("socket failed with error %d\n", WSAGetLastError()); WSACleanup(); return -1; } u_long iMode = 1; ret = ioctlsocket(clientSocket, FIONBIO, &iMode); if (ret != NO_ERROR) { printf("ioctlsocket failed with error %d\n", ret); closesocket(clientSocket); WSACleanup(); return -1; } const char* serverAddr = "127.0.0.1"; const int serverPort = 12345; sockaddr_in serverAddrIn; serverAddrIn.sin_family = AF_INET; serverAddrIn.sin_addr.s_addr = inet_addr(serverAddr); serverAddrIn.sin_port = htons(serverPort); ret = connect(clientSocket, (sockaddr*)&serverAddrIn, sizeof(serverAddrIn)); if (ret == SOCKET_ERROR) { if (WSAGetLastError() != WSAEWOULDBLOCK) { printf("connect failed with error %d\n", WSAGetLastError()); closesocket(clientSocket); WSACleanup(); return -1; } } fd_set writeSet; FD_ZERO(&writeSet); FD_SET(clientSocket, &writeSet); timeval timeVal; timeVal.tv_sec = 5; timeVal.tv_usec = 0; ret = select(0, NULL, &writeSet, NULL, &timeVal); if (ret == SOCKET_ERROR) { printf("select failed with error %d\n", WSAGetLastError()); closesocket(clientSocket); WSACleanup(); return -1; } else if (ret == 0) { printf("select timed out\n"); closesocket(clientSocket); WSACleanup(); return -1; } char* sendData = "Hello, server!"; int sendLen = strlen(sendData); ret = send(clientSocket, sendData, sendLen, 0); if (ret == SOCKET_ERROR) { printf("send failed with error %d\n", WSAGetLastError()); closesocket(clientSocket); WSACleanup(); return -1; } closesocket(clientSocket); WSACleanup(); return 0; } ``` 希望对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值