开发者

what is the meaning of a UDP "blocking socket" when it comes to receiving data through it?

I get the return value of the read operation as 0 if the sockets is non blocking, and the real number of bytes read if the开发者_Python百科 socket is marked as blocking. cant understand why though ...

this is on an embedded OS, but supposed to be Berkely sockets


a blocking read will wait until there's data available to read. a non-blocking read will always return immediately (whether 0 bytes were available or more).

   Upon successful completion, recv() shall return the length of the mes-
   sage  in  bytes.  If  no messages are available to be received and the
   peer has performed an orderly shutdown, recv() shall return 0.  Other-
   wise, -1 shall be returned and errno set to indicate the error.


A blocking setup means that when you read data from the socket, it will stay stuck there until two things happen: 1) you get data or 2) you get a signal.

A non-blocking setup means that when you try to read, if data are available it will return them. If there is nothing it returns immediately instead of waiting. This is useful if you don't want to wait forever for data, and in the meantime you want to do something else, like doing computation, GUI redraw, or serve other requests.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜