我目前正在开发一台服务器,该服务器应该将从一个客户端收到的所有UDP数据包重新传输到另一个客户端。
这是我用于重传的代码。
// This class contains the Tx and Rx Threads
public UdpRtxThread(int port, String serverIp) {
this.serverIp = serverIp;
this.port = port;
try {
socketUDP = new DatagramSocket(port);
socketUDP.setSoTimeout(300);
socketUDP.setReceiveBufferSize(bufferSize);
} catch (SocketException e2) {
e2.printStackTrace();
return;
}
urx = new UDPRx(socketUDP);
utx = new UDPTx(socketUDP, this.serverIp, this.port);
}
private class UDPTx extends Thread {
private DatagramSocket ds;
private boolean working = true;
private String targetIp;
private int targetPort;
// private DatagramPacket dp;
public UDPTx(DatagramSocket ds, String targetIp, int targetPort) {
this.ds = ds;
this.targetIp = targetIp;
this.targetPort = targetPort;
this.start();
}
public void closeTxThread() {
this.working = false;
}
while (working) {
if (packetsList.size() > 0) {
try {
random = rand.nextInt(100);
System.out.println("------------------------------------------------------------");
System.out.println("Random value to prove that is the same thread: "+random);
// Obtain a new UDP packet to be retransmitted
DatagramPacket packet = packetsList.get(0);
// Check that the destination port and ip specified by the thread is right
System.out.println("Must be sent to: "+InetAddress
.getByName(targetIp)+":"+targetPort+"----> Check random: "+random);
// Set the destination IP and port
packet.setAddress(InetAddress
.getByName(targetIp));
packet.setPort(targetPort);
// Check the packet destination
System.out.println("Sent to: "+packet.getAddress()+":"+packet.getPort()+"----> Check random: "+random);
// send packet
ds.send(packet);
//Remove the sent packet from the list
packetsList.remove(0);
System.out.println("------------------------------------------------------------");
} catch (IOException e1) {
e1.printStackTrace();
}
}
else{
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
System.out.println("UDP Tx Thread stopped");
}
}
packetsList(ArrayList< DatagramPacket> packetsList)是包含在接收线程中接收的所有DatagramPackets的列表。一旦接收到分组,就将其发送到重传线程并列在packetsList中。在传输线程中,如果packetsList包含一些必须重传的数据包,它将更改数据包ipaddress和数据包端口,然后将其发送到网络。
由于某些我无法理解的原因,此过程会不断将数据包发送到其原始数据源,甚至更改目标IP地址和端口。
这里我提供了服务器中获得的一些日志:
随机值证明是同一个线程:79
必须发送至:/172.16.112.0:12349---->随机检查:79
发送至:/172.16.102.95:12352---->随机检查:79
随机值证明是同一个线程:43
必须发送至:/172.16.112.0:12349---->随机检查:43
发送至:/172.16.102.95:12352---->随机检查:43
随机值证明是同一个线程:61
必须发送至:/172.16.112.0:12349---->随机检查:61
发送至:/172.16.102.95:12352---->随机检查:61
随机值证明是同一个线程:72
必须发送至:/172.16.112.0:12349---->随机检查:72
发送至:/172.16.102.95:12352---->随机检查:72
随机值证明是同一个线程:26
必须发送至:/172.16.112.0:12349---->随机检查:26
发送至:/172.16.102.95:12352---->随机检查:26
随机值证明是同一个线程:39
必须发送至:/172.16.112.0:12349---->随机检查:39
发送至:/172.16.102.95:12352---->随机检查:39
随机值证明是同一个线程:64
必须发送至:/172.16.112.0:12349---->随机检查:64
发送至:/172.16.102.95:12352---->随机检查:64
随机值证明是同一个线程:93
必须发送至:/172.16.112.0:12349---->随机检查:93
发送至:/172.16.102.95:12352---->随机检查:93
有人能给我一些关于这里发生的事情的线索吗?
感谢您的时间,感谢抱歉这个杂乱的帖子!