非阻塞套接字-如何检查连接是否成功? [英] Non blocking socket - how to check if a connection was successful?

查看:226
本文介绍了非阻塞套接字-如何检查连接是否成功?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正确设置了非阻塞套接字后,我将执行以下连接:

After setting up a non blocking socket correctly I do the following to connect:

  1. 在套接字上调用connect.
  2. 如果返回0,则表明我已经连接,否则,请检查errno.
  3. 如果errno不是EINPROGRESS,则存在错误.
  4. 如果errnoEINPROGRESS,我可以通过以下方式轮询连接状态:

  1. Call connect on the socket.
  2. If it returns 0, I have already connected, if not, check errno.
  3. If errno is not EINPROGRESS there is an error.
  4. if errno is EINPROGRESS I can poll the connect status by:

select_status = sock_select(FD_SETSIZE, NULL, &file_descriptor_set, NULL, &timeout);

如果select_status > 0,然后与FD_ISSET检查是否设置了文件描述符.

if select_status > 0 then check with FD_ISSET if the file descriptor is set.

那是正确的吗?我应该检查fd_write而不是fd_read吗?选择后我应该打电话给getsockopt吗?有什么争论?

Is that correct? And should I check for fd_write not fd_read? Should I call getsockopt after select? With what arguments?

我找不到需要做什么的明确解释.

I cannot find a clear explanation of what needs to be done.

我确实连接了,但是我的程序未正确报告它,所以我错误地使用了select,或者是因为我没有检查getsockopt.

I do connect but my program is not reporting it correctly so I am using select wrong or it is because I don't check getsockopt.

什么告诉我建立了连接?

What tells me that the connection was made?

推荐答案

  1. 在套接字上调用connect.
  2. 如果返回0,则表明我已经连接,否则,请检查errno.
  3. 如果errno不是EINPROGRESS,则出现错误.
  1. Call connect on the socket.
  2. If it returns 0, I have already connected, if not, check errno.
  3. If errno is not EINPROGRESS there is an error.

以上所有内容都是正确的(Windows除外,您需要检查WSAEWOULDBLOCK而不是EINPROGRESS).

All of the above is correct (except under Windows you'd need to check for WSAEWOULDBLOCK instead of EINPROGRESS).

  1. 如果errno是EINPROGRESS,我可以通过以下方式轮询连接状态:select_status = sock_select(FD_SETSIZE,NULL,& file_descriptor_set,NULL,& timeout);如果select_status> 0,则检查FD_ISSET是否设置了文件描述符.

正确(假设sock_select()与select()是一样的东西),不同之处在于FD_SETSIZE应该是您正在观看的所有文件描述符值的最大值加1.

Correct (assuming sock_select() is the same thing as select()), except that FD_SETSIZE should be the maximum value of all the file-descriptor values you are watching, plus 1.

那是正确的吗?我应该检查fd_write而不是fd_read吗?

Is that correct? And should I check for fd_write not fd_read?

对.

什么告诉我建立了连接?

What tells me that the connection was made?

异步连接操作完成后,select()将返回,并且FD_ISSET(theSocket,& writeSet)将返回true.

When the async-connection-operation has completed, select() will return and FD_ISSET(theSocket, &writeSet) will return true.

发生这种情况时,您需要确定连接尝试是成功还是失败.这是我为此使用的功能:

When that happens, you need to find out if the connection attempt succeeded or failed. Here's a function I use for that:

// Returns true if the async connection on (fd) is connected; false if it failed
bool DidTheAsyncTCPConnectionSucceed(int fd)
{
   struct sockaddr_in junk;
   socklen_t length = sizeof(junk);
   memset(&junk, 0, sizeof(junk));
   return (getpeername(fd, (struct sockaddr *)&junk, &length) == 0);
}

如果返回true,则说明您的套接字已连接并可以正常使用.如果返回false,则表明异步连接失败,您应该关闭套接字并以某种方式处理该失败.

If that returns true, your socket is connected and ready to use in the normal ways. If it returns false, the async-connection failed and you should close() the socket and handle that failure somehow.

(同样,在Windows下,行为略有不同;在Windows下,如上所述,获得有关连接成功的通知的工作原理,但是如果您想获得有关连接失败的通知,则需要观看exception-fd在Windows中,当异步TCP连接失败时,会通知您FD_ISSET(fd,& exceptionSet).

(Again there is a slightly different behavior under Windows; under Windows, getting notified about the connection succeeding works as described above, but if you want to be notified about the connection failing, you'll need to watch the exception-fd-set as well. Under Windows, FD_ISSET(fd, &exceptionSet) is what will notify you when the asynchronous TCP connection failed).

这篇关于非阻塞套接字-如何检查连接是否成功?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆