【发布时间】:2014-12-12 13:30:05
【问题描述】:
我的程序有一个客户端/服务器,服务器使用 UDP 从网络摄像头向客户端发送实时图像流。服务器在我的工作中,而客户端可以从任何地方在线访问。 图像的发送和接收工作得非常好——有时。我注意到,根据客户所在的位置,他们可能无法接收图像。我有几个不同地方的人访问在线客户端,其中一些人可以完美接收图像,而其他人则无法接收任何图像。
那些不能接收图像的,但是可以使用相同的端口从服务器接收消息。
确保计算机没有问题。我在工作中用笔记本电脑测试了客户端,并且能够接收图像。但是,当我连接到我的家庭网络(使用同一台笔记本电脑)时,我不能。我只能认为这个问题与不同的网络有关,因为这是唯一改变的地方。
发送图片代码:
public void sendImage()
{
// get image as bytes for UDP communication
ByteArrayOutputStream baStream =null;
try {
//compresses image file and returns a ByteArrayOutputStream
baStream =compress(cap.getOneFrame(),0.1f);
} catch (IOException e1) {
e1.printStackTrace();
}
//byte array to send via udp
packet = baStream.toByteArray();
try {
sendPacket=(new DatagramPacket(packet,packet.length,IPAddress,port));
System.out.println(sendPacket.getLength());
serverSocket.send(sendPacket);
}
catch (Exception e) {
e.printStackTrace();
}
}
检索图像代码:
public void receiveImage()
{
//receive the incoming packet
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
try {
clientSocket.receive(receivePacket);
}
catch (IOException e) {
e.printStackTrace();
}
//retrieve the data from the packet
byte[] data = receivePacket.getData();
// Read incoming data into a ByteArrayInputStream
ByteArrayInputStream bais = new ByteArrayInputStream( data );
try
{
//convert to buffered image
BufferedImage img = ImageIO.read(bais);
if (img != null)
{
gui.getBottomCamPanel().setImage(img);
gui.getBottomCamPanel().repaint();
}
} catch (IOException e) {
e.printStackTrace();
}
}
【问题讨论】:
-
也许他们在不可靠的网络上,并且一些 UDP 数据包丢失或无序传送?
-
可能,但我的家庭连接非常可靠(很少会出现限制)。路由器/ISP 是否可以阻止大于特定大小的 UDP 数据包?
-
不是 ISP 选择按大小阻止,而是底层传输技术将施加固有的 MTU 大小限制(对于通过 VPN 的数据包,整体大小会因包装器开销和技术例如 DSL 的 PPPoE 的 MTU 可能比您的 LAN 低)。您正在处理的数据包有多大?
-
嗯,我明白了。它们平均为 6000-7000 字节。远低于 65536 的限制
-
好吧,那是你的问题。对于任意 Internet 连接,您不能期望超过 1400 字节的可用 MTU。