Comunicação cliente-servidor-cliente usando Sockets

Eu estou construindo uma pequena aplicação de bate-papo em que o cliente A quer enviar algo para o cliente C com o servidor B no meio. Em primeiro lugar, esta é uma abordagem correta para o problema? Eu sou capaz de enviar e receber dados de e para um servidor, mas é limitado apenas ao cliente.Por exemplo, se o Cliente A envia dados para o servidor B e o cliente C está enviando dados para o servidor B, então eu posso enviar dados de volta para A e C apenas como um servidor de eco. Mas o que eu quero é encaminhar dados provenientes do cliente A para o cliente C via servidor B.

O seguinte é o código do servidor:

<code>public class Server {
    public static void main(String[] args) {
        int port = 666; //random port number
        try {
            ServerSocket ss = new ServerSocket(port);
            System.out.println("Waiting for a client....");

            System.out.println("Got a client :) ... Finally, someone saw me through all the cover!");
            System.out.println();
            while(true) {
                Socket socket = ss.accept();

                SSocket sSocket = new SSocket(socket);
                Thread t = new Thread(sSocket);
                t.start();
                System.out.println("Socket Stack Size-----"+socketMap.size());
            }
        }
        catch (Exception e) { }
    }
}

class SSocket implements Runnable {
    private Socket socket;

    public SSocket(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            InputStream in = socket.getInputStream();
            OutputStream out = socket.getOutputStream();

            DataInputStream dIn = new DataInputStream(in);
            DataOutputStream dOut = new DataOutputStream(out);

            String line = null;
            while (true) {
                line = dIn.readUTF();
                System.out.println("Recievd the line----" + line);
                dOut.writeUTF(line + " Comming back from the server");
                dOut.flush();
                System.out.println("waiting for the next line....");    
            }
        }
        catch (Exception e) { }
    }
}
</code>

O código do cliente é:

<code>public class Client {
    public static void main(String[] args) {
        int serverPort = 666;

        try {
            InetAddress inetAdd = InetAddress.getByName("127.0.0.1");
            Socket socket = new Socket(inetAdd, serverPort);

            InputStream in = socket.getInputStream();
            OutputStream out = socket.getOutputStream();

            DataInputStream dIn = new DataInputStream(in);
            DataOutputStream dOut = new DataOutputStream(out);

            BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));

            System.out.println("Type in something and press enter. Will send it to the server and tell ya what it thinks.");
            System.out.println();

            String line = null;
            while (true) {
                line = keyboard.readLine();
                System.out.println("Wrinting Something on the server");
                dOut.writeUTF(line);
                dOut.flush();

                line = dIn.readUTF();
                System.out.println("Line Sent back by the server---" + line);
            }
        }
        catch (Exception e) { }
    }
}
</code>

questionAnswers(3)

yourAnswerToTheQuestion