简体   繁体   中英

Can send() become non-blocking automatically?

Do I have to wait for signals with select() in order to send something in non-blocking sockets ? What if I have always have something to send and then I call send() function ? I mean, everytime i call send(), there is definitely some data to send with fixed length. Does it mean that send will not block ?

A call to send will never block on a non-blocking socket. If the data could not be sent (for example if the send buffer is full), then send will return immediately with a value of SOCKET_ERROR .

Any call that would block is signaled by an error code of WSAEWOULDBLOCK (by calling WSAGetLastError ).

The same is true for a call to receive , if there is no data in the receive buffer, the call still returns immediately, with an error.

That depends on the protocol. With regular blocking TCP socket send() might block since in-kernel socket buffer might be full. UDP, on the other hand, will either push the packet down the stack, or discard it if it's too big, without blocking.

Edit 0:

Ok, thanks to @Cicada, I see I missed that we are talking about non-blocking sockets, so ...

It still depends on the protocol - with UDP your packet is either sent or discarded; with TCP you try to send at the time you have data, and if you get EWOULDBLOCK you register your socket with select() / poll() , and re-try sending when kernel tells you the socket is writable again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM