Monday 24 September 2018

Write a Mulitithreaded TCP Server which will send system data and time to clinet.

Multithreaded TCP server:

import java.io.*;
import java.net.*;
import java.util.*;
public class MultiServe implements Runnable
{
 private ServerSocket ss;
 public static void main(String args[]) throws Exception
 {
  MultiServe m = new MultiServe();
  m.go();
 }
 public void go() throws Exception
 {
  ss = new ServerSocket(DayClient1.DAYTIME_PORT, 5);
  Thread t1 = new Thread(this, "1");
  Thread t2 = new Thread(this, "2");
  Thread t3 = new Thread(this, "3");
  t1.start(); t2.start(); t3.start();
 }
 public void run()
 {
  Socket s = null;
  BufferedWriter out = null;
  String myname = Thread.currentThread().getName();
  for (;;)
  {
   try
   {
    System.out.println("thread " + myname + " about to accept..");
    s = ss.accept();
    System.out.println("thread " + myname + " accepted a connection");
    out = new BufferedWriter(
    new OutputStreamWriter(s.getOutputStream()));
    out.write(myname + " " + new Date());
    Thread.sleep(10000);
    out.write("\n");
    out.close();
   }
   catch (Exception e)
   {
    e.printStackTrace();
   }
  }
 }
}

Client:

//A TCP Client for the Daytime service
import java.net.*;
import java.io.*;
public class DayClient1 {
public static final int DAYTIME_PORT = 2012;
String host;
Socket s;
public static void main(String args[]) throws
IOException {
DayClient1 that = new DayClient1("192.681.158.1");
that.go();
}
public DayClient1(String host) {
this.host = host;
}
public void go() throws IOException {
s = new Socket(host, DAYTIME_PORT);
BufferedReader i = new BufferedReader(
new InputStreamReader(s.getInputStream()));
System.out.println(i.readLine());
i.close();
s.close();
}
}

Output:


‘OR’

Server:

import java.io.*;
import java.net.*;
public final class TcpServerStarter 
{
 public static void main(String[] args) throws IOException 
 {
  ServerSocket serverSocket = null;
  boolean listening = true;
  try 
  {
   serverSocket = new ServerSocket(2012);
  } 
  catch (IOException e) 
  {
   System.err.println("Could not listen on port: 2012.");
   System.exit(-1);
  }
  while (listening)
  new ConnectionHandler( serverSocket.accept()).start();
  serverSocket.close();
 }
}

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Date;
public final class ConnectionHandler extends Thread {
    private Socket socket;
    public ConnectionHandler(Socket socket){
          setName("ConnectionHandler");
          this.socket = socket;
    }
    public void run(){
          BufferedReader in = null;
          PrintWriter out = null;
          String request;
          try{
                // Get the input and output streams
                in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                out = new PrintWriter(socket.getOutputStream(), true);

                while( (request = in.readLine()) != null){

                      // Deliver the current date and time if the client sends GET_TIME
                      if(request.equals("GET_TIME"))
                            out.println(new Date().toString());

                      // Deliver the JRE version if the client sends GET_JAVA_VERSION
                      else if(request.equals("GET_JAVA_VERSION"))
                            out.println(System.getProperty("java.version"));

                      // Exit the loop if the client sends CLOSE
                      else if(request.equals("CLOSE"))
                            break;
                }
          }
          catch(IOException e){
                e.printStackTrace();
          }
          finally{
                try{
                      if(socket != null)
                  socket.close(); // Close the socket (closing the socket also closes the input and output streams)
                }
                catch(IOException e){
                      e.printStackTrace();
                }
          }
    }
}

Client:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public final class TcpClient {
    private void process() {
          Socket socket = null;
          BufferedReader in = null;
          PrintWriter out = null;
          try{
                /* Establish a TCP connection with the server running locally on the port number 2012.
                 * You can write "localhost" instead of "192.681.158.1".
                                  */
                socket = new Socket("192.681.158.1", 2012);

                // Get the input and output streams
                in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                out = new PrintWriter(socket.getOutputStream(), true);

                // Send a GET_TIME request and print the response to the standard output
                out.println("GET_TIME");
                System.out.println(in.readLine());

                // Send a GET_JAVA_VERSION request and print the response to the standard output
                out.println("GET_JAVA_VERSION");
                System.out.println(in.readLine());

                // Send a CLOSE request to the server to end communication
                out.println("CLOSE");
          }
          catch(IOException e){
                e.printStackTrace();
          }
          finally{
                try{
                      if(socket != null)
                            socket.close(); // Close the socket (closing the socket also closes the input and output streams)
                }
                catch(IOException e){
                      e.printStackTrace();
                }
          }
    }
    public static void main(String[] args){
          new TcpClient().process();
    }
}

Output:


No comments:

Post a Comment

If you have any doubt please let me know

Methods in JAVA Language

 In general, a way may be thanks to perform some task. Similarly, the tactic in Java may be a collection of instructions that performs a sel...