Fazer
Fazer

Reputation: 89

Two clients on a single thread Java

I am writing a program in which I have a server that is continuously listening for requests (like most servers). I want it to create a new thread each time I get a client connection(I'm done with this part), and I want it to add the client immediately after it, to the same thread. So I'll have two clients connected to the same thread.

Something along the lines of:

.....
try(ServerSocket serversocket = new ServerSocket();
    Socket client1 = serversocket.accept();
    Socket client2 = serversocket.accept();){

    //spawn new thread with sockets to client1 and client2

Is this possible? Are there any alternatives to this? Would it be possible to get two different threads to converse with each other?

Would it be possible to create a new server socket from the thread extending to a client?

...
try(ServerSocket serversocket = new ServerSocket();
    Socket client1 = serversocket.accept();){
    //spawn thread
}

And now in the thread:

....
try(ServerSocket serversocket2 = new ServerSocket();
    Socket client2 = serversocket2.accept();){
    //exchange information between two clients and do other stuff
}

Any ideas? All help is greatly appreciated.

Upvotes: 1

Views: 1657

Answers (2)

l1zZY
l1zZY

Reputation: 100

How about you make an ArrayList and you could make a ClientThread class or something and it's constructor takes a Socket argument. Then when you get a new connection, do something like

Socket clientSocket = serverSocket.accept();
arrayList.add(new ClientThread(clientSocket));

Upvotes: 2

shardulc
shardulc

Reputation: 301

The main reason why we use different threads for different clients is so that the clients can be handled individually, without blocking up other clients who may be wanting different things. If you wanted to extend your program afterwards, say, to have different difficulty levels for clients, you would need a different setup.

When a client connects, a new thread is started and a flag is set in a flag table on the server, saying "Client available" (maybe with more info about the client; eg. difficulty level). Then the server tells the client that a suitable opponent is being waited for, and goes back into waiting. When a suitable opponent does connect, the corresponding flag contains references to both clients and now you have two threads with suitable clients.

Assuming the games are turn-based and not real-time, the game can be played as follows: at the start, each client requests the server for the socket corresponding to the other client, which the server gives it, looking up the reference in the flag table above. Then, the clients decide on the game, options, etc. between themselves by communicating over the socket. Once both the clients know what the empty board looks like, they start playing; they send each other changes on the board, like 'knight to d5'. They handle finishing on their own, or maybe keeping a record on the server.

In the code, you need to use synchronized methods and thread-safe objects whenever dealing with separate threads trying to communicate. You could read further about it at the Java Tutorials page.

Upvotes: 1

Related Questions