Results 1 to 4 of 4
  1. #1
    Vampire5 is offline Member
    Join Date
    Aug 2012
    Posts
    20
    Rep Power
    0

    Question Sending Game Data to Server

    Hi! I've been doing some work with Java Networking, specifically a Chat that sends a string to a server which then broadcasts to the other clients. I would like to create a game, but I am wondering how I would send data to the server in such a way that when it goes to the other clients, it doesn't confuse information.

    For instance, how would I send the x and y coordinates of an object to a server. Please note that I would have other variables that might not be integers/doubles/floats, but maybe booleans, chars, or strings.

    However, one thing that I was thinking is, would it be better (and possible) to send a whole class, such as a "spaceship" class or something like that. That way, the class would already contain all the variables.

    In any case, thanks in advance!

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,563
    Rep Power
    25

    Default Re: Sending Game Data to Server

    how I would send data to the server in such a way that when it goes to the other clients, it doesn't confuse information.
    Define a "protocol" for all the data that is sent. For example have a header field that describes what is in the record that is being sent. Any code that reads the header field would know how to read the rest of the record.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Vampire5 is offline Member
    Join Date
    Aug 2012
    Posts
    20
    Rep Power
    0

    Default Re: Sending Game Data to Server

    Quote Originally Posted by Norm View Post
    For example have a header field that describes what is in the record that is being sent.
    Forgive me, I'm still rather new to Java Network programming, would you mind clarifying a bit? The only thing I've done is sent Strings back and forth (writeUTF and stuff like that).

    Thanks!

  4. #4
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Sending Game Data to Server

    Hi,

    you can define a protocol that you want to use.

    So if you want to send messages, then you have to define a delimiter between messages. On text based protocols (e.g. IRC, SMTP, NNTP, ...) you can have something like "a newline splits up 2 messages". And then you could define something like "First comes a command followed by a whitespace and then optional stuff that depend on the command. So your commands could define something like
    MSG <target> <message>
    COORDS <x> <y>
    And so on ... (Also be aware that you should define an encoding e.g. ASCII.)

    Or for a game you might want to reduce the traffic. So you could decide to use Bytes only. Then you could define a protocol like this:
    <4 Bytes: Size of message>
    <1 Byte: Command>
    <Optional stuff>

    That is something that I build. So my loop that reads the Bytes checks the first 4 bytes (an integer) for the size required and reads data into the buffer till the message is read completely. Then depending on the next byte I call a method that reads the message from the ByteBuffer. So the read/write methods just need to read/write data in the same order. Integers are quit simple. Text is a little harder because the size is not fixed. I just used the same approach as I did with the message: I write the length first and then I read the text. (But of course: You could also use the ascii encoding and use the limitation of strings as it is done in C: A 0x00 ends the string.)

    But all this is completely up to you. You can do it in any way you like. Just be sure that you can distinguish everything. (Just concatenate the messages send to each other and you need to be able to separate them again.)

    With kind regards,

    Konrad

Similar Threads

  1. Replies: 10
    Last Post: 05-17-2014, 07:50 PM
  2. Help with sending data?
    By ludo0777 in forum New To Java
    Replies: 4
    Last Post: 06-06-2012, 07:19 PM
  3. Replies: 4
    Last Post: 09-29-2011, 01:17 PM
  4. Sending more than one message to the server.
    By eLancaster in forum New To Java
    Replies: 5
    Last Post: 05-31-2011, 08:34 PM
  5. Sending a Vector to TCP server
    By eva in forum Networking
    Replies: 1
    Last Post: 01-02-2008, 10:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •