Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (491)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (556)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  About sending data on sockets  (Read 3007 times)
0 Members and 1 Guest are viewing this topic.
Offline elioncho

Senior Newbie





« Posted 2008-03-27 21:59:52 »

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
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 784
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2008-03-27 22:21:46 »

1  
2  
3  
4  
5  
6  
7  
8  
// 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();

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline elioncho

Senior Newbie





« Reply #2 - Posted 2008-03-27 23:49:02 »

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?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline elioncho

Senior Newbie





« Reply #3 - Posted 2008-03-28 00:21:26 »

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.
Offline DzzD
« Reply #4 - Posted 2008-03-28 02:01:36 »

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 ?

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 784
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2008-03-28 05:53:10 »

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.


Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline DzzD
« Reply #6 - Posted 2008-04-01 19:29:00 »

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

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  Roll Eyes

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

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 784
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #7 - Posted 2008-04-01 21:38:01 »

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

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

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline DzzD
« Reply #8 - Posted 2008-04-01 23:36:50 »

Quote
tcpnodelay = one frame per write-op.

thanks, I thought you was right but I could not resist and I made a verification Smiley and you are absolutly right.... this is exacly that!:
one write==one frame (or two sometimes of course....)

thanks


Offline Riven
« League of Dukes »

JGO Overlord


Medals: 784
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #9 - Posted 2008-04-02 00:34:43 »

or three :p

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Nickropheliac (15 views)
2014-08-31 22:59:12

TehJavaDev (23 views)
2014-08-28 18:26:30

CopyableCougar4 (33 views)
2014-08-22 19:31:30

atombrot (41 views)
2014-08-19 09:29:53

Tekkerue (40 views)
2014-08-16 06:45:27

Tekkerue (35 views)
2014-08-16 06:22:17

Tekkerue (25 views)
2014-08-16 06:20:21

Tekkerue (37 views)
2014-08-16 06:12:11

Rayexar (72 views)
2014-08-11 02:49:23

BurntPizza (49 views)
2014-08-09 21:09:32
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!