简体   繁体   English

如何在C套接字上将超时设置为“ connect()”函数?

[英]How to set a timeout to “connect()” function on C sockets?

I have to connect a several ips on same subnet concurrently. 我必须同时在同一子网上连接几个IP。 Buy its takes me too long! 买它花了我太多时间! (almost 25 seconds) to wait untill the all " connect() " functions done! (将近25秒)等待所有“ connect() ”函数完成!

How can I oprtimize this behavior in winsock, CPP? 如何在Winsock,CPP中优化这种行为? I find about somthing called " select " but aperrently it works only for linux so.. 我发现一种叫做“ select ”的东西,但目前看来它仅适用于linux。

select() works on Unix, Linux, Windows, Netware, and every other platform I have ever tested it on. select()可在Unix,Linux,Windows,Netware以及我曾经在其上进行过测试的所有其他平台上使用。

  1. Put the socket into non-blocking mode. 将套接字置于非阻塞模式。
  2. Call connect() . 调用connect() It will fail with EAGAIN/EWOULDBLOCK or EINPROGRESS. 它将因EAGAIN / EWOULDBLOCK或EINPROGRESS而失败。
  3. Call select() with your desired timeout, with the socket set into the writefds , ie select for writeability. 在套接字设置为writefds ,以所需的超时时间调用select() ,即选择可写性。
  4. When the socket becomes writeable, it is connected. 当套接字变为可写状态时,便已连接。 If that doesn't happen within your timeout period, close it, otherwise reset it to blocking mode if you prefer and proceed. 如果在超时时间内没有发生这种情况,请将其关闭,否则请根据需要将其重置为阻止模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM