mjr
mjr

Reputation: 97

Java UDP packet only holds 5 bites

The following code is used to send over a simple string in the following formart "address,porttNum".

Here is the client side:

ByteArrayInputStream bin = new ByteArrayInputStream(packet.getData());
DataInputStream dis = new DataInputStream(bin);

try {
            System.out.println("Data in packet: " + dis.readLine());
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

Here is the server side:

byte[] sbuf = data.getBytes();
        // sbuf = output.getBytes();
        packet = new DatagramPacket(sbuf,
                sbuf.length, packet.getAddress(), packet.getPort());
try { 
            socket = new DatagramSocket();
            socket.send(packet);
        } catch (Exception e) {
            e.printStackTrace();
        }

Say the server sends "abcdefghi", the client only recieves "abcde". I have tried it with multiple test cases and the client always recieves 5 bytes. Can anyone point out where I messed up?

edit: For debugging purposes I even added the following:

try {
            System.out.println("Data in packet: " + dis.readLine());
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

which outputs the right data the client still does not get it.

edit 2: I changed the client side to the following:

String data = new String(packet.getData(), StandardCharsets.UTF_8);
        System.out.println("Data in packet: " + data);

It makes no difference.

Upvotes: 0

Views: 86

Answers (1)

AJNeufeld
AJNeufeld

Reputation: 8695

Here is some example code showing construction of a Datagram packet from the bytes of a String, sending it, and reconstruction of the String.

Notice that the buffer for receiving a packet is created much larger than the message that is eventually received. This is necessary as the the UDP socket will truncate the message to the size of the buffer if it receives a message larger than the buffer. For example, if the client sent the message "ZYXWV" to the server, and only created the buffer large enough for that message, and then reused the buffer for the incoming message, only the first 5 characters of the incoming message would be received.

public class DataGram {

    public static void main(String[] args) throws SocketException {
        new Thread(new Client()).start();
        new Thread(new Server()).start();
    }

    static class Client implements Runnable {
        DatagramSocket socket;

        Client() throws SocketException {
            socket = new DatagramSocket(1234);
        }

        @Override
        public void run() {
            byte[] buf = new byte[1024];
            DatagramPacket packet = new DatagramPacket(buf, buf.length);
            try {
                try {
                    socket.receive(packet);
                    String msg = new String(packet.getData());
                    System.out.println(msg);
                } finally {
                    socket.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    static class Server implements Runnable {

        @Override
        public void run() {
            SocketAddress address = new InetSocketAddress("localhost", 1234);
            try (DatagramSocket socket = new DatagramSocket()) {
                String msg = "abcdefghi";
                byte[] buf = msg.getBytes();
                DatagramPacket packet = new DatagramPacket(buf, buf.length, address);
                socket.send(packet);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

No DataInputStream or ByteArrayInputStream is used here, since you can directly convert a String to byte[] and back again.

Upvotes: 1

Related Questions