Sending and receiving data over network

Discussion in 'Java' started by Jan Li?e, Feb 7, 2004.

  1. Jan Li?e

    Jan Li?e Guest

    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 Li?e, Feb 7, 2004
    #1
    1. Advertising

  2. Jan Li?e wrote:

    > 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.

    --

    Knute Johnson
    email s/nospam/knute/
    Molon labe...
     
    Knute Johnson, Feb 7, 2004
    #2
    1. Advertising

  3. Jan Li?e

    Vincent H Guest

    On Sat, 07 Feb 2004 17:48:39 GMT, Knute Johnson
    <> wrote:
    >Jan Li?e wrote:
    >
    >> 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
     
    Vincent H, Feb 7, 2004
    #3
  4. Jan Li?e wrote:
    > 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
     
    Steve Horsley, Feb 7, 2004
    #4
  5. Jan Li?e

    Sudsy Guest

    Steve Horsley wrote:
    <snip>
    > 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.
     
    Sudsy, Feb 7, 2004
    #5
  6. Jan Li?e

    Jon A. Cruz Guest

    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.
    >


    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)
     
    Jon A. Cruz, Feb 8, 2004
    #6
  7. Jon A. Cruz wrote:
    > 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.
    >>

    >
    > 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
     
    Steve Horsley, Feb 8, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    500
  2. TreatmentPlant
    Replies:
    4
    Views:
    414
    Default User
    Oct 17, 2007
  3. subeen
    Replies:
    2
    Views:
    592
    subeen
    Oct 4, 2009
  4. Mike

    Receiving and sending data

    Mike, Aug 2, 2006, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    122
  5. pintu
    Replies:
    0
    Views:
    206
    pintu
    Dec 14, 2006
Loading...

Share This Page