热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

UNIX域流和数据报套接字之间的区别?-DifferencebetweenUNIXdomainSTREAMandDATAGRAMsockets?

ThisquestionisNOTforthedifferencebetweenSTREAMtypeandDATAGRAMtypeINTERNETsockets.Ikn

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.

这个问题不是关于流类型和数据报类型INTERNET套接字之间的区别。我知道流套接字使用TCP,数据报套接字使用UDP和所有TCP、UDP之类的东西,按顺序到达的包,ACK, NACK等等。我理解这些在internet上的重要性。

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域套接字(即本地套接字)时,如果套接字是流套接字或数据报套接字,那又有什么关系呢?这种类型的套接字将数据写入套接字文件,在这种情况下,协议是否重要,因为我不通过网络传输数据?在这种情况下,如果我使用基于unix的数据报套接字,是否有可能丢失数据?UNIX数据报套接字是否比UNIX流套接字提供更好的性能?Q3)如何在我的应用程序中确定基于流/数据报UNIX的套接字?


Thanks

谢谢

4 个解决方案

#1


50  

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.

正如手册页所说,Unix套接字总是可靠的。SOCK_STREAM和SOCK_DGRAM之间的区别在于从套接字中使用数据的语义。

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.

流套接字允许读取任意数量的字节,但仍然保留字节序列。换句话说,发送方可能向套接字写入4K数据,而接收方可以逐个字节地使用该数据。另一种方法也是正确的——发送方可以向套接字写入几个小消息,接收方可以在一次读取中使用这些消息。流套接字不保留消息边界。

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).

而另一方面,数据报套接字则保留了这些边界——发送方的写入总是对应于接收方的读取(即使接收方给读(2)或recv(2)的缓冲区小于该消息)。

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_DGRAM,因为这样更容易管理。

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.

如果您的协议调用任意长的消息有效负载,或者只是一个非结构化的流(如原始音频或其他),那么选择SOCK_STREAM并进行所需的缓冲。

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

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

#2


16  

The main difference is that one is connection based (STREAM) and the other is connection-less (DGRAM) - the difference between stream and packet oriented communication is usually much less important.

主要的区别是一个是基于连接的(流),另一个是无连接的(DGRAM)——流和面向包的通信之间的区别通常不那么重要。

With SOCK_STREAM you still get all the connection handling, i.e. listen/accept and you can tell if a connection is closed by the other side.

使用SOCK_STREAM,您仍然可以获得所有的连接处理,例如监听/接收,并可以判断另一端是否关闭了连接。

Note that there is also a SEQPACKET socket type that's still connection oriented, but preserves message boundaries (which might save you from implementing a message-oriented layer on top of a STREAM socket).

注意,还有一个SEQPACKET套接字类型仍然是面向连接的,但是保留了消息边界(这可能会使您避免在流套接字之上实现面向消息的层)。

I would expect data transfer performance to be similar for all of these types, the main difference is just what semantics you want.

我希望所有这些类型的数据传输性能都是相似的,主要的区别在于您需要什么样的语义。

#3


3  

  1. One likely difference are message boundaries. Datagrams will be delivered as a whole with the datagrams being the natural message boundaries. With stream sockets you can read N bytes and the socket will block until N bytes are ready. But this means no obvious message boundaries.
  2. 一个可能的区别是消息边界。数据报将作为一个整体交付,而数据报是自然的消息边界。使用流套接字,您可以读取N个字节,套接字将阻塞,直到N个字节就绪。但这意味着没有明显的信息界限。
  3. Maybe. A stream socket with TCP at least needs the initial three way handshake to establish the connection. A UDP socket does not.
  4. 也许吧。带有TCP的流套接字至少需要最初的三种方式握手来建立连接。UDP套接字没有。
  5. All things being equal, if speed is a concern, instrument and measure. (I assume you already know that only a TCP stream socket provides built-in reliable in-order transport, and only datagram sockets can be used to send to multiple receivers).
  6. 所有的事物都是平等的,如果速度是一个关注,工具和测量。(我假设您已经知道,只有TCP流套接字提供内置的可靠的按顺序传输,并且只有数据报套接字可以用于发送到多个接收器)。

#4


0  

If the clients and servers will always be on the same machine and the goal is to have minimal latency and maximum bandwidth, use shared memory.

如果客户端和服务器总是在同一台机器上,并且目标是使延迟和最大带宽最小化,那么使用共享内存。


推荐阅读
  • 域名解析系统DNS
    文章目录前言一、域名系统概述二、因特网的域名结构三、域名服务器1.根域名服务器2.顶级域名服务器(TLD,top-leveldomain)3.权威(Authoritative)域名 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • IB 物理真题解析:比潜热、理想气体的应用
    本文是对2017年IB物理试卷paper 2中一道涉及比潜热、理想气体和功率的大题进行解析。题目涉及液氧蒸发成氧气的过程,讲解了液氧和氧气分子的结构以及蒸发后分子之间的作用力变化。同时,文章也给出了解题技巧,建议根据得分点的数量来合理分配答题时间。最后,文章提供了答案解析,标注了每个得分点的位置。 ... [详细]
  • 初探PLC 的ST 语言转换成C++ 的方法
    自动控制软件绕不开ST(StructureText)语言。它是IEC61131-3标准中唯一的一个高级语言。目前,大多数PLC产品支持ST ... [详细]
  • 概述H.323是由ITU制定的通信控制协议,用于在分组交换网中提供多媒体业务。呼叫控制是其中的重要组成部分,它可用来建立点到点的媒体会话和多点间媒体会议 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • Html5-Canvas实现简易的抽奖转盘效果
    本文介绍了如何使用Html5和Canvas标签来实现简易的抽奖转盘效果,同时使用了jQueryRotate.js旋转插件。文章中给出了主要的html和css代码,并展示了实现的基本效果。 ... [详细]
  • position属性absolute与relative的区别和用法详解
    本文详细解读了CSS中的position属性absolute和relative的区别和用法。通过解释绝对定位和相对定位的含义,以及配合TOP、RIGHT、BOTTOM、LEFT进行定位的方式,说明了它们的特性和能够实现的效果。同时指出了在网页居中时使用Absolute可能会出错的原因,即以浏览器左上角为原始点进行定位,不会随着分辨率的变化而变化位置。最后总结了一些使用这两个属性的技巧。 ... [详细]
  • 【shell】网络处理:判断IP是否在网段、两个ip是否同网段、IP地址范围、网段包含关系
    本文介绍了使用shell脚本判断IP是否在同一网段、判断IP地址是否在某个范围内、计算IP地址范围、判断网段之间的包含关系的方法和原理。通过对IP和掩码进行与计算,可以判断两个IP是否在同一网段。同时,还提供了一段用于验证IP地址的正则表达式和判断特殊IP地址的方法。 ... [详细]
  • 本文介绍了绕过WAF的XSS检测机制的方法,包括确定payload结构、测试和混淆。同时提出了一种构建XSS payload的方法,该payload与安全机制使用的正则表达式不匹配。通过清理用户输入、转义输出、使用文档对象模型(DOM)接收器和源、实施适当的跨域资源共享(CORS)策略和其他安全策略,可以有效阻止XSS漏洞。但是,WAF或自定义过滤器仍然被广泛使用来增加安全性。本文的方法可以绕过这种安全机制,构建与正则表达式不匹配的XSS payload。 ... [详细]
  • 合并列值-合并为一列问题需求:createtabletab(Aint,Bint,Cint)inserttabselect1,2,3unionallsel ... [详细]
  • Android实战——jsoup实现网络爬虫,糗事百科项目的起步
    本文介绍了Android实战中使用jsoup实现网络爬虫的方法,以糗事百科项目为例。对于初学者来说,数据源的缺乏是做项目的最大烦恼之一。本文讲述了如何使用网络爬虫获取数据,并以糗事百科作为练手项目。同时,提到了使用jsoup需要结合前端基础知识,以及如果学过JS的话可以更轻松地使用该框架。 ... [详细]
  • Hibernate延迟加载深入分析-集合属性的延迟加载策略
    本文深入分析了Hibernate延迟加载的机制,特别是集合属性的延迟加载策略。通过延迟加载,可以降低系统的内存开销,提高Hibernate的运行性能。对于集合属性,推荐使用延迟加载策略,即在系统需要使用集合属性时才从数据库装载关联的数据,避免一次加载所有集合属性导致性能下降。 ... [详细]
  • 本文整理了Java中org.gwtbootstrap3.client.ui.Icon.addDomHandler()方法的一些代码示例,展示了Icon.ad ... [详细]
  • iwanttowriteasimpleAppforme(Android).Forthisitisnecessarythattheservercansenddat ... [详细]
author-avatar
倒颠0
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有