|
| 1 | +/* |
| 2 | + * Has 2 classes one class: |
| 3 | + * Server to create a Server |
| 4 | + * CLientHandler ot handle clients using multithreading |
| 5 | + * |
| 6 | + * Life Cycle of multithreads: |
| 7 | + * New |
| 8 | + * - New Thread created, is in new state, the code has not been run yet |
| 9 | + * Active |
| 10 | + * -(New thread created when invokes start(), it moves from new to active state) |
| 11 | + * -Runnable: Ready to run and is moved to runnable state, ready to run at anytime |
| 12 | + * -Running: Thread gets CPU, moves from runnable to running state. (Generally from runnable to running and again back to runnable) |
| 13 | + * Blocked / Writing |
| 14 | + * - Whenever a thread is inactive for a span of time(not permanently) then the thread is either in blocked or waiting state |
| 15 | + * Timed Waiting |
| 16 | + * -Sometimes waiting for leads to starvation, For example, a thread (its name is A) has entered the critical section of a code and is not willing to leave that critical section. In such a scenario, another thread (its name is B) has to wait forever, which leads to starvation. To avoid such scenario, a timed waiting state is given to thread B. Thus, thread lies in the waiting state for a specific span of time, and not forever. A real example of timed waiting is when we invoke the sleep() method on a specific thread. The sleep() method puts the thread in the timed wait state. After the time runs out, the thread wakes up and start its execution from when it has left earlier. |
| 17 | + * -sleep() normally used for timed waiting |
| 18 | + * Terminated |
| 19 | + * - Multiple ways that a thread can terminate |
| 20 | + * - When a thread finishes its job, it exits normally |
| 21 | + * - Abnormal Termination: |
| 22 | + * - Occurs when some unusual events such as unhandled exception or segmentation fault |
| 23 | + */ |
| 24 | +import java.io.*; |
| 25 | +import java.net.*; |
| 26 | + |
| 27 | + |
| 28 | +// Server class |
| 29 | +class Server { |
| 30 | + public static void main(String[] args) { |
| 31 | + ServerSocket server = null; |
| 32 | + |
| 33 | + try { |
| 34 | + |
| 35 | + // server is listening on port "1234" |
| 36 | + server = new ServerSocket(1234); |
| 37 | + server.setReuseAddress(true); |
| 38 | + |
| 39 | + |
| 40 | + // running infinite loop for geting client request |
| 41 | + while(true) { |
| 42 | + |
| 43 | + // returns new socket |
| 44 | + Socket client = server.accept(); |
| 45 | + // returns remote IP address to which the socket is connected or returns null is the socket is not connected |
| 46 | + InetAddress addy = client.getInetAddress(); |
| 47 | + // returns raw IP address in a string format |
| 48 | + String remoteIp = addy.getHostAddress(); |
| 49 | + |
| 50 | + |
| 51 | + // Displaying that new client is connected to server |
| 52 | + System.out.println("New client connected" + addy + remoteIp); |
| 53 | + |
| 54 | + // create a new thread object |
| 55 | + ClientHandler clientSock = new ClientHandler(client); |
| 56 | + |
| 57 | + // This thread will handle the client seperately |
| 58 | + new Thread(clientSock).start(); |
| 59 | + } |
| 60 | + } catch (IOException e) { |
| 61 | + e.printStackTrace(); |
| 62 | + } |
| 63 | + finally { |
| 64 | + if (server != null) { |
| 65 | + try { |
| 66 | + server.close(); |
| 67 | + } catch (IOException e) { |
| 68 | + e.printStackTrace(); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + // ClientHandler class |
| 75 | + private static class ClientHandler implements Runnable { |
| 76 | + |
| 77 | + private final Socket clientSocket; |
| 78 | + |
| 79 | + // Construtor |
| 80 | + public ClientHandler(Socket socket) { |
| 81 | + this.clientSocket = socket; |
| 82 | + } |
| 83 | + |
| 84 | + public void run() { |
| 85 | + PrintWriter out = null; |
| 86 | + BufferedReader in = null; |
| 87 | + |
| 88 | + try{ |
| 89 | + |
| 90 | + // get the outputstream of client |
| 91 | + out = new PrintWriter(clientSocket.getOutputStream(), true); |
| 92 | + |
| 93 | + // get the inputStream of client |
| 94 | + in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); |
| 95 | + |
| 96 | + String line; |
| 97 | + while ((line = in.readLine()) != null) { |
| 98 | + |
| 99 | + // Writing the recieved message from client |
| 100 | + System.out.printf(" Sent from the client: %s\n", line); |
| 101 | + out.println(line); |
| 102 | + } |
| 103 | + } catch (IOException e) { |
| 104 | + e.printStackTrace(); |
| 105 | + } |
| 106 | + finally { |
| 107 | + try { |
| 108 | + if (out != null) { |
| 109 | + out.close(); |
| 110 | + } |
| 111 | + if (in != null) { |
| 112 | + in.close(); |
| 113 | + } |
| 114 | + } |
| 115 | + catch (IOException e) { |
| 116 | + e.printStackTrace(); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + } |
| 122 | + |
| 123 | +} |
0 commit comments