简体   繁体   中英

java udp packet merging

I've got a strange problem while practising with UDP in Java. If I send multiple UDP handshake packets of different data lengths between servers, the UDP packets merge. I'm using a timer to send the handshakes between the servers.

public class UDPtest {

public static UDPtest test = new UDPtest();
public static byte[] buffer = new byte[1024];
public static InetAddress host = null;
Timer timer = new Timer();
TimerTask ttimer = new TimerTask() {
    public void run() {
    //System.out.println("time");
    }   
};

public static void main(String args[]) throws Exception {
    DatagramSocket sendSocket = new DatagramSocket();
    DatagramSocket receiveSocket = new DatagramSocket(Integer.parseInt(args[1]));
    String s = "SEND "+args[0];
    host = InetAddress.getByName("localhost");
    buffer = s.getBytes();
    DatagramPacket handshake = new DatagramPacket(buffer, buffer.length, host, Integer.parseInt(args[2])); 
    test.RouteTimer(receiveSocket,handshake);


while (true) {
        DatagramPacket receivePacket = new DatagramPacket(buffer,buffer.length);
        receiveSocket.receive(receivePacket);
        String receiveString = new String(receivePacket.getData());

        if (receiveString.substring(0,1).matches("A")) {

            System.out.println(receiveString);
            continue;

        }
        else {

            System.out.println(receiveString);
            String temp = "A "+args[0];
            buffer = temp.getBytes();
            DatagramPacket sendPacket = new DatagramPacket(buffer, buffer.length, host, Integer.parseInt(args[2]));
            sendSocket.send(sendPacket);
        }
    }

}

public void RouteTimer(final DatagramSocket socket, final DatagramPacket handshake) {
    Timer timer = new Timer();
    TimerTask ttimer = new TimerTask() {
        @Override
        public void run() {
                    try {
                    socket.send(handshake);         
                    }
                    catch (Exception e) {

                    }
        }
    };

timer.scheduleAtFixedRate(ttimer, 1000, 5000);
}

}

For example: If I have server X and Y, the expected output for server X would be:

SEND X

AX

SEND X

AX

but the output becomes:

SEND Y

AY

SEN

AY

I'm trying to understand why this happens, and how to fix the problem. Any help would be appreciated.

If I send multiple UDP handshake packets of different data lengths between servers, the UDP packets merge.

No they don't.

String receiveString = new String(receivePacket.getData());

Your problem is here. You are ignoring the length of the packet. It should be:

String receiveString = new String(receivePacket.getData(), receivePacket.getOffset(), receivePacket.getLength());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM