Java UDP图像传输取决于网络
我的程序有一个客户机/服务器,服务器使用UDP从网络摄像头向客户机发送实时图像流。服务器在我的工作中,而客户端可以从任何地方在线访问。 图像的发送和接收有时工作得非常好。我注意到,根据客户所在的位置,他们可能无法接收图像。我让几个来自不同地点的人访问在线客户端,其中几个人可以完美地接收图像,而其他人则无法接收任何图像 但是,无法接收图像的端口可以使用同一端口从服务器接收消息 确保电脑没有问题。我在工作时用我的笔记本电脑测试了客户端,并且能够接收图像。然而,当我用同一台笔记本电脑连接到我的家庭网络时,我就不能了。我只能认为问题与不同的网络有关,因为这是唯一改变的事情 发送图像代码:Java UDP图像传输取决于网络,java,sockets,udp,Java,Sockets,Udp,我的程序有一个客户机/服务器,服务器使用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();
}
}
检索图像代码:
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();
}
}
是的,这取决于网络的类型。更具体地说,不同网络中使用的不同网络地址转换NAT方案会导致这种情况。客户端未接收图像的原因是防火墙正在阻止UDP数据包
路由器的防火墙允许传出UDP数据包,但默认情况下阻止传入UDP数据包。
它只允许来自UDP数据包较早发送且在其路由表中创建映射的源的传入数据包。这是一个典型的过程,不同类型的NAT会有所不同
根据客户端连接到的路由器使用的NAT类型,内部IP和端口的映射(系统使用的是外部IP和端口)与路由器分配用于连接到internet的外部IP和端口的映射是不同的
您需要了解不同类型的NATsor NAPT以及这些映射是如何创建的
没有通用的解决方案来解决这个问题。在某种程度上,UDP打孔解决了这个问题,但对于对称NAT也不起作用
了解更多有关NAT方案的信息。其中一些来源是:
也许他们在不可靠的网络上,一些UDP数据包丢失或传输出现故障?有可能,但我家的连接非常可靠,很少发生阻塞。路由器/ISP是否有可能阻止大于特定大小的UDP数据包?与其说ISP选择按大小进行阻止,不如说基础传输技术将对通过VPN的数据包施加固有的MTU大小限制。总体大小由包装器开销减少,而像PPPoE for DSL这样的技术可能比局域网的MTU更低。你处理的包裹有多大?嗯,我明白了。它们的平均值为6000-7000字节。在65536限制器下,这是你的问题。对于任意internet连接,您不能期望超过1400字节的可用MTU。