我的程序有一个客户机/服务器,其中服务器使用UDP向客户机发送从网络摄像头到客户端的实时映像流。服务器在我的工作中,而客户端可以从任何地方在线访问。发送和接收图像非常好--有时。我注意到,取决于客户端的位置,他们可能接收图像,也可能无法接收图像。我让来自几个不同地点的几个人访问了这个在线客户端,其中一些人可以完美地接收图像,而其他人则无法接收任何图像。
但是,无法接收图像的用户可以使用相同的端口接收来自服务器的消息。
以确保电脑不出问题。我在工作时用笔记本电脑对客户进行了测试,并且能够接收图像。然而,当我连接到我的家庭网络(用同样的笔记本电脑),我不能。我只能认为这个问题与不同的网络有关,因为这是唯一的改变。
SendImage代码:
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();
}
}发布于 2014-12-12 05:30:05
是,它取决于网络的类型。更具体地说,不同网络中使用的不同网络地址转换(NAT)方案导致了这种情况。客户端不接收映像的原因是防火墙阻塞了UDP数据包。
路由器的防火墙允许发送UDP数据包,但默认情况下阻止传入的UDP数据包。它只允许来自较早发送UDP数据包的源的传入数据包,并在其路由表中创建映射。这是一个针对不同类型NAT的典型过程。
根据客户端连接到的路由器所使用的NAT类型,映射 of 内部IP和端口(您的系统正在使用的端口)到外部IP和端口(路由器分配用于连接internet的NAT)是不同的。
您需要了解不同类型的NAPT(或NAPT)以及如何创建这些映射。
没有通用的解决方案来解决这个问题。在某种程度上,UDP孔穿孔解决了这个问题,但是也不适用于对称NAT。
阅读更多关于NAT计划的信息。其中一些消息来源是:
NAT
NAT类型
https://stackoverflow.com/questions/27431176
复制相似问题