Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
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  
  Multi Threaded Message Server  (Read 3707 times)
0 Members and 1 Guest are viewing this topic.
Offline Xyle
« Posted 2009-01-05 05:45:42 »

I have a main MessServ class which extends thread and listens for connections to the server.
I have a MessConn class which extends thread and listens to messages sent from a client.
I have a MessClient class which will send input to the MessConn which echos it back to the client.

My question is, how do I make it so that if I have multiple clients connected to the server, if 1 client sends a message, the other client will see it? How do I make the 2 connection threads communicate with each other.

Through the Main MessServ?

I cant see how to do that when the MessServ code waits at...
mConn[cCnt] = new MessConn(servSock.accept(),cCnt);

So theres no way for me to get the message from 1 client and send it to the other.

Any help would be greatly appreciated!!

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

Junior Member




...


« Reply #1 - Posted 2009-01-05 12:55:44 »

<yourserversocket>.getInputStream(); <- <yourclientsocket.getOutputStream()
<yourserversocket>.getOutputStream(); -> <yourclientsocket>.getInputStream()
Offline Xyle
« Reply #2 - Posted 2009-01-06 01:22:05 »

Thank you for the reply, and I do appreciate seeing the code I will be needing to make the communication happen, the problem is, If the server is constantly listening for new connections no other code will get executed unless a connection is made, then 1 full loop of the while is made. Do I need to start 1 thread for listening to connections, 1 thread for listening for message from connections that have been made and 1 thread for sending messages?

Hmmm, lets see if I can get that working.

Thanks for the help.

Life is just a game, learn to play!
------------------------------------------
╬-YellzBellz Games!-╬ Cheesy
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Mr_Light

Senior Member




shiny.


« Reply #3 - Posted 2009-01-06 03:10:54 »

there're ways to listen in a non-blockig way...

anyway, this is a much debated topic I sugjest you try searching google and/or this forum for "thread per connection" or "network thread pools" or "network reactors" or if you want java specific have a look at the abundancy of nio networking flowting around here.

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Offline Xyle
« Reply #4 - Posted 2009-01-06 03:53:35 »

Thank you for that,

Non-Blocking meaning un-assigned sockets? I have read about them over the last 3 days, but not really wrapping my head around them. The MainServer, Connection Listener, Connection Creator really isnt working. I'll look into nio more.

All Im looking to write is a Message server as basic as possible...
Server that listens for message, receives message, sends messages.

When I looked at NIO and read other peoples input, it kinda scared me away, hehehe.

Thanks for any help

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

Junior Member


Projects: 2



« Reply #5 - Posted 2009-01-06 21:46:22 »

Have you looked at Blahblahblahh's nio stuff here?

Offline SunshineKiller

Junior Member





« Reply #6 - Posted 2009-03-02 01:29:35 »

I have a main MessServ class which extends thread and listens for connections to the server.
I have a MessConn class which extends thread and listens to messages sent from a client.
I have a MessClient class which will send input to the MessConn which echos it back to the client.

My question is, how do I make it so that if I have multiple clients connected to the server, if 1 client sends a message, the other client will see it? How do I make the 2 connection threads communicate with each other.

Through the Main MessServ?

I cant see how to do that when the MessServ code waits at...
mConn[cCnt] = new MessConn(servSock.accept(),cCnt);

So theres no way for me to get the message from 1 client and send it to the other.

Any help would be greatly appreciated!!


here is a tutorial on sorta what you are trying to accomplish

http://www.ase.md/~aursu/ClientServerThreads.html

i ran into your same problem on how to get the broadcast to go to all clients. Scroll down to the chat server example and you can see how its done.

<b>Check out my Development Blog:</b> <a href="http://www.scottscreations.com">Scotts Creations</a> | <b>Games in Development: </b> <a href="http://mechwarfare.scottscreations.com">Mech Warfare</a> | Mech Warfare: Facebook Edition | Game Master
Offline Xyle
« Reply #7 - Posted 2009-03-03 00:24:36 »

Thanks for the reply. Since the time of the post, I have figured out (with some help) a good multi player base class. The answer for me was the multi threading part and what multi threading actually meant. It opened up a whole new world for me, hehehe.

1 client sends a message, the server side thread of this connection receives it, parses it and then sends it to the actual main server thread, the main server thread then sends it to all clients.

Thanks again for the reply.

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

Junior Member





« Reply #8 - Posted 2009-03-03 05:13:08 »

Xyle quick question, ive got the server part down but how did you get yours to update so it keeps updating and the user can still type? thats where i am at now everything i try either freezes it or im just a noob at this. haha did you make your client a thread and run a while loop for the reading from the server?

<b>Check out my Development Blog:</b> <a href="http://www.scottscreations.com">Scotts Creations</a> | <b>Games in Development: </b> <a href="http://mechwarfare.scottscreations.com">Mech Warfare</a> | Mech Warfare: Facebook Edition | Game Master
Offline Mr_Light

Senior Member




shiny.


« Reply #9 - Posted 2009-03-03 10:34:13 »

see http://www.java-gaming.org/topics/networked-gaming/19345/30/view.html

But do note that it focuses on simplicity - so it's not production like code.
For bonus points read http://tech.puredanger.com/2009/02/28/java-7-transferqueue/

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Xyle
« Reply #10 - Posted 2009-03-04 05:02:46 »

Xyle quick question, ive got the server part down but how did you get yours to update so it keeps updating and the user can still type? thats where i am at now everything i try either freezes it or im just a noob at this. haha did you make your client a thread and run a while loop for the reading from the server?

Are you using a console window? I created applets in my website for the client side and a swing app on the server side to add users when they open the app, display any text that the user types, and display game data. The swing server app also contains a message box so I can send messages via the server to all connected clients.

Basically my server side is running from a console at startup, it then launches the main class which contains the swing app, the Listener Class which listens for new connections constantly, and as a new connection is created by a user joining, a clientHandler thread is created. The clientHandler thread is constantly listening to the client for any input or it will send the client server messages.

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

Junior Member





« Reply #11 - Posted 2009-03-04 17:51:57 »

i got it Smiley, no my client is GUI just needed a thread to do the updating in a class, within the GUI class.

<b>Check out my Development Blog:</b> <a href="http://www.scottscreations.com">Scotts Creations</a> | <b>Games in Development: </b> <a href="http://mechwarfare.scottscreations.com">Mech Warfare</a> | Mech Warfare: Facebook Edition | Game Master
Offline Xyle
« Reply #12 - Posted 2009-03-05 01:58:42 »

So your using 2 threads in your client? I didnt have to do it that way. I am still able to type in the chat box while the client checks for messages from the server. Heres my the Run() method from my client class...

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
   public void run(){
      // Set Thread Priority to minimum
     Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
     
      //Send server connection message
      if(pType.equals("0")){
          out.println("00"+p1Name+",None,"+gName+"0"); //Let Server know if hosting
      }else{
          out.println("01"+p2Name+","+p1Name+",Unknown1"); //Let Server know if joining
         pWin=0; //Set pwin to zero so player 2 can begin playing
      }

      try{
         while(true){
            /////////////////////////////////
           //Get Server message
           if(in.ready()){
               fromServ = in.readLine();
               if(fromServ != ""){
                  parseMess(fromServ);
               }
            }
           
            /////////////////////////////////
           repaint();
           
            /////////////////////////////////
           try {
               t.sleep(10);
            } catch (InterruptedException e) {
               System.out.println(e);
               stop();
            }
         }
      }
      catch(IOException e){
         System.out.println("Lost Connection to server.");
      }
     
      stop();

   }


Basically my client implements Keylistener and Mouselistener and is reactionary only. It will either react to a server message, a mouse click or a keypress. Thats why theres no other method call in the run, other than listen and repaint.



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

Junior Member





« Reply #13 - Posted 2009-03-05 03:53:09 »

oh no my gui isnt a thread, i just have one thread.

<b>Check out my Development Blog:</b> <a href="http://www.scottscreations.com">Scotts Creations</a> | <b>Games in Development: </b> <a href="http://mechwarfare.scottscreations.com">Mech Warfare</a> | Mech Warfare: Facebook Edition | Game Master
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (12 views)
2014-08-28 18:26:30

CopyableCougar4 (24 views)
2014-08-22 19:31:30

atombrot (37 views)
2014-08-19 09:29:53

Tekkerue (30 views)
2014-08-16 06:45:27

Tekkerue (29 views)
2014-08-16 06:22:17

Tekkerue (18 views)
2014-08-16 06:20:21

Tekkerue (27 views)
2014-08-16 06:12:11

Rayexar (65 views)
2014-08-11 02:49:23

BurntPizza (41 views)
2014-08-09 21:09:32

BurntPizza (33 views)
2014-08-08 02:01:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org 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‑gaming.org
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!