Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (755)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (842)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Single thread for Multi User?  (Read 1775 times)
0 Members and 1 Guest are viewing this topic.
Offline Xyle
« Posted 2009-04-04 16:08:19 »

Currently I use 1 thread for each client that connects to a game or chat system. I just realized how easier it would be for me to maintain code, add new features or to update the systems if I used 1 thread for communicating with the clients instead of 1 thread for each client.

Currently I use this code to connect each client to the main program...
public void run(){ 
         //Find next free Conn number
         nt1[Conn] = new NetConn(serverSocket.accept(), Conn);
      }catch(IOException e){

      try {
      } catch (InterruptedException e) {

This creates multiple threads and assigns each a variable I can use in the main program for passing data back and forth.

If I wanted to use 1 thread, this try loop would catch a connecting client and open a socket between that client and the existing thread that is already there. The problem is, I have no idea how to do this. Anyone have any pointers on this?

Any help would be greatly appreciated!

Life is just a game, learn to play!
╬-YellzBellz Games!-╬ Cheesy
Offline Riven

« JGO Overlord »

Medals: 1340
Projects: 4
Exp: 16 years

Hand over your head.

« Reply #1 - Posted 2009-04-04 17:09:03 »

When you want your networking in one thread, you're only option is java.nio (or some framework based on that).

NIO is basically a can of worms, compared to plain old Socket/ServerSocket.

I'd highly recommend you not to switch to NIO, maybe look around for more highlevel frameworks (like JGN or MINA). IMHO it's best to just make it work with and learn from it.

It all those threads make you feel uncomfortable, simply pass all your received data in 1 'global inbound queue', that is drained by 1 thread. Also put all data you intend to write in an 'outbound queue' per connection.

Now you have the benefits of an easy to grasp networking API, with the simplicity of 1 thread that handled all networking data.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline Xyle
« Reply #2 - Posted 2009-04-05 03:31:03 »

Thank you very much! Exactly the information I needed to know. I really appreciate the help there!

Thank you!

Life is just a game, learn to play!
╬-YellzBellz Games!-╬ Cheesy
Pages: [1]
  ignore  |  Print  

DesertCoockie (36 views)
2018-05-13 18:23:11

nelsongames (82 views)
2018-04-24 18:15:36

nelsongames (74 views)
2018-04-24 18:14:32

ivj94 (755 views)
2018-03-24 14:47:39

ivj94 (85 views)
2018-03-24 14:46:31

ivj94 (626 views)
2018-03-24 14:43:53

Solater (101 views)
2018-03-17 05:04:08

nelsongames (182 views)
2018-03-05 17:56:34

Gornova (408 views)
2018-03-02 22:15:33

buddyBro (1068 views)
2018-02-28 16:59:18
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!