how to... join DatagramPacket and Serializable

Hi!

Here's what I want to achieve:

I want to send a packet much more bigger than 256bytes. My serializable object has about 4000bytes. And I want send him to Client. How to do that? Everything is working while an object has not more than 256bytes. If it's more, there's exception like:

java.io.EOFException

at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2279)

at java.io.ObjectInputStream$BlockDataInputStream.readUTFBody(ObjectInputStream.java:3017)

...

Is there any better method to send packets of any size?

[593 byte] By [blelumpa] at [2007-11-27 4:55:51]
# 1
If your network path includes a router you may have trouble getting a 4k datagram through it. I would use TCP for this.The EOFException can only mean that you didn't put all the serialized data into the DatagramPacket. Coding error. Did you flush & close the
ejpa at 2007-7-12 10:10:53 > top of Java-index,Core,Core APIs...
# 2
1. How to use TCP then?2. Yes, I closed ObjectOutputStream. I guess the Exception occured cause just first 256bytes has been send via one packet (the whole object has about 4000bytes so...)Message was edited by: blelump
blelumpa at 2007-7-12 10:10:53 > top of Java-index,Core,Core APIs...
# 3

1. java.net.Socket, java.net.ServerSocket, new ObjectOutputStream(socket.getOutputStream()), etc. Much easier. Why would you try to use a streaming protocol over a datagram protocol?

2. You must have incorrectly constructed the DatagramPacket to be sent, i.e. with a size of 256 instead of the size of the ByteArrayOutputStream.

ejpa at 2007-7-12 10:10:53 > top of Java-index,Core,Core APIs...
# 4

/////////////////// Server

ServerSocket serverSocket;

try {

serverSocket = new ServerSocket(4444);

ObjectOutputStream out;

Socket clientSocket = serverSocket.accept();

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

Object temp=in.readObject();

javax.swing.JButton button = (javax.swing.JButton)temp;

clientSocket = serverSocket.accept();

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

JLabel label = new JLabel("lab");

out.flush();

out.writeObject(label);

out.flush();

in.close();

out.close();

clientSocket.close();

}

catch (IOException e1) {

e1.printStackTrace();

}

catch(ClassNotFoundException e) {

e.printStackTrace();

}

///////////////// Client

[code]

Socket socket = new Socket("localhost", 4444);

JButton butt = new JButton("button");

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

out.flush();

out.writeObject(butt);

out.flush();

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

Object temp=in.readObject();

in.close();

out.close();

socket.close();

and well Server gets the JButton object (and sends JLabel object aswell) but client doesn't want get JLabel object. there's exception:

Exception in thread "main" java.net.SocketException: Broken pipe

at java.net.SocketOutputStream.socketWrite0(Native Method)

at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)

at java.net.SocketOutputStream.write(SocketOutputStream.java:136)

at java.io.ObjectOutputStream$BlockDataOutputStream.drain(ObjectOutputStream.java:1838)

at java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(ObjectOutputStream.java:1747)

at java.io.ObjectOutputStream.writeNonProxyDesc(ObjectOutputStream.java:1249)

at java.io.ObjectOutputStream.writeClassDesc(ObjectOutputStream.java:1203)

at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1387)

at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1150)

at java.io.ObjectOutputStream.writeFatalException(ObjectOutputStream.java:1538)

at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:329)

what I'm doing wrong?

blelumpa at 2007-7-12 10:10:53 > top of Java-index,Core,Core APIs...
# 5
You have two accepts. Is the client connecting twice, once to send and once to receive? Doesn't seem likely - use the same accepted socket for both.
ejpa at 2007-7-12 10:10:53 > top of Java-index,Core,Core APIs...
# 6
what do you mean? Don't I use same socket for both? There's just ObjectOutputStream for the output and ObjectInputStream for the input of the socket. Shouldn't it be like that? And how should it be then?
blelumpa at 2007-7-12 10:10:53 > top of Java-index,Core,Core APIs...
# 7
I finally fixed it out. Thank you for your time :)
blelumpa at 2007-7-12 10:10:53 > top of Java-index,Core,Core APIs...