tcp底层传输机制/网络编程
我已经搜索过,但找不到以下内容:
Process1 通过 TCP 套接字传输数据。进行传输的代码是(伪代码)
//Section 1
write(sock,data,len);//any language.Just write data
//Section 2
Process1,写入后可以继续第2部分,但这并不意味着数据已经传输。 TCP 可以缓冲数据以供以后传输。
现在 Process2 与 Process1 同时运行。两个进程都尝试同时发送数据。即两者都会有上面的代码。
问题1:如果两个进程同时向 TCP 套接字写入数据,数据最终将如何通过 IP/OS 通过网络传输?
a) Process1 的所有数据后跟 Process2 的所有数据(或相反),即某些 FIFO 顺序?
或
b) 来自 Process1 和 Process1 的数据Process2 将由 IP 层(或操作系统)通过线路进行多路复用,并“同时”发送?
问题2:例如,如果我添加了延迟,我是否可以确定来自2个进程的数据通过线路串行发送(例如,Process1的所有数据后面跟着Process2的所有数据)?
更新:
Process1 和 Process2 不是父子。他们也在不同的套接字上工作
谢谢
I have searched but I could not find the following:
Process1 transmits data over TCP socket. The code that does the transmission is (pseudocode)
//Section 1
write(sock,data,len);//any language.Just write data
//Section 2
Process1 after the write could continue in section 2, but this does not mean that data has been transmitted. TCP could have buffered the data for later transmission.
Now Process2 is running concurrently with Process1. Both processes try to send data concurrently. I.e. both will have code as above.
Question1: If both processes write data to TCP socket simultaneously how will the data be eventually transmitted over the wire by IP/OS?
a) All data of Process1 followed by all data of Process2 (or reverse) i.e. some FIFO order?
or
b) Data from Process1 & Process2 would be multiplexed by IP layer (or OS) over the wire and would be send "concurrently"?
Question2: If e.g. I added a delay, would I be sure that data from the 2 processes were send serially over the wire (e.g. all data of Process1 followed by all data of Process2)?
UPDATE:
Process1 and Process2 are not parent child. Also they are working on different sockets
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
嗯,您是在谈论由两个进程(如父进程和子进程)共享的单个套接字吗?在这种情况下,数据将按照输出系统调用(
write(2)
)的顺序进行缓冲。如果(更有可能的是)您正在谈论两个进程中的两个不相关的 TCP 套接字,则无法保证数据到达线路的任何顺序。原因是套接字可能连接到以不同速度消耗数据的远程点。 TCP流量控制然后确保快速发送方不会压倒慢速接收方。
Hmm, are you are talking about single socket shared by two processes (like parent and child)? In such a case the data will be buffered in order of output system calls (
write(2)
s).If, which is more likely, you are talking about two unrelated TCP sockets in two processes then there's no guarantee of any order in which the data will hit the wire. The reason for that is sockets might be connected to remote points that consume data with different speeds. TCP flow control then makes sure that fast sender does not overwhelm slow receiver.
答案 1:顺序未指定,至少在我见过的支持套接字的操作系统上是这样。流程1& 2 应设计为协作,例如通过在套接字上共享锁/互斥锁。
答案 2:如果您只是指固定时间延迟,则不是。相反,让进程 1 向进程 2 发出继续信号,表明进程 1 已完成发送。使用管道、本地套接字、信号、共享内存或操作系统在进程间通信方面提供的任何内容。仅在“刷新”套接字之后发送信号(实际上并没有冲洗)。
Answer 1: the order is unspecified, at least on the sockets-supporting OS's that I've seen. Processes 1 & 2 should be designed to cooperate, e.g. by sharing a lock/mutex on the socket.
Answer 2: not if you mean just a fixed-time delay. Instead, have process 1 give a go-ahead signal to process 2, indicating that process 1 has done sending. Use pipes, local sockets, signals, shared memory or whatever your operating system provides in terms of interprocess communication. Only send the signal after "flushing" the socket (which isn't actually flushing).
TCP套接字由通常至少为(源IP、源端口、目标IP、目标端口)的元组来标识。不同的套接字有不同的标识元组。
现在,如果您在两个进程上使用相同的套接字,则这取决于 write(2) 调用的顺序。但是,您应该考虑到 write(2) 可能不会消耗您传递给它的所有数据,发送缓冲区可能已满,导致短写入(write()'ing 小于要求,并返回作为返回值写入的字节数),导致 write() 阻塞/睡眠,直到有一些缓冲区空间,或导致 write() 返回 EAGAIN/EWOULDBLOCK 错误(对于非阻塞套接字)。
A TCP socket is identified by a tuple that usually is at least (source IP, source port, destination IP, destination port). Different sockets have different identifying tuples.
Now, if you are using the same socket on two processes, it depends on the order of the write(2) calls. But, you should take into account that write(2) may not consume all the data you've passed to it, the send buffer may be full, causing a short write (write()'ing less than asked for, and returning the number of bytes written as return value), causing write() to block/sleep until there is some buffer space, or causing write() to return an EAGAIN/EWOULDBLOCK error (for non-blocking sockets).
编辑:但如果我现在看到你正在谈论每个进程的不同套接字,你的问题似乎毫无意义。应用程序无法知道 TCP 如何使用网络,所以这有什么关系呢? TCP 将按照其认为合适的顺序以最大 MTU 的数据包进行传输。
EDIT: but if as I now see you are talking about different sockets per process your question seems pointless. There is no way for an application to know how TCP used the network so what does it matter? TCP will transmit in packets of up to an MTU each in whatever order it sees fit.