问题
I want to create a non-blocking connect. Like this:
socket.connect(); // returns immediately
For this, I use another thread, an infinite loop and Linux epoll. Like this(pseudocode):
// in another thread
{
create_non_block_socket();
connect();
epoll_create();
epoll_ctl(); // subscribe socket to all events
while (true)
{
epoll_wait(); // wait a small time(~100 ms)
check_socket(); // check on EPOLLOUT event
}
}
If I run a server and then a client, all it works. If I first run a client, wait a some small time, run a server, then the client doesn't connect.
What am I doing wrong? Maybe it can be done differently?
回答1:
You should use the following steps for an async connect:
- create socket with
socket(..., SOCK_NONBLOCK, ...) - start connection with
connect(fd, ...) - if return value is neither
0norEINPROGRESS, then abort with error - wait until
fdis signalled as ready for output - check status of socket with
getsockopt(fd, SOL_SOCKET, SO_ERROR, ...) - done
No loops - unless you want to handle EINTR.
If the client is started first, you should see the error ECONNREFUSED in the last step. If this happens, close the socket and start from the beginning.
It is difficult to tell what's wrong with your code, without seeing more details. I suppose, that you do not abort on errors in your check_socket operation.
回答2:
There are a few ways to test if a nonblocking connect succeeds.
- 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
- 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
- call connect again; if the errno is EISCONN, the connection is already connected and the first connect succeeded.
Ref: UNIX Network Programming V1
来源:https://stackoverflow.com/questions/17769964/linux-sockets-non-blocking-connect