Connect two client Sockets into one GameHandler

Viewed 36

Hello I am creating a server for the turn based game. I achieved connecting people to the server in the separated threads and they can send messages to server and recieve answer from server. However, now I need to connect two people to one room and make them communicate with each other (I do not know if through server or just somehow connect them with each other. These people connect to server and can communicate with it when every of them has one clientHandler. However, when I make a clientHandler which handles two sockets it does not work. Does somebody know how to make handler (room on a server) in which the pair of clients could send message to each other?

public void handlePVPGame(Socket playerWhite, Socket playerBlack, int pointerId) throws IOException {
    int firstPlayerId = pointerId;
    pointerId +=1;
    int secondPlayerId = pointerId;

    ClientHandler clientHandlerWhite = new ClientHandler(playerWhite);
    ClientHandler clientHandlerBlack = new ClientHandler(playerBlack);
    new Thread(clientHandlerWhite).start();
    new Thread(clientHandlerBlack).start();
//        This does not work
//        TwoClientsHandler twoClientsHandler = new TwoClientsHandler(playerWhite, playerBlack);
//        new Thread(twoClientsHandler).start();

    
 //       ClientHandlerClass
public class ClientHandler implements Runnable {
    private final Socket clientSocket;
    private PrintWriter printWriter;
    private BufferedReader bufferedReader;

    private InputStreamReader inputStreamReader;

    public ClientHandler(Socket socket)
    {
        this.clientSocket = socket;
    }



    @Override
    public void run() {

    try {
        printWriter = new PrintWriter(clientSocket.getOutputStream());
        inputStreamReader = new InputStreamReader(clientSocket.getInputStream());
        bufferedReader = new BufferedReader(inputStreamReader);

        String receivedMessage;
        while ((receivedMessage = bufferedReader.readLine())!= null){
            System.out.printf(
                    " Sent from the client: %s\n",
                    receivedMessage);
            printWriter.println("Welcome to server");
            printWriter.flush();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    }

}


//       Class which does not work
public class TwoClientsHandler implements Runnable{
    private final Socket playerWhite;
    private PrintWriter printWriterWhite;
    private InputStreamReader inputStreamReaderWhite;
    private BufferedReader bufferedReaderWhite;

    private final Socket playerBlack;
    private PrintWriter printWriterBlack;
    private InputStreamReader inputStreamReaderBlack;
    private BufferedReader bufferedReaderBlack;



    public TwoClientsHandler(Socket playerWhite,Socket playerBlack) {
        this.playerWhite = playerWhite;
        this.playerBlack = playerBlack;

    }

    @Override
    public void run() {
        try {
            printWriterWhite = new PrintWriter(playerWhite.getOutputStream());
            inputStreamReaderWhite = new InputStreamReader(playerWhite.getInputStream());
            bufferedReaderWhite = new BufferedReader(inputStreamReaderWhite);

            printWriterBlack = new PrintWriter(playerBlack.getOutputStream());
            inputStreamReaderWhite = new InputStreamReader(playerBlack.getInputStream());
            bufferedReaderWhite = new BufferedReader(inputStreamReaderWhite);

            String receivedMessage;
            while ((receivedMessage = bufferedReaderWhite.readLine())!= null){
                System.out.printf(
                        " Sent from the client: %s\n",
                        receivedMessage);
                printWriterWhite.println("Welcome to server");
                printWriterWhite.flush();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}


 
1 Answers

I suggest sticking to a one-thread one-client approach. It is a much simpler solution requiring the creation of the protocol according to which users will send the messages. Say, your game is chess. Mike opens the application:

  1. You create a separate thread for Mike and give him some UUID
  2. You assign this UUID to the specific room
  3. You keep UUID as a key and Room as the value in the ConcurrentHashMap
  4. Room has an array of UUIDs and potentially whose the next turn

In the Server thread, you assign to Mike UUID 5 and room 3. Now when Mike wants to move his bishop to D5, he sends for example 5BD5. Then:

  1. You parse this message according to your protocol, so you extract UUID from the first character of the message -- 5, chess piece -- B, being bishop and square of the chessboard -- D5.
  2. Having UUID extracted you immediately have a room and from the room you have the second player whom potentially you'd like to pass the messages from Mike.

This way you have perfectly separated rooms from one another. The server will convey the messages between the users only within the same room.

You can have several threads in one socket using Java NIO Selector, however, for your purpose above solution seems to be simpler.

Related