简体   繁体   中英

Java UDP Receiver not receiving data

Here is my Sender program

import java.net.*;

class Send{
    public static void main(String[] args) {
        try{
            //setup
            DatagramSocket socket=new DatagramSocket();
            byte[] buffer=new byte[100];
            InetAddress address=InetAddress.getLocalHost();
            System.out.println("Address:"+address);
            DatagramPacket packet=new DatagramPacket(buffer,buffer.length,address,10000);

            //get data
            String data="Hello";

            //send data
            buffer=data.getBytes();
            System.out.println("Sending data");
            socket.send(packet);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }
}

Here is the Receiver Program

import java.net.*;

class Listen{
    public static void main(String[] args) {
        try{
            //set up
            DatagramSocket socket=new DatagramSocket(10000);
            byte[] buffer=new byte[100];
            DatagramPacket packet=new DatagramPacket(buffer,buffer.length);
            //recieve
            System.out.println("Started Listening");
            socket.receive(packet);
            //print
            String receivedData=new String(packet.getData());
            System.out.println(receivedData);               
            System.out.println("Done!");
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }

    }
}

What I get is, when the Listener is running, it seems to be waiting for the sender to send data(prints "started Listening"). When sender is executed, it seems to run properly as well(prints "Sending data"). receiver executes further as well(prints "Done!"), but the packet remains empty.

What might be the problem?

The problem is that you never put anything in the packet. First you create an empty packet, then you fill a buffer with data - but that buffer isn't used in the packet.

Reverse the order of things:

        String data="Hello";
        //get data
        // HERE : first put data in the buffer, THEN build the packet
        byte[] buffer=data.getBytes();

        InetAddress address=InetAddress.getLocalHost();
        System.out.println("Address:"+address);
        DatagramPacket packet=new DatagramPacket(buffer,buffer.length,address,10000);

        //send data
        System.out.println("Sending data");
        socket.send(packet);

As Jon Skeet mentioned, you also need to worry about character encodings on both ends. If you don't specify an encoding when you do String.getBytes() or new String(byte[]) on the receiving side, then you will use the default character encoding on your computer. Which is fine if you're on the same computer, but can create a lot of problems if the sender and receiver are on different computers and the computers have different platform default character encodings.

So you need to specify the encoding, and your best best is probably UTF8, if most of the text you send is using the Latin script.

So on the sending side:

byte[] buffer = data.getBytes(StandardCharsets.UTF_8);

And on the receiving side:

String receivedData = new String(packet.getData(), StandardCharsets.UTF_8);

You are creating the data packet before filling the buffer with your data, hence you're sending an empty buffer.

Change the order of the lines as follows and your program works as expected:

//get data
String data="Hello";
buffer=data.getBytes();
DatagramPacket packet=new DatagramPacket(buffer,buffer.length,address,10000);

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