Non-blocking connect()?

Started by
2 comments, last by zppz 17 years, 6 months ago
Hello. Is there anything i can do to know when a non-blocking connect finishes? I need to know when the connection is accepted or if it times out. Im using berkely socket functions only, not winsock asyncronus functions. So i guess there must be a way to poll the socket with select() or something. But how do i know if the connection was accepted, timedout, or rejected? Thanks in advance.
Shields up! Rrrrred alert!
Advertisement
Use "select" to determine if the socket is write ready or in an error state.
This source code has a tcp_client::Connect class that performs non-blocking connect() through ioctlsocket() by manually breaking down the connect timeout period into 250ms slices:

http://cwiki.org/index.php/Winsock_TCP_Client-Server_Library
It's also possible to use non-blocking sockets. Just keep calling connect() and handle the errors appropriately:
The first call will probably return WOULDBLOCK or INPROGRESS which is fine, after that you will get INVAL or ALREADY which can be ignored. When the connection succeeds you will get ISCONN, then you can stop calling connect() and start using the socket.


This topic is closed to new replies.

Advertisement