Sending and receiving data over network

J

Jan Li?e

Hello,
Im currently programing a network-game in java.
For this i have to transfer on the one hand strings
to implement some simple protocol(like "Ready", "Phase1",etc...)
and on the other hand data, to be more precisely i have to
transfer a char[] [] and later some integers. I know that
i can do the later with serialization. But on my clients is running as a
background thread a tcp receiver and my question is how can i manage to
differentiate within my receiver between the protocol strings i transfer and
the objects i want to send via serialization. Im struggling with this at the
moment...
Thanks in advance,
Jan
 
K

Knute Johnson

Jan said:
Hello,
Im currently programing a network-game in java.
For this i have to transfer on the one hand strings
to implement some simple protocol(like "Ready", "Phase1",etc...)
and on the other hand data, to be more precisely i have to
transfer a char[] [] and later some integers. I know that
i can do the later with serialization. But on my clients is running as a
background thread a tcp receiver and my question is how can i manage to
differentiate within my receiver between the protocol strings i transfer and
the objects i want to send via serialization. Im struggling with this at the
moment...
Thanks in advance,
Jan

Jan:

No matter how you do it, the receiving end is going to need to know what
format the data is in. You can do that several ways; always send the
same kind of data packet or tag the packet with a data type. A simple
example; if you are going to serialize and send several different data
types, send an int first that identifies what is coming next.

How you do this really depends more on the type and quantity of data.
 
V

Vincent H

Jan said:
Hello,
Im currently programing a network-game in java.
For this i have to transfer on the one hand strings
to implement some simple protocol(like "Ready", "Phase1",etc...)
and on the other hand data, to be more precisely i have to
transfer a char[] [] and later some integers. I know that
i can do the later with serialization. But on my clients is running as a
background thread a tcp receiver and my question is how can i manage to
differentiate within my receiver between the protocol strings i transfer and
the objects i want to send via serialization. Im struggling with this at the
moment...
Thanks in advance,
Jan

Jan:

No matter how you do it, the receiving end is going to need to know what
format the data is in. You can do that several ways; always send the
same kind of data packet or tag the packet with a data type. A simple
example; if you are going to serialize and send several different data
types, send an int first that identifies what is coming next.

How you do this really depends more on the type and quantity of data.

Or... You could choose to pack the information in an simple XML
document. Makes the "protocol" more readable and extendible when you
want to add additional functionality in the future.

Parsing the incoming XML might become a bottleneck depending on the
frequency and the amount of data your sending/expecting. But NIO might
come in handy there.

Vincent
 
S

Steve Horsley

Jan said:
Hello,
Im currently programing a network-game in java.
For this i have to transfer on the one hand strings
to implement some simple protocol(like "Ready", "Phase1",etc...)
and on the other hand data, to be more precisely i have to
transfer a char[] [] and later some integers. I know that
i can do the later with serialization. But on my clients is running as a
background thread a tcp receiver and my question is how can i manage to
differentiate within my receiver between the protocol strings i transfer and
the objects i want to send via serialization. Im struggling with this at the
moment...
Thanks in advance,
Jan

Strings and Integers are Objects, as well as arrays, and can all be
serialized. You can use "instanceof java.lang.String" to find out if the
Object that just arrived is a String or not.

Of course, you're stuffed if you're expecting a String and something
else arrives instead, but that would be true of any protocol.

Steve
 
S

Sudsy

Steve Horsley wrote:
Strings and Integers are Objects, as well as arrays, and can all be
serialized. You can use "instanceof java.lang.String" to find out if the
Object that just arrived is a String or not.

Of course, you're stuffed if you're expecting a String and something
else arrives instead, but that would be true of any protocol.

Here's some sample code:

<<<<< ObjectReceiver.java >>>>>

import java.io.*;
import java.net.*;

public class ObjectReceiver {

public static void main( String args[] ) {
ServerSocket serverSocket = null;
Socket socket = null;
ObjectInputStream ois = null;
Object obj = null;

try {
serverSocket = new ServerSocket( 8888 );
socket = serverSocket.accept();
ois = new ObjectInputStream( socket.getInputStream() );
while( ( obj = ois.readObject() ) != null ) {
System.out.println( "received " +
obj.getClass().getName() );
}
socket.close();
serverSocket.close();
}
catch( EOFException e ) {
System.out.println( "End-of-file received" );
}
catch( Exception e ) {
e.printStackTrace();
}
}
}

<<<<< ObjectSender.java >>>>>

import java.io.*;
import java.net.*;

public class ObjectSender {

public static void main( String args[] ) {
Socket socket = null;
ObjectOutputStream oos = null;
Object obj = null;
int x[][] = new int[1][1];

x[0][0] = 4;

try {
socket = new Socket( "localhost", 8888 );
oos = new ObjectOutputStream( socket.getOutputStream() );
oos.writeObject( "test" );
oos.writeObject( x );
socket.close();
}
catch( Exception e ) {
e.printStackTrace();
}
}
}


Pretty simple, eh? Here's the output from the receiver:

received java.lang.String
received [[I
End-of-file received

Just examine the class of the objects received and react accordingly.
 
J

Jon A. Cruz

Steve said:
Strings and Integers are Objects, as well as arrays, and can all be
serialized. You can use "instanceof java.lang.String" to find out if the
Object that just arrived is a String or not.

For an application like this, it's probably better to use an explicit
protocol using DataInputStream and DataOutputStream to put primitives
explicitly (and avoiding sending objects directly)
 
S

Steve Horsley

Jon said:
For an application like this, it's probably better to use an explicit
protocol using DataInputStream and DataOutputStream to put primitives
explicitly (and avoiding sending objects directly)

I agree, after re-reading the original post.

I has seen that he wanted to send a [][] of something and immediately
thought "It's a complex structure, use serialization.". I hadn't noticed
it was just char[][].

Also, serialization might hurt game performance.

Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top