• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Sytto

Logic for handle multiple clients in my TCP server written in java

5 posts in this topic

hi [img]http://public.gamedev.net//public/style_emoticons/default/tongue.png[/img] first of all, this is my first post!
second, i read the FAQ's, but my doubts are still there [img]http://public.gamedev.net//public/style_emoticons/default/sad.png[/img]

So I'm programming a server for a multiplayer game in Java, but I don't know the proper way to handle the client connections.

The game is like a tiny mmo inspired inspired by Transformice. For those who do not know, Transformice is a MMO flash and tcp based game. It has around 60.000 concurrent players / day average and I wonder how they handle this amount of connections. Right now, my server spawns a thread for every client connection and I read in the FAQ's this is not good but also I thought same before read it.

So basically the question is, how i manage my server to keep as many client connections as hardware / network is capable to (Without spawning a new thread for each client connection because i think this is not suitable for a mmo game).

As far as i know JVM can't handle that huge amount of threads, so this can't be the right way.

also I'm such a noob in networking. I started to learn it few months ago.
Someone gives me some light?

Regards. Edited by Sytto
0

Share this post


Link to post
Share on other sites
You should be using, last I recall, the NIO sockets.
0

Share this post


Link to post
Share on other sites
[quote name='Sytto' timestamp='1336249083' post='4937658']
hi [img]http://public.gamedev.net//public/style_emoticons/default/tongue.png[/img] first of all, this is my first post!
second, i read the FAQ's, but my doubts are still there [img]http://public.gamedev.net//public/style_emoticons/default/sad.png[/img]

So I'm programming a server for a multiplayer game in Java, but I don't know the proper way to handle the client connections.

The game is like a tiny mmo inspired inspired by Transformice. For those who do not know, Transformice is a MMO flash and tcp based game. It has around 60.000 concurrent players / day average and I wonder how they handle this amount of connections. Right now, my server spawns a thread for every client connection and I read in the FAQ's this is not good but also I thought same before read it.

So basically the question is, how i manage my server to keep as many client connections as hardware / network is capable to (Without spawning a new thread for each client connection because i think this is not suitable for a mmo game).

As far as i know JVM can't handle that huge amount of threads, so this can't be the right way.

also I'm such a noob in networking. I started to learn it few months ago.
Someone gives me some light?

Regards.
[/quote]

First of all, Transformice is not an MMO, (each game has a very low number of players so you don't need to deal with MMO scale networking for this type of game).

What you should do is have one master server for the "lobby" and then run multiple game servers, They can but doesn't have to be on the same machine but needs different ports and should be handled by their own instance of the server application.

Have the master server keep track of how many instances of the game server each node is running and launch new ones as needed on the least busy node, (You can have game servers report their nodes status to the master server for this purpose)

For the game servers 1 or 2 threads are enough, (having one thread that only waits for new connections and one that runs the game can simplify things slightly)
0

Share this post


Link to post
Share on other sites
[quote name='Washu' timestamp='1336253793' post='4937665']
You should be using, last I recall, the NIO sockets.
[/quote]

thank you very much, didn't know about this!

[quote name='SimonForsman' timestamp='1336253844' post='4937666']
First of all, Transformice is not an MMO, (each game has a very low number of players so you don't need to deal with MMO scale networking for this type of game).

What you should do is have one master server for the "lobby" and then run multiple game servers, They can but doesn't have to be on the same machine but needs different ports and should be handled by their own instance of the server application.

Have the master server keep track of how many instances of the game server each node is running and launch new ones as needed on the least busy node, (You can have game servers report their nodes status to the master server for this purpose)

For the game servers 1 or 2 threads are enough, (having one thread that only waits for new connections and one that runs the game can simplify things slightly)
[/quote]

I'm not sure if I understand your post. Do you mean that rather than spawning a new thread per client connected, I run some kind of game server node for handle the new client connected ? Or do you mean these game server nodes should be able to handle more than one client ? Because that is what really I was wondering. I can't figure out a way for handling multiple clients without spawning a new thread for each connection.
Maybe I'm a little bad for these things [img]http://public.gamedev.net//public/style_emoticons/default/tongue.png[/img] also thank you very much for your answer too!

Regards.
0

Share this post


Link to post
Share on other sites
[quote name='Sytto' timestamp='1336256848' post='4937678']
[quote name='Washu' timestamp='1336253793' post='4937665']
You should be using, last I recall, the NIO sockets.
[/quote]

thank you very much, didn't know about this!

[quote name='SimonForsman' timestamp='1336253844' post='4937666']
First of all, Transformice is not an MMO, (each game has a very low number of players so you don't need to deal with MMO scale networking for this type of game).

What you should do is have one master server for the "lobby" and then run multiple game servers, They can but doesn't have to be on the same machine but needs different ports and should be handled by their own instance of the server application.

Have the master server keep track of how many instances of the game server each node is running and launch new ones as needed on the least busy node, (You can have game servers report their nodes status to the master server for this purpose)

For the game servers 1 or 2 threads are enough, (having one thread that only waits for new connections and one that runs the game can simplify things slightly)
[/quote]

I'm not sure if I understand your post. Do you mean that rather than spawning a new thread per client connected, I run some kind of game server node for handle the new client connected ? Or do you mean these game server nodes should be able to handle more than one client ? Because that is what really I was wondering. I can't figure out a way for handling multiple clients without spawning a new thread for each connection.
Maybe I'm a little bad for these things [img]http://public.gamedev.net//public/style_emoticons/default/tongue.png[/img] also thank you very much for your answer too!

Regards.
[/quote]

You'll most likely have multiple nodes, each node runs a number of game servers, (each game server hosts 1 game with X players), the game server should have 1 thread for all clients, possibly 2 if you want to use blocking sockets (not really recommended), If you're allowing players to create games themselves you'll have the master server tell the least populated node to launch a new game server, (This can quite easily be done using ssh) before directing the player to it, if players can't create games then you just launch new game servers when the currently running ones are full).

as for the game server handling multiple connections its fairly easy.
loop through the connections and deal with them one at the time. use non blocking sockets to avoid having reads and writes stall the application. (If you use blocking sockets you have to be very careful and always check how much data you can attempt to read before you try to read it and you can't ever write more data than what you can fit in the sockets output buffer)

When reading data you should take all the data that is currently on the socket and place it in your own buffer, extract any complete "packets" from it and handle those, save any data belonging to incomplete packets in the buffer (The next time you read from the same socket you'll get the rest of it.

When writing data you have to do the opposite, keep your own buffer of data to be sent, try to write it all to the socket, check the return value from the write function (it indicates how many bytes was actually written) and remove that number of bytes from your buffer then move on to the next socket in the list.
The important thing is to never stop to wait for a socket.

If your server runs at a fairly low update rate you can increase the size of the recieve buffer using the setRecieveBufferSize method (allthough its only a hint to the underlying implementation, if you have a problem with it filling up and can't increase it enough you might benefit by moving the socket reads to their own thread to get data to your own buffers faster. (Your own buffers aren't limited in size by the platform in the same way) but usually its not necessary (a game shouldn't generate that much data on a single socket) Edited by SimonForsman
0

Share this post


Link to post
Share on other sites
Standard non-NIO sockets do not have a non-blocking write (OutputStream may block). Only workaround here is a thread pool, which can, in worst case, grow to same size as number of sockets.

That alone makes it intractable.

They also lack select()-like timeout functionality, so they must be constantly polled for both, read and write.

NIO has its own warts, but manages to workaround these limitations. NIO is mostly integrated in old interfaces these days, so it's slightly easier to use, at least for basic functionality.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0