Results 1 to 9 of 9
  1. #1
    Daslee's Avatar
    Daslee is offline Member
    Join Date
    Mar 2012
    Location
    Plunge, Lithuania
    Posts
    36
    Rep Power
    0

    Default 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?

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,317
    Rep Power
    25

    Default Re: Sockets. Send new coordinates to each client

    how i can send functions instead of message?
    What do you mean by a "function"?

    when 1 client moves send new coordinates to server and server sends new coordinates to all other clients
    That sounds like a reasonable approach.

  3. #3
    Daslee's Avatar
    Daslee is offline Member
    Join Date
    Mar 2012
    Location
    Plunge, Lithuania
    Posts
    36
    Rep Power
    0

    Default Re: Sockets. Send new coordinates to each client

    Quote Originally Posted by Norm View Post
    What do you mean by a "function"?


    That sounds like a reasonable approach.

    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.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,317
    Rep Power
    25

    Default Re: Sockets. Send new coordinates to each client

    public void updateCoordinates(int newX, newY){ }
    That looks like a method.

    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.

  5. #5
    Daslee's Avatar
    Daslee is offline Member
    Join Date
    Mar 2012
    Location
    Plunge, Lithuania
    Posts
    36
    Rep Power
    0

    Default Re: Sockets. Send new coordinates to each client

    Quote Originally Posted by Norm View Post
    That looks like a method.

    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.
    That's what i need, but how i can send from client to server message which contains:
    do an update on the coordiates to this new position(x, y)
    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:"?

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,317
    Rep Power
    25

    Default 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 :

  7. #7
    Daslee's Avatar
    Daslee is offline Member
    Join Date
    Mar 2012
    Location
    Plunge, Lithuania
    Posts
    36
    Rep Power
    0

    Default Re: Sockets. Send new coordinates to each client

    Quote Originally Posted by Norm View Post
    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 :
    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);
    		}

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,317
    Rep Power
    25

    Default Re: Sockets. Send new coordinates to each client

    And do more of the same for the other message types.

  9. #9
    Daslee's Avatar
    Daslee is offline Member
    Join Date
    Mar 2012
    Location
    Plunge, Lithuania
    Posts
    36
    Rep Power
    0

    Default Re: Sockets. Send new coordinates to each client

    Quote Originally Posted by Norm View Post
    And do more of the same for the other message types.
    Yes i know. :P

Similar Threads

  1. need to send diifreent type of data through sockets
    By ketan in forum Advanced Java
    Replies: 3
    Last Post: 10-11-2011, 03:38 PM
  2. client can choose to send message to another client,
    By banzuke in forum Threads and Synchronization
    Replies: 3
    Last Post: 08-25-2011, 02:37 PM
  3. send message from a client to another client
    By esko in forum Networking
    Replies: 2
    Last Post: 02-04-2010, 07:21 PM
  4. how to send any file size using sockets
    By amjad.88 in forum Networking
    Replies: 12
    Last Post: 03-14-2009, 06:46 AM
  5. how to send files through sockets
    By gabriel in forum Advanced Java
    Replies: 3
    Last Post: 01-12-2008, 08:10 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
  •