• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

UNIX 域 STREAM 和 DATAGRAM 套接字:间的区别?

用户头像
it1352
帮助1

问题说明

这个问题NOT是关于STREAM类型和DATAGRAM类型INTERNET套接字的区别.我知道 STREAM 套接字使用 TCP,数据报套接字使用 UDP 以及所有 TCP、UDP 内容、按顺序到达的数据包、ACK、NACK 等.我了解这些在互联网上的重要性.

This question is NOT for the difference between STREAM type and DATAGRAM type INTERNET sockets. I know that STREAM sockets use TCP, Datagram sockets use UDP and all the TCP,UDP stuff, packets arriving in order, ACK, NACK etc. I understand the importance of these over internet.

Q1) 当我创建一个本地套接字的 UNIX 域套接字时,该套接字是 STREAM 套接字还是 DATAGRAM 套接字有什么关系.这种类型的套接字会将数据写入套接字文件,在这种情况下协议是否重要,因为我没有通过网络传输数据?如果我使用基于 UNIX 的 DATAGRAM 套接字,在这种情况下是否有可能丢失数据?

Q2) UNIX DATAGRAM 套接字是否提供比 UNIX STREAM 套接字更好的性能?

Q3) 如何在我的应用程序中决定使用基于 STREAM/DATAGRAM UNIX 的套接字?

Q1) When I create a UNIX domain socket which is a local socket, how would it matter if the socket is STREAM socket or DATAGRAM socket. This type of socket would write the data to the socket file, would the protocol matter in this case since I am not transmitting data over a network? Is there any chance of data loss in this case if I use UNIX-based DATAGRAM sockets?

Q2) Does UNIX DATAGRAM sockets provide better performance than UNIX STREAM sockets?

Q3) How to decide for a STREAM/DATAGRAM UNIX based socket in my application?


谢谢

正确答案

#1

就像手册页 说 Unix 套接字总是可靠的.SOCK_STREAMSOCK_DGRAM 的区别在于从套接字中消费数据的语义.

Just as the manual page says Unix sockets are always reliable. The difference between SOCK_STREAM and SOCK_DGRAM is in the semantics of consuming data out of the socket.

流套接字允许读取任意数量的字节,但仍保留字节序列.换句话说,发送者可能会向套接字写入 4K 的数据,而接收者可以逐字节地使用该数据.反之亦然——发送方可以将几条小消息写入套接字,接收方可以在一次读取中使用这些消息.流套接字不保留消息边界.

Stream socket allows for reading arbitrary number of bytes, but still preserving byte sequence. In other words, a sender might write 4K of data to the socket, and the receiver can consume that data byte by byte. The other way around is true too - sender can write several small messages to the socket that the receiver can consume in one read. Stream socket does not preserve message boundaries.

另一方面,数据报套接字确实保留了这些边界 - 发送方的一次写入始终对应于接收方的一次读取(即使接收方的缓冲区已分配给 read(2)recv(2) 比那个消息小).

Datagram socket, on the other hand, does preserve these boundaries - one write by the sender always corresponds to one read by the receiver (even if receiver's buffer given to read(2) or recv(2) is smaller then that message).

因此,如果您的应用程序协议包含具有已知消息大小上限的小消息,则最好使用 SOCK_DGRAM,因为这样更易于管理.

So if your application protocol has small messages with known upper bound on message size you are better off with SOCK_DGRAM since that's easier to manage.

如果您的协议需要任意长的消息负载,或者只是一个非结构化流(如原始音频或其他内容),则选择 SOCK_STREAM 并执行所需的缓冲.

If your protocol calls for arbitrary long message payloads, or is just an unstructured stream (like raw audio or something), then pick SOCK_STREAM and do the required buffering.

性能应该是相同的,因为这两种类型都只是通过本地内核内存,只是缓冲区管理不同.

Performance should be the same since both types just go through local in-kernel memory, just the buffer management is different.

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcfgiji
系列文章
更多 icon
同类精品
更多 icon
继续加载