Linux, sockets, non-blocking connect

前端 未结 3 683
北海茫月
北海茫月 2020-12-25 14:42

I want to create a non-blocking connect. Like this:

socket.connect(); // returns immediately

For this, I use another thread, an infinite lo

3条回答
  •  臣服心动
    2020-12-25 15:25

    There are a few ways to test if a nonblocking connect succeeds.

    1. call getpeername() first, if it failed with error ENOTCONN, the connection failed. then call getsockopt with SO_ERROR to get the pending error on the socket
    2. call read with a length of 0. if the read failed, the connection failed, and the errno for read indicates why the connection failed; read returns 0 if connection succeeds
    3. call connect again; if the errno is EISCONN, the connection is already connected and the first connect succeeded.

    Ref: UNIX Network Programming V1

提交回复
热议问题