Max Segal
Max Segal

Reputation: 2045

Can't receive correct message from UDP server in java

I'm trying to send a message using a udp client to udp server and send the message back to client with various metadata about the received message. I have these two classes:

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

public class UdpDateClient {
    public static void main(String[] args) throws IOException {
        String host = "localhost";
        if (args.length > 0)
            host = args[0];
        // get a datagram socket on any available port
        try {
            DatagramSocket socket = new DatagramSocket();
            // send request
            byte[] buf = new byte[2048];
            buf="hi it's Max".getBytes();
            InetAddress address = InetAddress.getByName(host);
            DatagramPacket packet = new DatagramPacket(buf, buf.length, address, 4445);
            socket.send(packet);
            // get response
            packet = new DatagramPacket(buf, buf.length);
            socket.receive(packet);
            // display response
            buf = packet.getData();
            int len = packet.getLength();
            String received = (new String(buf)).substring(0, len);
            System.out.println("From server: " + received);
            socket.close();
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Date;

public class UdpDateServer {
    private DatagramSocket socket = null;
    private boolean moreClients = true;

    public static void main(String[] args) {
        UdpDateServer server = new UdpDateServer();
        server.start();
    }

    public UdpDateServer() {
        try {
            socket = new DatagramSocket(4445);
            System.out.println("server ready...");
        } catch (SocketException e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    public void start() {
        DatagramPacket packet;
        while (moreClients) {
            try {
                byte[] buf = new byte[2048];
                // receive request
                packet = new DatagramPacket(buf, buf.length);
                socket.receive(packet);
                System.out.println("recieved packet from client: "+new String(packet.getData()));
                // prepare response
                String responseString = "Client's message received: "+new String(packet.getData())+"\n"
                        + "Received on:" +new Date().toString();

                buf = responseString.getBytes();
                System.out.println("buf to be sent from server: "+(new String(buf)));
                System.out.println("buf.length="+buf.length);
                // send the response to "address" and "port"
                InetAddress address = packet.getAddress();
                int port = packet.getPort();
                packet = new DatagramPacket(buf, buf.length, address, port);
                socket.send(packet);
            } catch (IOException e) {
                e.printStackTrace();
                moreClients = false;
            }
        }
        socket.close();
    }
}

what I get on the client side is only first 11 bytes of the message that is sent by the server:

Client side:

From server: Client's me

Server side:

server ready...

recieved packet from client: hi it's Max

buf to be sent from server: Client's message received: hi it's Max

Received on:Fri Aug 14 16:00:20 IDT 2015

buf.length=2116

As you can see, the message from server that is recieved back by the client is cut after 11 characters.

What am I doing wrong?

Upvotes: 1

Views: 1773

Answers (2)

Ben Win
Ben Win

Reputation: 840

byte[] buf = new byte[2048];
buf="hi it's Max".getBytes();

after this buf.length will be 11 not 2048!

And on your way back

packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);

will only receive 11 Bytes because buf.length is 11. Change the first block to something like

byte[] buf = new byte[2048];
System.arraycopy("hi it's Max".getBytes(), 0, buf, 0, 11);

Upvotes: 1

itsme
itsme

Reputation: 852

The problem is the line buf="hi it's Max".getBytes(); of the client code. Here you set the buffer length to 11. Your client code should look like the following

            DatagramSocket socket = new DatagramSocket();
            // send request
            byte[] buf = "hi it's Max".getBytes();
            InetAddress address = InetAddress.getByName(host);
            DatagramPacket packet = new DatagramPacket(buf, buf.length, address, 4445);
            socket.send(packet);
            // get response
            buf = new byte[2048];
            packet = new DatagramPacket(buf, buf.length);
            socket.receive(packet);
            // display response
            buf = packet.getData();
            int len = packet.getLength();
            String received = (new String(buf)).substring(0, len);
            System.out.println("From server: " + received);
            socket.close();

Upvotes: 1

Related Questions