0

I am working on a client server java application, using serialization and deserialization. At first I simply want to send a serialized packet to the server that deserialize it and print it on the screen.

Here is mi Client:

public class Client {

    public static void main(String[] args) throws IOException {

        int portUDP = Integer.parseInt("6004");
        InetAddress host = InetAddress.getByName("127.0.0.1");

        DatagramSocket UDPsock = new DatagramSocket();

        ByteArrayOutputStream oSt = new ByteArrayOutputStream();
        ObjectOutputStream ooSt = new ObjectOutputStream(oSt);

        packet pck = new packet(2,1,3,"try");

        ooSt.writeObject(pck);
        ooSt.flush();

        byte[] sendBuf = new byte[30];
        sendBuf = oSt.toByteArray();

        DatagramPacket payload = new DatagramPacket(sendBuf, sendBuf.length, host, portUDP);
        UDPsock.send(payload);


        UDPsock.close();
    }

}

And here is my Server:

public class Server {

    public static void main(String[] args) throws IOException, ClassNotFoundException {

        int portUDP = 6004;

        DatagramSocket UDPsock = new DatagramSocket(portUDP);

        byte[] payload = new byte[30];
        DatagramPacket inUDP = new DatagramPacket(payload, payload.length);

        UDPsock.receive(inUDP);

        ByteArrayInputStream oSt = new ByteArrayInputStream(inUDP.getData());
        ObjectInputStream ooSt = new ObjectInputStream(oSt);

        packet pck = (packet)ooSt.readObject();

        pck.printContents();

        UDPsock.close();
    }

}

I think that my problem is in the deserialization but I am not able to spot it. Please help me

This are my errors:

Exception in thread "main" java.io.EOFException
    at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2353)
    at java.io.ObjectInputStream$BlockDataInputStream.readUTFBody(ObjectInputStream.java:3092)
    at java.io.ObjectInputStream$BlockDataInputStream.readUTF(ObjectInputStream.java:2892)
    at java.io.ObjectInputStream.readUTF(ObjectInputStream.java:1075)
    at java.io.ObjectStreamClass.readNonProxy(ObjectStreamClass.java:717)
    at java.io.ObjectInputStream.readClassDescriptor(ObjectInputStream.java:833)
    at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1609)
    at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1521)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1781)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1353)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:373)
    at Server.main(Server.java:23)
1
  • I updated it with my errors Commented Oct 3, 2017 at 22:08

1 Answer 1

2
byte[] payload = new byte[30];

The problem is here. Serialized streams are far bigger than this. Try 576.

And keep a watch on the size of the outgoing byte array as well. If this gets over 576 you will run into IP fragmentation, which will start to cause datagram loss, and you will eventually run into the path MTU which is generally only 1250-1500 bytes. So you can't serialize large objects.

Also:

ByteArrayInputStream oSt = new ByteArrayInputStream(inUDP.getData());

should be:

ByteArrayInputStream oSt = new ByteArrayInputStream(inUDP.getData(), inUDP.getOffset(), inUDP.getLength());
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.