Java - TCP an example - MD, manager and employees

Question:

-> Unidirectional data transfer from the Managing Director to the Manager
-> Broadcast from the Manager to the Employees
-> Public Chat between Employees


ManagingDirector.java

import java.io.*;
import java.net.*;

public class ManagingDirector {
    ServerSocket serverSocket = null;
    Socket socketToManager = null;
    PrintWriter toManager = null;
    BufferedReader fromKeyboard = null;

    public ManagingDirector() {
        try {
            serverSocket = new ServerSocket(9999);
            System.out.println("Waiting for a connection...");
            socketToManager = serverSocket.accept();
            System.out.println("Connection accepted...");

            fromKeyboard = new BufferedReader(new InputStreamReader(System.in));
            toManager = new PrintWriter(socketToManager.getOutputStream());

            while(true) {
                String message = fromKeyboard.readLine();
                toManager.println(message);
                toManager.flush();
            }

        } catch(Exception exp) {
            exp.printStackTrace();
        }
    }   

    public static void main(String[] args) {
        new ManagingDirector();
    }
}



Manager.java

import java.io.*;
import java.net.*;
import java.util.*;

public class Manager implements Runnable {
    ArrayList clientHandlerList = new ArrayList();
    ServerSocket serverSocket = null;
    Socket socketToMD = null;
    BufferedReader readFromMD = null;

    public Manager() {
        try {
            socketToMD = new Socket("127.0.0.1", 9999);
            readFromMD = new BufferedReader(
                new InputStreamReader(
                socketToMD.getInputStream()));

            serverSocket = new ServerSocket(9998);       
           
            new Thread(this).start();
            new Thread(new BroadCastServer(this)).start();

            while(true) {
                Socket socket = serverSocket.accept();
                ClientHandler clientHandler = new ClientHandler(socket, this);
                clientHandlerList.add(clientHandler);

                new Thread(clientHandler).start();               
            }
        } catch(Exception exp) {
            exp.printStackTrace();
        }
    }   

    public void run() {
        System.out.println("Thread started");
        try {
            while(true) {
                String message = readFromMD.readLine();
                System.out.println(message);
            }
        } catch(Exception exp) {
            exp.printStackTrace();
        }
    }

    public ArrayList getClientHandlers() {
        return clientHandlerList;
    }

    public void broadcast(String message, ClientHandler sourceHandler) {
        try {

            for(int i = 0 ; i < clientHandlerList.size() ; i++) {
                ClientHandler handler = (ClientHandler) clientHandlerList.get(i);
                if(handler != sourceHandler) {
                    handler.writeToClient.println(message);
                    handler.writeToClient.flush();
                }
            }
        } catch(Exception exp) {
            exp.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new Manager();
    }
}


ClientHandler.java

import java.io.*;
import java.net.*;

public class  ClientHandler implements Runnable {
    Manager manager = null;
    Socket socketToClient = null;
    private BufferedReader readFromClient = null;
    private BufferedReader readFromKeyboard = null;
    public PrintWriter writeToClient = null;

    public ClientHandler (Socket socketToClient, Manager manager) {
        try {
            this.socketToClient = socketToClient;
            this.manager = manager;
            readFromKeyboard = new BufferedReader(new InputStreamReader(System.in));
            readFromClient = new BufferedReader(
                new InputStreamReader(
                socketToClient.getInputStream()));
            writeToClient = new PrintWriter(socketToClient.getOutputStream());

            new Thread(this).start();
           
        } catch(Exception exp) {
            exp.printStackTrace();
        }
    }

    public void run() {
        try {
            while(true) {
                String message = readFromClient.readLine();
                manager.broadcast(message, this);
            }
        } catch(Exception exp) {
            exp.printStackTrace();
        }
    }
}


BroadCastServer.java

import java.io.*;
import java.net.*;
import java.util.*;

public class BroadCastServer implements Runnable {
    private BufferedReader readFromKeyboard = null;
    Manager manager = null;

    public BroadCastServer (Manager manager) {
        this.manager = manager;
        readFromKeyboard = new BufferedReader(new InputStreamReader(System.in));
    }

    public void run() {
        try {
            while(true) {
                String message = readFromKeyboard.readLine();
                ArrayList clientHandlerList = manager.getClientHandlers();

                for(int i = 0 ; i < clientHandlerList.size() ; i++) {
                    ClientHandler handler = (ClientHandler) clientHandlerList.get(i);
                    handler.writeToClient.println(message);
                    handler.writeToClient.flush();
                }
            }

        } catch(Exception exp) {
            exp.printStackTrace();
        }
    }
}


Employee.java

import java.io.*;
import java.net.*;

public class Employee implements Runnable {
    Socket socket = null;
    BufferedReader readFromClient = null;
    BufferedReader readFromServer = null;
    PrintWriter writeToServer = null;

    public Employee() {
        try {
            socket = new Socket("localhost", 9998);

            readFromServer = new BufferedReader(
                new InputStreamReader(socket.getInputStream()));
            writeToServer = new PrintWriter(socket.getOutputStream());
            readFromClient = new BufferedReader(
                new InputStreamReader(System.in));

            new Thread(this).start();

            while(true) {
                String message = readFromClient.readLine();
                writeToServer.println(message);
                writeToServer.flush();
            }
        } catch(UnknownHostException exp) {
            exp.printStackTrace();
        } catch(IOException exp) {
            exp.printStackTrace();
        }
    }

    public void run() {
        try {
            while(true) {
                String message = readFromServer.readLine();
                System.out.println(message);
            }
        } catch(IOException ioExp) {
            ioExp.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new Employee();
    }   
}


Post a Comment

1 Comments

  1. This is a nice article..
    Its very easy to understand ..
    And this article is using to learn something about it..

    c#, dot.net, php tutorial

    Thanks a lot..!

    ReplyDelete