UNIX 域 STREAM 和 DATAGRAM 套接字之间的区别? [英] Difference between UNIX domain STREAM and DATAGRAM sockets?

查看:18
本文介绍了UNIX 域 STREAM 和 DATAGRAM 套接字之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题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?


谢谢

推荐答案

就像手册页 说 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.

这篇关于UNIX 域 STREAM 和 DATAGRAM 套接字之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆