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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

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
mukundha_expertCommented:
you may also add some flags like start to control the trhread
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.