We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Fastest network code
Page Index Toggle Pages: 1
Fastest network code? (Read 865 times)
Fastest network code?
Oct 4th, 2005, 9:09pm
 
I am working on a client/server setup where data like:

230,343,3(\n)

This is the coordinates & state of a player in a multiplayer game.

It needs to be sent/received 60 or so times per second. I can get the data to transfer, but the client ends up getting it very slowly and just jerking around the screen.

I have this on the server:
myServer.write(x + "," + y + "," + currentState + "\n");

And the client chops it up with a StringTokenizer.

Whats the best way?

Cheers,
Dr.E
Re: Fastest network code?
Reply #1 - Oct 4th, 2005, 11:17pm
 
send bytes rather than String objects, because String objects have to be parsed back out and create a lot of garbage when used quickly like this situation.
Re: Fastest network code?
Reply #2 - Oct 5th, 2005, 1:37am
 
fry wrote on Oct 4th, 2005, 11:17pm:
send bytes rather than String objects, because String objects have to be parsed back out and create a lot of garbage when used quickly like this situation.


Do you mean send an array of bytes Does Processing have a method for converting an int to byte[] Otherwise the byte is not large enough to hold the coords, etc.

Dr.E
Re: Fastest network code?
Reply #3 - Oct 5th, 2005, 2:59am
 
Might want to look up java.io.DataOutputStream and DataInputStream classes.  Or write your own int<->byte array conversion functions (use bitshift and binary and), float<->byte can be done with the java.lang.Float.floatToRawIntBits(float value) method.

Marcello
Re: Fastest network code?
Reply #4 - Oct 5th, 2005, 12:41pm
 
4 bytes make up an integer. I had to ask about this for my final project last year. I wanted to save bytes instead of String files to save space in recording data from an exhibit that was likely to run all day. K. Damkjer gave me some code that shunts stuff back and forth quite happily:
Code:

int dave = 10;
byte [] larry = {
0,
0,
0,
10
};
void setup(){
noLoop();
}
void draw(){
println (joinBytes(larry));
byte [] tom = splitInteger(dave);
for(int i = 0; i < tom.length; i++){
println(tom[i]);
}
}

byte [] splitInteger (int num){
byte [] sendBack = {
byte((num&0xFF000000)>>24),
byte((num&0x00FF0000)>>16),
byte((num&0x0000FF00)>>8),
byte((num&0x000000FF)),
};
return sendBack;
}
int joinBytes (byte [] bytes){
return (bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | bytes[3];
}
Re: Fastest network code?
Reply #5 - Oct 5th, 2005, 3:26pm
 
That's the ticket! Many thanks!

Dr.E
Page Index Toggle Pages: 1