Results 1 to 9 of 9
- 03-17-2012, 07:21 PM #1
Sockets. Send new coordinates to each client
Hello. Im trying to make online game with sockets, but now i have really big problem. Now for example i have chat application, but how i can send functions instead of message? Let's say 1 client moves to new coordinates and then how 2 client must get 1 client new coordinates? Maybe somehow when 1 client moves send new coordinates to server and server sends new coordinates to all other clients?
- 03-17-2012, 07:40 PM #2
Re: Sockets. Send new coordinates to each client
What do you mean by a "function"?how i can send functions instead of message?
That sounds like a reasonable approach.when 1 client moves send new coordinates to server and server sends new coordinates to all other clients
- 03-17-2012, 07:45 PM #3
Re: Sockets. Send new coordinates to each client
Function, something like that: public void updateCoordinates(int newX, newY){ }
Im thinking about something like that: Server have this function: public void updateCoordinates(int newX, newY){ } And when clients moves to new coordinates, he sends this message: "updateCoordinates(newX, newY)", But thats the problem, because server do not read it as function using, but reads as message.Last edited by Daslee; 03-17-2012 at 07:48 PM.
- 03-17-2012, 08:11 PM #4
Re: Sockets. Send new coordinates to each client
That looks like a method.public void updateCoordinates(int newX, newY){ }
There should be a protocol that is used to tell the other side what to do. The message would say: do an update on the coordiates to this new position(x, y). When the server gets the message, it would call its updateCoordinates() method with the x,y values passed in the message.
- 03-17-2012, 09:07 PM #5
Re: Sockets. Send new coordinates to each client
That's what i need, but how i can send from client to server message which contains:
Lets say i send this: Update X:13 Y:20 And how i must read x and y? I can check if he wants to do update with: if startsWith Update... Maybe using split "x:" and "y:"?do an update on the coordiates to this new position(x, y)
- 03-17-2012, 09:13 PM #6
Re: Sockets. Send new coordinates to each client
Splitting the message up into its parts is called parsing. When the code receives an Update message the parser would strip out the x and y values and call the method with them. To get the parts of the message you could use some of the String class methods: indexOf and substring for example.
Or you could change the protocol to: Update:13:20 and split on the :
- 03-17-2012, 09:24 PM #7
Re: Sockets. Send new coordinates to each client
Thank you so much. Finally i find out how to do that.
Java Code:String whatToSplit; whatToSplit = "UPDATE:13:20"; //Lets say client sended this new coordinates x:13 and y:20 String[] whatToDo; String xCoord, yCoord; whatToDo = whatToSplit.split(":", 0); //Split what we want to do (For now: UPDATE) xCoord = whatToDo[1]; //Split x coordinates yCoord = whatToDo[2]; //Split y coordinates if(whatToDo[0].contains("UPDATE")){ System.out.println(whatToDo[0] + " - We want to update"); System.out.println("And update with this coordinates x: " + xCoord + " y: " + yCoord); }
- 03-17-2012, 09:27 PM #8
Re: Sockets. Send new coordinates to each client
And do more of the same for the other message types.
- 03-17-2012, 09:32 PM #9
Similar Threads
-
need to send diifreent type of data through sockets
By ketan in forum Advanced JavaReplies: 3Last Post: 10-11-2011, 03:38 PM -
client can choose to send message to another client,
By banzuke in forum Threads and SynchronizationReplies: 3Last Post: 08-25-2011, 02:37 PM -
send message from a client to another client
By esko in forum NetworkingReplies: 2Last Post: 02-04-2010, 07:21 PM -
how to send any file size using sockets
By amjad.88 in forum NetworkingReplies: 12Last Post: 03-14-2009, 06:46 AM -
how to send files through sockets
By gabriel in forum Advanced JavaReplies: 3Last Post: 01-12-2008, 08:10 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks