3

I'm trying to implement object serialization but stuck at the StreamCorruptedException.

On the server's side:

public class MulticastServer {
                public static void main(String[] args) throws java.io.IOException {
                new MulticastServerThread().start();
                }
} 

Which calls:

public class MulticastServerThread extends QuoteServerThread {
                 boolean moreQuotes=true;
public void run() {
  while (moreQuotes) {
  try {
    byte[] buf = new byte[256];
    String dString="Server";
    System.out.println(dString);
    buf = dString.getBytes();

    InetAddress group = InetAddress.getByName("230.0.0.1");
    DatagramPacket packet = new DatagramPacket(buf, buf.length,
                                               group, 4446);

    socket.send(packet);

    ObjectInputStream is=null;

    ByteArrayInputStream byteStream = new ByteArrayInputStream(recvBuf);
    is = new ObjectInputStream(new BufferedInputStream(byteStream));

    Object o1=(Object)is.readObject();
    System.out.println(o1.a);

    is.close();
    socket.close();
    }}}}

And Object class on both server and client:

public class Object implements Serializable{
private static final long serialVersionUID=1L;
int a=10;
}

And Client side code:

public class MulticastClient {
public static void main(String[] args) throws IOException {
MulticastSocket socket = new MulticastSocket(4446);
InetAddress address = InetAddress.getByName("230.0.0.1");
socket.joinGroup(address);

Object o1=new Object();

DatagramPacket packet;

for (int i = 0; i < 5; i++) {
  byte[] buf = new byte[256];
  packet = new DatagramPacket(buf, buf.length);
  socket.receive(packet);

  String received = new String(packet.getData());
  System.out.println("received data" +received);

  ObjectOutputStream os = null;
  ByteArrayOutputStream byteStream = new ByteArrayOutputStream(15000);
            os = new ObjectOutputStream(new BufferedOutputStream(byteStream));
            os.flush();
            os.writeObject((Object)o1);
            System.out.println(o1.a);
            os.flush();
 }
 socket.leaveGroup(address);
 socket.close();
 }
 }

I've done all this:

  1. Put all classes under the same path on both machines
  2. attach breaks and try to find out where the problem is

Can someone please help me? Thanks!

3
  • I'm struggling to see how the ByteArray[Input|Output]Streams in your client and server are supposed to work. Do you expect them to be connected? Commented Dec 12, 2011 at 12:00
  • Oh. I got it now. but how do I make the server wait until the client sends it? Commented Dec 12, 2011 at 12:05
  • You should be able to have receive() wait until there is a packet. Commented Dec 12, 2011 at 12:44

1 Answer 1

2

If you are trying to do object serialization over UDP, then you need to serialize the object into the packet's byte array, and read the object from that at the receiving end. There is an old Java World article: Object transport via datagram packets which you might find useful.

It looks like you want to implement a two-directional communication:

server -> send packet
client -> receive packet
client -> send object
server -> receive object

I'm not sure how that's going to work out for you (especially if you have multiple clients).

But at least the following changes will be needed to get anything working:

  • Your server code is going to need to do a blocking receive call after sending the packet.
  • The client code is going to need to send the object in a packet.
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.