When using WinSocket, send (), recv () process sometimes due to network conditions and other reasons, send and receive can not be expected to do, set the time to send and receive:
int nnettimeout = 1000; 1 seconds
Delivery time limit
setsockopt (socket, Sol_socket, So_sndtimeo, (char *) &nnettimeout, sizeof (int));
Receiving time limit
setsockopt (socket, Sol_socket, So_rcvtimeo, (char *) &nnettimeout, sizeof (int));
But when our client connects to the server via the Connect function, the time-out is very long, so how do we set the time-out for connect? After checking the data, we found that the Select method can query the status of socket sockets.
Fd_set RfD; // Descriptor Set this will test whether the connection is available with a struct timeval timeout; // Time Structure body Fd_zero ($RFD); 60; //0; one out of 10,000 seconds, microseconds u_long ul=1; represents non-blocking ioctlsocket (Socket,fionbio, $ul); // set to non-blocking connection
Below begins the Connectconnect (socket, (sockaddr*) &addrclient,sizeof (sockaddr)) fd_set (SOCK,&RFD); After the connection, add the socket to the descriptor RfB so that the socket can be tested. A descriptor can be added under multiple sockets to execute the SELECT function, making a query for ret = SELECT (0, 0, &rfd, 0, &timeout), if (ret<=0) {Description timed out, still not connected successfully and then can be The corresponding processing in the program} If the connection is successful within the timeout period, the socket is reset to the blocking status as follows UL = 0;ioctlsocket (Socket,fionbio,&ul); The normal recv and send operations can be performed below. By the way, the operation of sending data to multiple network cards is recorded. When the client goes to the active Connection server (TCP), the default is no need to specify, local IP and port, the operating system will automatically assign you the port, and then automatically select the egress according to the route. But when you want to take the initiative according to different data to choose a different NIC to send, we should do it. The answer is simple,
Sockaddr_in addrself; // Local address addrSelf.sin_addr.s_addr = inet_addr ("192.168.1.110"); Specify the address of the NIC addrself.sin_family =
Addrself.sin_port = htons (20000); if (-1 = = bind (Sockclient[i], (sockaddr*) &addrself,sizeof (SOCKADDR)))// Forcibly bind the NIC address to Soket {bind succeeded}
Reprint please indicate the original connection www.cnblogs.com/gaoteng/p/3767967.html Snow Wolf's personal blog
C + + Socket timeout setting