Linux, sockets, non-blocking connect
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 0 nor EINPROGRESS , then abort with error
- wait until fd is 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.
Solution 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
Solution 3
D. J. Bernstein gathered together various methods how to check if an asynchronous connect() call succeeded or not. Many of these methods do have drawbacks on certain systems, so writing portable code for that is unexpected hard. If anyone want to read all the possible methods and their drawbacks, check out this document.
For those who just want the tl;dr version, the most portable way is the following:
Once the system signals the socket as writable, first call getpeername() to see if it connected or not. If that call succeeded, the socket connected and you can start using it. If that call fails with ENOTCONN , the connection failed. To find out why it failed, try to read one byte from the socket read(fd, &ch, 1) , which will fail as well but the error you get is the error you would have gotten from connect() if it wasn’t non-blocking.
Non-blocking Linux server socket
I want to create a server socket that always prints to screen «tick» , and if one client sends data to this server, the server will print that data. I use non-blocking socket, but the it does not work, server prints to screen «tick» but cannot receives data from client. Server
int main(int argc, char *argv[]) < int server_sockfd, client_sockfd; sockaddr_un server_address; sockaddr_un client_address; int client_len; int res; /* remove old socket and create unnamed socket */ unlink("server_socket"); server_sockfd = socket(AF_UNIX, SOCK_STREAM, 0); /* non-blocking socket */ fcntl(server_sockfd, F_SETFL, O_NONBLOCK); /* name the socket */ server_address.sun_family = AF_UNIX; strcpy(server_address.sun_path, "server_socket"); bind(server_sockfd, (sockaddr*)&server_address, sizeof(server_address)); /* listen client */ printf("server_waiting\n"); listen(server_sockfd, 5); client_len = sizeof(client_address); client_sockfd = accept(server_sockfd, (sockaddr*)&client_address, (socklen_t*)&client_len); while(1) < char ch; res = recv(client_sockfd, &ch, 1, 0); if (res == -1) < printf("tick\n"); >else < printf("received: %c\n", ch); >> >
int main(int argc, char *argv[]) < int sock_fd; struct sockaddr_un address; int result; char ch = 'A'; /* create socket for client */ sock_fd = socket(AF_UNIX, SOCK_STREAM, 0); /* name of socket as agreed with server */ address.sun_family = AF_UNIX; strcpy(address.sun_path, "server_socket"); result = connect(sock_fd, (sockaddr*) &address, sizeof(address)); if (result == -1) < perror("fail\n"); exit(1); >/* write via socket */ send(sock_fd, &ch, 1, 0); close(sock_fd); exit(0); >
1 Answer 1
You are setting the listing socket to be non-blocking instead of the accepted socket.
Following your code logic, you DO want to wait on the accept call, but not the recv call
/* non-blocking socket */ fcntl(server_sockfd, F_SETFL, O_NONBLOCK);
Delete it and instead add the fcntl call to the socket you are getting back from the accept call, like
client_sockfd = accept(. ); int flags = fcntl(client_sockfd, F_GETFL, 0); fcntl(client_sockfd, F_SETFL, flags | O_NONBLOCK);
accept and fcntl can fail so you should check for failures in production code.