Jump to content
  • Advertisement
Sign in to follow this  
CombatHammie

[java] What to send through Sockets?

This topic is 4282 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So I have Client/Server implementing ServerSocket/Sockets. While practicing I just used readDouble/readInt/etc. But now that it is getting more complex (requesting information from the server that executes a MySQL query and returns all sort of data), how do I go about this? Is readObject() safe/efficient?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by CombatHammie
So I have Client/Server implementing ServerSocket/Sockets. While practicing I just used readDouble/readInt/etc.


If you intend your server to scale efficiently, avoid using the java.net package and use NIO instead (ByteBuffers, Selectors, SocketChannels, and so on). The java.net classes are implemented such that you can only do blocking IO with them, so in order for a server to handle multiple users you must create one thread per socket. This is horribly inefficient when dealing with large numbers of connections. NIO allows you to do non-blocking IO and set up a model based on the select() method from Berkley sockets in the C world.

Quote:
But now that it is getting more complex (requesting information from the server that executes a MySQL query and returns all sort of data), how do I go about this?


For games, generally you'd want to develop a packet protocol. For each packet, you send a header containing enough information for the other end to know how to handle it, followed by the payload (data). For example, a simple header might contain an ID and a payload size. When a packet is received, the header is parsed to decide how to handle it, then the payload is parsed to fetch the data. This allows you to choose appropriate data types to make the packet as small as possible. Serializing objects over the wire is not very bandwidth efficient.

For your MySQL queries, if the queries are fixed you might use a simple byte or short value that represents a query code. The server, upon encountering a query packet, parses the code and executes the appropriate query. If the queries are not fixed, you might use the query string as the payload, which the server would check for security (to prevent malicious queries) and then execute.

The structure of each packet need not correspond to the layout of your objects, nor do the data types need be the same. For example, you might store a particular value in an object as an int, but send it over the wire as a byte or a short if you are sure that it will never exceed the maximum value of either type. You could also encode several boolean values in a single byte and send it instead of each individual boolean. This sort of thing helps to keep your bandwidth down.

Quote:
Is readObject() safe/efficient?


It's as safe as sending any raw data over the internet can be. As for efficiency, in terms of bandwidth not so much. In terms of performance, that really depends on the type of application. For each object you send, there's a serialization step on write and a deserialization step on read. This may or may not be more efficient than any packet-based system you implement (in the general case, I'd say it wouldn't be). However, you do have to watch out for the common serialization pitfalls when reading/writing objects.

Share this post


Link to post
Share on other sites
Thank you for a very informative reply.

For now, I am going to send whole objects, as latency is really not all that much of a concern for this specific project.

Pocket design is something I definitely need to look into for my future projects.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!