TCP/IP套接字是原子的吗? [英] Are TCP/IP Sockets Atomic?

查看:116
本文介绍了TCP/IP套接字是原子的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,如果写入的数据量很小,则对TCP/IP套接字的写入将是原子的.原子的意思是接收器将接收所有数据或不接收任何数据.但是,如果写入的数据量很大,则不是原子的.我对么?如果是这样,什么才算大?

It is my understanding that a write to a TCP/IP socket will be atomic if the amount of data written is small. By atomic, I mean that the receiver will receive all of the data or none of the data. However, it is not atomic, if the amount of the data written is large. Am I correct? and if so, what counts as large?

谢谢, 鲍勃

推荐答案

对于UDP,这是正确的,因为该应用编写的所有数据都在一个UDP数据报中发送.

For UDP, that is true, because all data written by the app is sent out in one UDP datagram.

对于TCP,情况并非如此,除非应用程序一次仅发送1个字节的数据.写入TCP套接字会将所有数据写入与该套接字关联的缓冲区.然后,TCP将在后台从该缓冲区读取数据并将其发送到接收器. TCP在一个TCP段中实际发送多少数据取决于其流控制机制的变量以及其他因素,包括:

For TCP, that is not true, unless the application sends only 1 byte of data at a time. A write to a TCP socket will write all of the data to a buffer that is associated with that socket. TCP will then read data from that buffer in the background and send it to the receiver. How much data TCP actually sends in one TCP segment depends on variables of its flow control mechanisms, and other factors, including:

  • 其他节点(接收者)发布的接收窗口
  • 在飞行中的先前航段中发送的尚未确认的数据量
  • 启动缓慢和拥塞避免算法状态
  • 协商的最大段大小(MSS)

在TCP中,您永远不能假定应用程序写到套接字的内容实际上是在接收方一次读取的情况下接收到的.套接字缓冲区中的数据可以在一个或多个TCP段中发送到接收器.在数据可用的任何时刻,接收器都可以执行套接字读取并返回当时实际可用的任何数据.

In TCP, you can never assume what the application writes to a socket is actually received in one read by the receiver. Data in the socket's buffer can be sent to the receiver in one or many TCP segments. At any moment when data is made available, the receiver can perform a socket read and return with whatever data is actually available at that moment.

当然,所有发送的数据最终都将到达接收器,如果中间没有故障阻止接收器,并且接收器在数据到达之前没有关闭连接或停止读取.

Of course, all sent data will eventually reach the receiver, if there is no failure in the middle preventing that, and if the receiver does not close the connection or stop reading before the data arrives.

这篇关于TCP/IP套接字是原子的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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