BeArt
BeArt

Reputation: 15

Java DatagramSocket - client receives only first character

I have a problem with Java UDP connection... The goal of this code is to send to server two adders. The server responses sending the result of the sum.

Client

public class Client{

public static void main(String[] args) throws IOException{
    int add1, add2;
    String URL = "localhost";
    Scanner scan = new Scanner(System.in);
    try {
        InetAddress address = InetAddress.getLocalHost();
        DatagramSocket socket = new DatagramSocket();
        byte[] buf = new byte[256];
        System.out.println("First adder");
        add1 = scan.nextInt();
        buf =  String.valueOf(add1).getBytes();
        DatagramPacket packet = new DatagramPacket(buf, buf.length, address, 8189);
        socket.send(packet);
        System.out.println("Second adder");
        add2 = scan.nextInt();
        buf =  String.valueOf(add2).getBytes();
        packet = new DatagramPacket(buf, buf.length, address, 8189);
        socket.send(packet);
        packet = new DatagramPacket(buf, buf.length);
        socket.receive(packet);
        String response = new String(packet.getData());
        System.out.println(response);
        socket.close();
    } catch (SocketException | UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}}

Server

public class Server {

public static void main(String[] args) {
    DatagramSocket socket = null;
    int add1,add2;
    try {
        socket = new DatagramSocket(8189);
        int n = 1;
        while(n<=10) {
            
        
        byte[] buf = new byte[256];
        DatagramPacket packet = new DatagramPacket(buf,buf.length);
        //ricezione
        socket.receive(packet);
        InetAddress SourceAddress = packet.getAddress();
        int port = packet.getPort();
        add1 = Integer.parseInt(new String(packet.getData()).trim());
        socket.receive(packet);
        add2 = Integer.parseInt(new String(packet.getData()).trim());
        //invio
        int sum = add1+add2;
        String a = "Result "+sum;
        buf = a.getBytes();
        packet = new DatagramPacket(buf,buf.length,SourceAddress,port);
        socket.send(packet);
        n++;
        }
        socket.close();
    }
    catch(IOException e) {
        System.out.println(e);
        socket.close();
    }
}}

The output of this code is just "T". Why?

PS I tried with ByteBuffer.allocate ecc. but it doesn't work...

Upvotes: 0

Views: 40

Answers (1)

user13290159
user13290159

Reputation:

when you send your packet, you trim the byte array down to what .getBytes() returns. since UDP is a minimal protocol, size information isn't sent along, just the bytes themselves, so when you wait for 256 bytes to arrive on the server, you end up waiting forever, since that amount of bytes never arrives; what does arrive are maybe 2-4 bytes of data from the 2 strings you encode and send.

the server ends up waiting until you send it 256 bytes of data in total.

i recommend storing the length of the packet as a 2 byte short, then send that before the actual data. the server can then read the 2 bytes, and read that much data from the stream, ensuring that the right amount of data is present and being read. do the same from server -> client to make sure the client doesn't hang.

Upvotes: 0

Related Questions