Java ServerSocket connecting multiple clients using threads on server

I tried to learn java by myself and decide to design a very simple server that will handle a few clients. There are server and client classes. I tried to look for reference online but couldnt find any.

Brief description, server runs, and listen for incoming connections from clients, I can manage to get the server to send the questions to client and client can send back the answer. But, I can't manage to synchronise them, in the sense, I failed to make the server waits fom at least 2 clients are connected to start the game, and I want those clients are in the same game, and they compete each other to get the answer right and tracking the time for the client to answer the question. Additionally, if the game is still running, if there is any new connection from client, instead of making them wait for the game to end, it'll be good if the can observe the current game, i.e server sends questions to them and show them the answer of other clients, but they're not allowed to play.

Any help or idea of how to do that? I attached codes that I've written so far so you can look at it. Thank you very much
ClientTest.java
GameServer.java
GameServerThread.java
Question.java
AnsellAsked:
Who is Participating?
 
mukundha_expertCommented:
you may also add some flags like start to control the trhread
0
 
mukundha_expertCommented:
handle accepting connections in a seperate thread
like in your Gameserver class,


CountDownLatch latch = new CountDownLatch (2);

//Start thread to accept conneciton
                   run(){
                          Socket s = server.accept();
                          latch.countDown();
                          socketlist.add(s);   
                   }   

latch.await();
//start the game.

Open in new window

0
 
mukundha_expertCommented:
for multiple clients to view, you need to add them as observers to your GameServer thread and notify them of the questions and answers.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
AnsellAuthor Commented:
are you saying that I have 2 kinds of threads?
The first one is the thread I already had, and the second one is the one by making GameServer Thread as well?
0
 
mukundha_expertCommented:
yes. in GameServer create a new Thread which will be polling for Socket Connections and adding it to the list of Socket Connections.

in this case, you can use the sample code i had shown in my first comment t ostart the game.
next once the game is started you can broadcast the question to all the Socket Connections available in the list
0
 
AnsellAuthor Commented:
I don't really understand your code. By adding sockets into list, do you imply that to run GameServerThread, I iterate through it?


Iterator it = socketslist.iterator();
while(it.hasNext())
{
   Socket test = (Socket) it.next();
   //some other socket
   Socket messageSocket = null;
   GameServerThread thread = new GameServerThread(test, messageSocket);
   thread.start();

Open in new window

0
 
AnsellAuthor Commented:
should I create some another class to handle that? for example GameHandlerThread? and this class contains code from your example?
0
 
mukundha_expertCommented:
your GameServer class remains as it is.

your GameServer class will have the instance variables -
CountDownLatch latch
List<Socket> socketlist;

now, when you start your GameServer, you create a new instance of Runnable (a new class), which will try accpeting connections.

run(){
                          Socket s = server.accept();
                          GameServer.latch.countDown();
                          GameServer.socketlist.add(s);  
                   }  


------------------------>
after starting this thread,
in GameServer call latch,await(),
this will wait till atleast 2 participants have connected.
Now you can start your GameServerThread - with the first two elements in the list.

-----------

Remeber the initial thread you have created is also still runing. so the new connections will be added to hte socket list (basically observers as you stated).

In your GameServer - all sockets in the SocketList after the first 2 elements are observers. So now you can write a loic to play

hope this helps/


0
 
AnsellAuthor Commented:
So do I need to create a new class? If I have to, how do you access server.accept() in that class? as the SocketServer is located in GameServer
0
 
AnsellAuthor Commented:
can you provide me more example? I'm not really good at Java
0
 
AnsellAuthor Commented:
I tried to implement your example, runnable only accepts the connection once, and doesn't even bring down the count down.
GameServer.java
0
 
mukundha_expertCommented:
you need a infinte loop over this part in the run() method,

while(true)
			  {
                        Socket gameSocket = serverSocket.accept();
                        //Socket messageSocket = serverSocket.accept();
                        latch.countDown();
                        gameSocketsList.add(gameSocket);
                        System.out.println(gameSocket.toString());
			   }

Open in new window

0
 
ccvoCommented:
This tutorial tells you how to write a server for managing multiple clients:
http://java.sun.com/docs/books/tutorial/networking/sockets/clientServer.html
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.