Send String Arraylist via UDP in java

Asked

Viewed 54 times

0

Hello, the question is this: How do I send an entire Arraylist via Server UDP to the Java client? Can send a String only at a time and works very well. However, I don’t know how to send a String Arraylist.

Part of my code

SERVER

DatagramPacket request = new DatagramPacket(buffer, buffer.length);
    request.setData("Testando".getData());
    DatagramPacket reply = 
    new DatagramPacket(request.getData(), request.getLength(), request.getAddress(), request.getPort());
    aSocket.send(reply);

CLIENT

byte[] buffer = new byte[100];
            DatagramPacket reply = new DatagramPacket(buffer, buffer.length);
            aSocket.receive(reply);
            System.out.println("Resposta:" + new String(reply.getData()));

This way I can send the word "Testing" from the server to the client. However, I want to Send String Arraylist!

  • Do you need to send via UDP? I think it only complicates things compared to TCP. The packages will arrive in different order, for example.

  • Anyway, to send objects you need to use Objectoutputstream and write to a buffer (use Bytearrayoutputstream and toByteArray()).

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.