您的位置:首页 > 其它

How to detect whether socket is still connected...

2014-07-19 22:07 330 查看


"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

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}

/*
@licstart The following is the entire license notice for the
JavaScript code in this tag.

Copyright (C) 2012-2013 Free Software Foundation, Inc.

The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.

As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.

@licend The above is the entire license notice
for the JavaScript code in this tag.
*/


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 中国大陆许可协议 。)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: