Multi Threaded Message Server

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!!

.getInputStream(); <- <yourclientsocket.getOutputStream()
.getOutputStream(); -> .getInputStream()

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.

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.

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

Have you looked at Blahblahblahh’s nio stuff here?

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.

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.

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?

see http://www.java-gaming.org/index.php/topic,19345.30.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/

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.

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

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…


	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.

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