简体   繁体   English

Java中的UDP数据包路由

[英]UDP Packet route in Java

I have a question considering udp packet life/route. 我有一个考虑udp数据包寿命/路由的问题。 I have a simple client server UDP scheme with a send call in the client side and a receive call in the server side. 我有一个简单的客户端服务器UDP方案,在客户端具有发送调用,在服务器端具有接收调用。 Lets say the send method gets called and the packet actually arrives in the other side BUT the server's code execution hasn't yet reached the receive method call. 假设发送方法被调用,而数据包实际上到达了另一端,但是服务器的代码执行尚未到达接收方法调用。 What happens with the packet in that time . 那个时候那个包裹发生了什么。 Now i tried to stop the execution before the receive call with a simple command input prompt , waited a little and then let it continue and noticed that the packet got received . 现在,我尝试通过简单的命令输入提示符在接收调用之前停止执行,稍等片刻,然后继续执行,并注意到已接收到数据包。 Can you explain WHY that happen, like is it buffered on a different OSI level? 您能解释为什么会发生这种情况吗,就像它缓冲在另一个OSI级别上一样吗?

Thanks in advance. 提前致谢。

Every TCP or UDP socket has a send buffer and a receive buffer. 每个TCP或UDP套接字都有一个发送缓冲区和一个接收缓冲区。 Your datagram got queued into the send buffer at the sender, then it got sent, then it got queued into the receive buffer at the receiver, then you read it from there. 您的数据报在发送器处排队进入发送缓冲区,然后被发送,然后在接收器处排队进入接收缓冲区,然后从那里读取数据。

NB has nothing to do with it. NB 与它无关。 TCP/IP doesn't obey the OSI model. TCP / IP不遵循OSI模型。 It has its own, prior model. 它有自己的先前模型。

The "receive" method call doesn't receive the packet. “ receive”方法调用未接收到数据包。 If there's a UDP socket "open" for that port, it means that there is buffer space allocated, and that's where the NIC+OS put the data. 如果该端口有一个UDP套接字“打开”,则表示已分配了缓冲区空间,这是NIC + OS放置数据的地方。 When you call "receive", it just looks there, and if there's anything there, then it pretends to have just received it. 当您将其称为“接收”时,它只会出现在那里,如果那里有任何东西,则它会假装刚刚收到它。

I should add that if the buffer is empty, then the receive call does go into a blocking state, waiting to get notified by the OS that something has arrived. 我应该补充一点,如果缓冲区为空,则接收调用的确进入阻塞状态,等待操作系统通知某物已到达。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM