What is the way for sending & receiving objects ?

I hope this is the right section to ask about this problem. I am required to send a complex object to a server. The size of the object is aro10 mbHere are my questions :

1. Currently I am using this method(code 1) :

code 1 --

....

ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(server.getOutputStream()));

ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(server.getInputStream()));

....

code 2 --

...

byte[] recvBuf = new byte[5000];

DatagramPacket packet = new DatagramPacket(recvBuf,recvBuf.length);

DatagramSocket dSock = new DatagramSocket();

dSock.send(packet);

...

will the 2nd code do any better ?

2. Is there any other way which is better for doing it?

Thank in advance for your time and response :)

[859 byte] By [abiieeza] at [2007-11-27 3:57:27]
# 1
UDP is not a reliable protocol so it won't be better. Your first solution is about the best.
ejpa at 2007-7-12 9:01:47 > top of Java-index,Java Essentials,Java Programming...
# 2

Thanks for the reply and this what I've been doing :

public CRequest sendRequest(){

Socket server = new Socket(hostname, port);

ObjectOutputStream out = new ObjectOutputStream(server.getOutputStream());

ObjectInputStream in = new ObjectInputStream( server.getInputStream());

out.writeObject( this );

out.flush();

return (CRequest)in.readObject();

}

Just like what i stated earlier, the data that's going to be sent is about 10mb. So the above function takes quite long time to download the data.

What can i do to improve the function (make it faster to download)? How to check whether there are loss packets ?

Message was edited by:

abiieez

abiieeza at 2007-7-12 9:01:47 > top of Java-index,Java Essentials,Java Programming...
# 3

Use a BufferedOutputStream between the ObjectOutputStream and the Socket.getOutputStream() and specify a buffer size of at least 48k.

Use a BufferedInputStream between the ObjectInputStream and the Socket.getInputStream() and specify a buffer size of at least 48k.

Use a single ObjectInputStream/BufferedInputStream and ObjectOutputStream/BufferedOutputStream for the life of the socket.

Increase the socket's send and receive buffers to at least 48k.

Do this at both ends.

There can't be any lost packets in TCP.

ejpa at 2007-7-12 9:01:47 > top of Java-index,Java Essentials,Java Programming...
# 4
You could also compress the data before sending and uncompress after receiving:java.util.zip.GZIPInputStreamjava.util.zip.GZIPOutputStream
BIJ001a at 2007-7-12 9:01:47 > top of Java-index,Java Essentials,Java Programming...
# 5

thanks for the replies!

client sending objects to server-

..

..

GZIPOutputStream gos = new GZIPOutputStream(s.getOutputStream());

Person p = new Person("Heinz", "Kabutz", 0);

for (int age=0; age < 50000; age++) {

p.setAge(age);

oos.writeObject(p);

}

gos.finish();

...

--server reading objects

while (true) {

Person p = (Person) ois.readObject();

oos.writeObject(ois.readObject());

if (count++ % 1000 == 0) {

System.out.println(count + " : " + p);

}

else if (count==50000)

oos.close();

}

I've followed evertyhing (setting the socket's life to 48k at both ends for sending and receiving) and now I am having a small problem. I am receiving

this error :

java.net.SocketException: Connection reset

and that error points to "Person p = (Person) ois.readObject();".. What should i do ?

Thanks again :)

abiieeza at 2007-7-12 9:01:47 > top of Java-index,Java Essentials,Java Programming...