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)