f.ardelian
f.ardelian

Reputation: 6956

Linux client socket: can't set O_NONBLOCK before connect?

Is it possible to implement a non-blocking client socket? I have tried using

sockfd = socket(AF_INET, SOCK_STREAM, 0);
fcntl(sockfd, F_SETFL, O_NONBLOCK);
connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr));

but when the application reaches the connect statement, it exists with the following error:

ERROR connecting: Operation now in progress

I need to create a completely non-blocking socket, that doesn't even wait for the TCP handshake. Is this possible using standard socket functions or do I have to go into threading?

Upvotes: 2

Views: 4678

Answers (1)

Benoit Thiery
Benoit Thiery

Reputation: 6387

The error you receive is normal when in non-blocking mode. It just means that the operation could not be completed immediatly. That means you need to wait on the file descriptor for the connect to be finished before you can use it.

This is the way non-blocking sockets are working in C.

Upvotes: 3

Related Questions