Results 1 to 9 of 9
  1. #1
    lkcz is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

    Question send multiple ints of data from the server to the client..

    Basically I am not sure how to send multiple ints of data at once from the server to the client and vise versa.
    lets say:
    int hp = 10;
    int bullets = 25;

    Server ==> Client

    client reads:
    hp = 10;
    bullets = 25;

    I would like the server send this data to the client instead of announcing it in the client.
    This is so that I can use the saving method to load games..
    Any help would be highly appreciated. :)

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

    Default

    Do you have the code for using Sockets and ServerSockets for communicating between the client and server? Or how are you sending data from one to the other?
    When you have the code for connecting you need to define a "protocol" for how you are going to send data between the two. Many ways to do this.
    1)Convert the ints to Strings and send a String with a separator between the two numbers.
    2)Use a data stream to sent the ints as 4 byte int values.

    Its up to you to look at how the server and client will communicate and define a protocol.

  3. #3
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    When I set up the networking for my MMO, I did as Norm suggested and set it up as a String. The easiest way is probably to set up a class that has two methods, one to convert an array of values to a String and vice-versa.

  4. #4
    lkcz is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

    Default

    Well I never worked with the intToByte method before, could someone post an example and how the numbers are converted?
    I have already search for an explanation to this method but never received one.

    EX:
    Java Code:
    public static final byte[] intToByteArray(int value) {
            return new byte[] {
                    (byte)(value >>> 24),
                    (byte)(value >>> 16),
                    (byte)(value >>> 8),
                    (byte)value};
    }
    Java Code:
    public static final int byteArrayToInt(byte [] b) {
            return (b[0] << 24)
                    + ((b[1] & 0xFF) << 16)
                    + ((b[2] & 0xFF) << 8)
                    + (b[3] & 0xFF);
    }
    Could someone explain how these methods work?

  5. #5
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    How familiar are you with bit operations? Do you know what & does, and << does, and >>> does? I guess what I need to know is, are you asking how the functions work, or how the operands work?

  6. #6
    lkcz is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

    Default

    I am not familiar with the bit operations..
    btw my first name is zack too :D

  7. #7
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Neat-o. Small world, hey?

    As for the bit operations, you can find a lot of explanations on Google:
    Bitwise and Bit Shift Operators (The Java™ Tutorials > Learning the Java Language > Language Basics) (Java-specific)
    Bitwise operation - Wikipedia, the free encyclopedia (Generic)
    PHP: Bitwise Operators - Manual (PHP-based, but the same concepts apply as Java)

    Basically, to understand this, you have to think in binary. Let's imagine you have the number 12 (binary: 1100) and the number 7 (binary: 111). For an AND operation (&), you keep the bits (0s ans 1s) that are identical to each other. If they are not identical, you use a zero. This is best explained by an example:
    Java Code:
      1100
    & 0111
    ------
      0100
    Notice how because we have a 0 and 1 in the first, third, and fourth columns, we keep a 0. Since both numbers in column 2 are 1, we keep a 1. (If both numbers were 0, we'd keep a 0.)

    With shifting (>> and <<), it shifts the bits a position. So, if we have number 12 (1100), and we shift it left (using <<), we will get 11000, which is 24. Similarly, if we take 12 (1100) and shift it right (using >>), we get 110, which is 6. (Notice how it's just multiplying/dividing by 2? ;))

    The >>> operator is simply an unsigned right shift operator, which means that a 0 will be put on the leftmost position.

    Here is the result of some bitwise operators in Java code:
    Java Code:
    	System.out.println(12 << 1);
    	System.out.println(12 >> 1);
    	System.out.println(12 >>> 1);
    Java Code:
    Output:
    24
    6
    6
    In this case (and most), the >> and >>> operators serve the same purpose. Only when signage is important do they differ.

  8. #8
    lkcz is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

    Default

    Zack, I was reading the bit tutorial on shifting before I read this..
    I have to say, THANK YOU.. you simplified it to an easier understanding


    But what purpose does the 12 and 1 represent?
    what does the 1 do?
    Java Code:
    12 << 1
    Never mind, I just realized that it only shifts over 1
    Last edited by lkcz; 09-23-2010 at 09:03 PM.

  9. #9
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Yup. The right digit is how many to shift it by. Example would be, 2 << 5 == 64. (10 << 5 == 1000000)

Similar Threads

  1. send bytes from client to server
    By 0xHexaDecimal in forum Networking
    Replies: 41
    Last Post: 06-04-2010, 02:17 PM
  2. how to send mp3 file from server to client
    By Jigga008 in forum Networking
    Replies: 0
    Last Post: 12-01-2009, 12:02 PM
  3. Server socket - send image to client
    By Hinty in forum Networking
    Replies: 2
    Last Post: 03-14-2009, 07:39 AM
  4. send/read int in a client/server app
    By dim_ath in forum New To Java
    Replies: 2
    Last Post: 01-03-2008, 01:03 PM
  5. how to send .jar files client to server
    By gobinathm in forum Networking
    Replies: 1
    Last Post: 12-25-2007, 04:05 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
  •