无法通过 udp 发送大图像文件
我正在尝试使用 JAVA 通过 UDP 发送图像。我已经成功实现了发送器和接收器,它适用于小图像(在本例中为 18KB)。当我尝试发送较大的图像(2MB)时,接收器在发送数百个数据报后似乎出现堵塞。
这是我的接收器中的循环,它从发送器接收每个数据包。
while(true) {
packetCount++;
System.out.println("PKT: " + packetCount);
receievePacket = new DatagramPacket(recieveData, recieveData.length);
try {
receieveSocket.receive(receievePacket);
} catch (IOException e) {
System.out.println("Error receieving packet");
e.printStackTrace();
}
bos.write(recieveData, 0, (int) DATAGRAM_MAX_SIZE);
// Terminate loop if last packet received
if (packetCount == packetNum) {
break;
}
}
此循环之前的代码仅从用户接收端口号,设置套接字并从发送方接收单个数据包,该数据包指定将发送多少个数据包。这存储在 packetNum 中。
谁能想到在尝试发送更大图像时它停滞的原因吗? 谢谢
I am trying to send an image over UDP using JAVA. I have managed to implement the sender and receiver and it works for a small image (18KB in this case). When I try to send a larger image (2MB) the receiver seems to jam after sending a few hundred datagrams.
Here is the loop in my receiver which recieves each packet from the sender.
while(true) {
packetCount++;
System.out.println("PKT: " + packetCount);
receievePacket = new DatagramPacket(recieveData, recieveData.length);
try {
receieveSocket.receive(receievePacket);
} catch (IOException e) {
System.out.println("Error receieving packet");
e.printStackTrace();
}
bos.write(recieveData, 0, (int) DATAGRAM_MAX_SIZE);
// Terminate loop if last packet received
if (packetCount == packetNum) {
break;
}
}
The code before this loop just receives the port number from the user, sets up the socket and recieves a single packet from the sender which specifies how many packets will be sent. This is stored in packetNum.
Can anyone think of a reason why it stalls when trying to send larger images?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
UDP 只能处理每个数据包 64kB。您需要将数据分段并给它们编号,因为不能保证它们按顺序到达。
但实际上,除非有特殊原因,否则您应该切换到 TCP。它可以帮助您解决遇到的问题以及一些您尚未遇到的问题:)
UDP can only handle 64kB in each packet. You need to fragment the data into blocks and give them numbers, since they are not guaranteed to arrive in order.
But really, you should switch to TCP unless you have some special reason. It helps you with the problems you have, and some you have not yet seen :)
您的意思是“接收器在接收几百个数据报后似乎卡住了”,而不是发送,对吗?如果是这种情况,那么您可能是臭名昭著的 UDP 数据包丢失的受害者! :) 有两个选择:
第一个更容易做到,它应该可以让您快速确定是否遇到数据包丢失。运行一些测试,看看通过改变传输速率(即在发送每个数据包之间放置一个小睡眠)是否接收到相同数量的字节。如果您检测到数据包丢失的差异,则实施可靠的 UDP 并请求重新传输失序或丢失的数据包。
You mean "receiver seems to jam after receiving a few hundred datagrams," not sending, right? If that's the case, then you may possibly be the victim of the infamous UDP packet loss! :) There are two options:
The first one is easier to do and it should allow you to quickly determine if you're experiencing packet loss. Run a few tests and see if you're receiving the same number of bytes by varying the transmission rates (i.e. put a small sleep between sending out each packet). If you detect a difference in packet loss, then implement reliable UDP and request the re-transmission of out-of-sequence or missing packets.