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

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

问题描述

对于STREAM类型和DATAGRAM类型的INTERNET套接字之间的区别,这个问题不是 NOT .我知道STREAM套接字使用TCP,Datagram套接字使用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?


谢谢

推荐答案

就像

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.

Stream套接字允许读取任意数量的字节,但仍保留字节序列.换句话说,发送方可能将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.

数据报套接字确实保留了这些边界-发送方的写操作始终与接收方的读操作相对应(即使将接收方的缓冲区分配给 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天全站免登陆