How to detect whether socket is still connected...

时间:2022-01-01 00:44:23

How to detect whether socket is still connected…

*/-->

div.org-src-container {
font-size: 85%;
font-family: monospace;
}
pre.src {
background-color:#2e3436;
color:#fefffe;
}

p {font-size: 15px}
li {font-size: 15px}

From wget source code…

bool
test_socket_open (int sock)
{
fd_set check_set;
struct timeval to;
int ret = 0; /* Check if we still have a valid (non-EOF) connection. From Andrew
* Maholski's code in the Unix Socket FAQ. */ FD_ZERO (&check_set);
FD_SET (sock, &check_set); /* Wait one microsecond */
to.tv_sec = 0;
to.tv_usec = 1; ret = select (sock + 1, &check_set, NULL, NULL, &to);
#ifdef WINDOWS
/* gnulib select() converts blocking sockets to nonblocking in windows.
wget uses blocking sockets so we must convert them back to blocking
*/
set_windows_fd_as_blocking_socket ( sock );
#endif if ( !ret )
/* We got a timeout, it means we're still connected. */
return true;
else
/* Read now would not wait, it means we have either pending data
or EOF/error. */
return false;
}


(转载请注明出处
使用许可:署名-非商业性使用-相同方式共享 3.0 *许可协议 。)