UDP“阻塞套接字”是什么意思?当谈到通过它接收数据时?

发布于 2024-08-18 07:24:40 字数 105 浏览 0 评论 0原文

如果套接字是非阻塞的,则读取操作的返回值为 0;如果套接字被标记为阻塞,则读取的实际字节数。虽然无法理解为什么......

这是在嵌入式操作系统上,但应该是 Berkely 套接字

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 socket is marked as blocking. cant understand why though ...

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清风无影 2024-08-25 07:24:40

阻塞读取将等待,直到有数据可供读取。非阻塞读取将始终立即返回(无论是否有 0 字节可用或更多)。

   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 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.
暖阳 2024-08-25 07:24:40

阻塞设置意味着当您从套接字读取数据时,它将停留在那里,直到发生两件事:1)您获取数据或 2)您收到信号。

非阻塞设置意味着当您尝试读取时,如果数据可用,它将返回它们。如果没有任何内容,它会立即返回而不是等待。如果您不想永远等待数据,并且同时想要做其他事情,例如进行计算、GUI 重绘或服务其他请求,那么这非常有用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文