开发者

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

开发者 https://www.devze.com 2023-02-17 08:51 出处:网络
Is it possible to implement a non-blocking client socket? I have tried using sockfd = socket(AF_INET, SOCK_STREAM, 0);

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, i开发者_JAVA百科t 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消