Results 1 to 9 of 9
- 09-23-2010, 12:48 AM #1
Member
- Join Date
- Sep 2010
- Posts
- 5
- Rep Power
- 0
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. :)
- 09-23-2010, 01:55 AM #2
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.
- 09-23-2010, 05:28 AM #3
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.
- 09-23-2010, 08:34 PM #4
Member
- Join Date
- Sep 2010
- Posts
- 5
- Rep Power
- 0
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}; }Could someone explain how these methods work?Java Code:public static final int byteArrayToInt(byte [] b) { return (b[0] << 24) + ((b[1] & 0xFF) << 16) + ((b[2] & 0xFF) << 8) + (b[3] & 0xFF); }
- 09-23-2010, 08:35 PM #5
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?
- 09-23-2010, 08:38 PM #6
Member
- Join Date
- Sep 2010
- Posts
- 5
- Rep Power
- 0
I am not familiar with the bit operations..
btw my first name is zack too :D
- 09-23-2010, 08:50 PM #7
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:
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.)Java Code:1100 & 0111 ------ 0100
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);
In this case (and most), the >> and >>> operators serve the same purpose. Only when signage is important do they differ.Java Code:Output: 24 6 6
- 09-23-2010, 08:54 PM #8
Member
- Join Date
- Sep 2010
- Posts
- 5
- Rep Power
- 0
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?
Never mind, I just realized that it only shifts over 1Java Code:12 << 1
Last edited by lkcz; 09-23-2010 at 09:03 PM.
- 09-24-2010, 03:34 AM #9
Yup. The right digit is how many to shift it by. Example would be, 2 << 5 == 64. (10 << 5 == 1000000)
Similar Threads
-
send bytes from client to server
By 0xHexaDecimal in forum NetworkingReplies: 41Last Post: 06-04-2010, 02:17 PM -
how to send mp3 file from server to client
By Jigga008 in forum NetworkingReplies: 0Last Post: 12-01-2009, 12:02 PM -
Server socket - send image to client
By Hinty in forum NetworkingReplies: 2Last Post: 03-14-2009, 07:39 AM -
send/read int in a client/server app
By dim_ath in forum New To JavaReplies: 2Last Post: 01-03-2008, 01:03 PM -
how to send .jar files client to server
By gobinathm in forum NetworkingReplies: 1Last Post: 12-25-2007, 04:05 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks