About sending data on sockets

Hello,

I am using right know dataOutput and Input Streams to send data between server and clients. Using WriteInt and ReadInt I send and receive positions over the net. What I understand is that if I have:

out.writeInt(x);
out.writeInt(Y);

I am sending one packet after another, so Im trying to make like a frame protocol where I could send x and y in only one packet. Any ideas of how to do this?

Thanks,

Elías


// os  = socket.getOutputStream();
// bos = new BufferedOutputStream(os, maxPacketSize);
// dos = new DataOutputStream(bos);

dos = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
dos.writeInt(x);
dos.writeInt(y);
dos.flush();

Does anyone here uses like a protocol or a communication frame. Maybe one frame could be :

out.writeUTF(P@X@Y);

When the other side receives it and do an split("@");. It will understand its a Position frame(because of the P) and then use x and y.

If the frame is :

out.writeUTF(M@SADASDASDASD);

M stands for message so it will manage the frame as a text message.

Anyone has used this kind of idea?

Being more explicit:

Maybe I dont understand the theory on DataOutputStreams. Anyway I am going to try to be more explicit.
When I have the following:

out.writeInt(x);
out.writeInt(y);

I believe that the x is sent first in a frame that includes the address where it should go etc (IP frame). And then the y is sent on another independent frame with its header, address where it should go etc.

So if the aforementioned is true, wouldnt it better to send x and y in only one frame. I could make my own protocol to hande the data. For example

out.writeUTF(P@X@Y);

When I receive something like this, I will break it into parts by using .split("@") and then check that if it has a “P” is a Positional message. So I know the other two data splitted are X and Y.

If I have a out.writeUTF(M@HELLO); , The M will tell me its a textMessage and I will take the rest of the content as a message.

Does anyone has donde something like this? Please help.

I dont think that even with tcpnodelay allowed the following will sent two packet
out.writeInt(x);
out.writeInt(y);

this only write a binary representation of x & y on the outpustream (the socket here), the tcp packet size doe not depend on the fact you make one or two call. the underlying layer will take care of starting new tcp packet when requiered. and the flush method will try to empty the buffer and sent as soon as possible. so I think that Riven sample work as you are expcting ?

with tcpnodelay=true, 2 writeInts will be 8 (!!) frames as DataOutputStream does 4 write(byte)s per int.

with tcpnodelay=false, it’s either 1 or 2 frames, depending on whether the buffer was just about to ‘overflow’

with BufferedOutputStream the developer is in control.

[quote]with tcpnodelay=true, 2 writeInts will be 8 (!!) frames as DataOutputStream does 4 write(byte)s per int.
[/quote]
sure??, very strange, I was thinking that the underlying layers was doing some kind of buffering/wait even with tcpnodelay=true.

I mean if you write two byte, the second byte will be write before the first one has been sended even with a network of 100Mb/s , no?

tcpnodelay does not mean byte per byte, but rather as soon as possible. I think ::slight_smile:

EDIT: I mean tcpnodelay was added to only avoid problem with very small packet sended with a small delay between them , but with nearly nodelay I think the underlying layer do the job?

EDIT2: I will get some time to make a verification on this point using Wireshark, and will let you know about results

[quote]tcpnodelay does not mean byte per byte, but rather as soon as possible. I think
[/quote]
tcpnodelay = one frame per write-op.

for(int i=0; i<16; i++) out.write(i); => 16 frames
for(int i=0; i<4; i++) out.write(new byte[4]); => 4 frames

[quote]tcpnodelay = one frame per write-op.
[/quote]
thanks, I thought you was right but I could not resist and I made a verification :slight_smile: and you are absolutly right… this is exacly that!:
one write==one frame (or two sometimes of course…)

thanks

or three :stuck_out_tongue: