A:
The Connect () destination host is up to, but the destination port is not arrival, and the function will go back immediately. But if the destination host is not up to, the function will wait for a period of error to return, how do this timeout set in the program?
Q:
You can set the time-time mechanism Before using this function, you need to set the socket to a non-lock mode, so that you will jump immediately when Connect, and a WSAEWOULDBLOCK error will usually produce. This error does not matter. It is true timeout to perform SELECT.
WSADATA WSD; SOCKET CCLIENT; int R; Struct SockAddr_in Server; Hostent * Host = NULL;
IF (WSAStartup (MakeWord (2,0), & WSD) {RETURN 0;} cclient = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); if (cclient == invalid_socket) {return 0;} // set recv and send time Outint Timeout = 6000; // Settings Send Timeout 6 seconds if (: setsockopt (cclient, sol_socket, so_sndtimeo, (char *) & timeout, sizeof (timeout) == Socket_ERROR) {Return 0;} TIMEOUT = 6000; // Setting Receive timeout 6 seconds if (: setsockopt (cclient, sol_socket, so_rcvtimeo, (char *) & timeout, sizeof (timeout)) == SOCKET_ERROR) {RETURN 0;} // Set non-blocking method connection unsigned long ul = 1; RET = IOCTLSOCKET (CClient, Fionbio, (unsigned long *) & ul); if (RET == Socket_ERROR) RETURN 0;
// Connect server.sin_Family = AF_INET; Server.sin_Port = HTONS (25); server.sin_addr .s_addr = inet_addr ((lpcstr) psmtp); if (server.sin_addr.s_addr == inaddr_none) {Return 0;}
Connect (CClient, Const Struct SockAddr *) & Server, SIZEOF (Server);
// SELECT model, set timeout Struct TimeVal Timeout; fd_set r;
FD_ZERO (& R); fd_set (cclient, & r); timeout.tv_sec = 15; // Connection timeout 15 second Timeout.tv_usec = 0; RET = SELECT (0, 0, & R, 0, & Timeout); if (Ret <= 0) {:: closssocket (cclient); return 0;} // General non-lock mode jacket is more difficult to control, can consider resetting back blocking mode Unsigned long UL1 = 0 according to the actual situation; RET = ioctlsocket (cclient, fionbio, (unsigned long *) & ul1); if (RET == Socket_ERROR) {:: closeSocket (cclient); return 0;}