Use TCP sockets to construct a relay type of server.

Ques: Use TCP sockets to construct a relay type of server. The server maintains the information of clients and if any client requests the server, it replies to the client who connected just after the requesting client. Say clients abc, def and hij connected in this order. If abc requests any data to the server, then server will
reply to def.
To make it simple, you can restrict the clients to always request in an order. (Assumptions can be made
for any particular scenerio)

Source Code:

Server:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

public class Server {
    private static ArrayList<Socket> list = new ArrayList<>();
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(18475);
        System.out.println("Waiting For Clients");
        while (true){
            Socket client = serverSocket.accept();
            new Thread(()->{
                try {
                    list.add(client);
                    System.out.println("Client Connected "+list.size()+" from "+client.getPort());
                    handleClients(client,list.size()-1);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }

    private static void handleClients(Socket client, int index) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream()));
        while (true) {
            String x = br.readLine();
            System.out.println("Received "+x+" from Client "+(index+1));
            OutputStream os;
            if (index < list.size() - 1) {
                os = list.get(index + 1).getOutputStream();
            } else os = list.get(0).getOutputStream();
            os.write((x + "\n").getBytes());
        }
    }
}

Client:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

public class Clients {

    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("127.0.0.1",18475);
        System.out.println("Connected To Server");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        OutputStream os = socket.getOutputStream();
        new Thread(()->{
            while (true) {
                try {
                    System.out.println("Write Message:");
                    String x = br.readLine();
                    os.write((x+"\n").getBytes());
                    os.flush();
                    System.out.println("Sent : "+x);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        while (true) {
                try {
                    String x = in.readLine();
                    System.out.println("Received : "+x);
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }
}


Output:
Use TCP sockets to construct a relay type of server.

Comments