Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Sending an object over a socket  RSS feed

 
Jón Jónsson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As my first post states I'm writing this game where clients connect to a server and can see each other move around on a small board. Each of the player is represented by an object that has x,y coordinates, an Id and a string containing that players name, this information is stored on the server side.
Each client is responsible for drawing each of the players on the board, so in order to do that the client obviously needs these information from the server. Pretending that the server is simply an object on you local machine that you can easily reference, you could easily get these information via a get method from the server. Since we are connected with the server via socket connection that is a bit more complicated, the information must be sent to the client in the form of String, then the client must store that String and extract the information from it. Lets say there is only one player in the game and that client requests that players (his own) information, the server might respond with the String "2,3,john" that is x and y coordinates and the name, the client must parse that String and convert the first two arguements to integers and so on.

Finally, my question is, is there a more convenient way to send such information to the clients via socket?
 
Jayesh A Lalwani
Rancher
Posts: 2762
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can always serialize the object. But that's not the best way to do it. Serialization is very heavy.

I am assuming that since you are implementing a game, performance is a big factor for you. The lightest way to do this is to parse the information yourself. The best way to do it is to make your object Externalizable and implement your own parsing in your readExternal and writeExternal methods


 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!