Monday, 24 September 2018

Multithreading Program in Java

Write down a complete multithreaded connection oriented socket based server, that'll wait for the connections from clients, after the establishment of connection with client, it will receive a string word from the client and send it back the meaning of that word from the array stored on server to client. 


Multithreaded Server Starter: 

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(7777);
  } 
  catch (IOException e) 
  {
   System.err.println("Could not listen on port: 7777.");
   System.exit(-1);
  }
  while (listening){
  new P10_5( serverSocket.accept()).start();
  System.out.println("Accepted connection from client");}
  serverSocket.close();
 }
}

Multithreaded Server:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public final class P10_5 extends Thread {
    private final Socket socket;
    public P10_5(Socket socket) {
        setName("P10_5");
        this.socket = socket;
    }
    public void run() {
        BufferedReader in = null;
        PrintWriter out = null;
        int n = 3;
        String request;
        try {
            // Get the input and output streams
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            out = new PrintWriter(socket.getOutputStream(), true);
            class dic {
                String word, mean;
                dic(String w, String m) {
                    word = w;
                    mean = m;
                }
            }
            dic d[] = new dic[4];
            d[0] = new dic("coincidence", "itifaq");
            d[1] = new dic("thanks", "shukriya");
            d[2] = new dic("problem", "masla");
            d[3] = new dic("night", "raat");
            while ((request = in.readLine()) != null) {
                for (int i = 0; i < n; i++) {
                    if (request.equals(d[i].word)) {
                        out.println(d[i].mean);
                        break;
                    }
                    if (i == n - 1) {
                        out.println("No knowledge for " + request);
                        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;
import java.util.Scanner;
public final class P10_5Client {
    private void process() {
        Socket socket = null;
        BufferedReader in = null;
        PrintWriter out = null;
        try {
            /* Establish a TCP refrenced to  the server running locally on the port number 7777.
             * You can write "localhost" instead of "127.0.0.1".
             * If the server is running on a foriegn computer, replace "127.0.0.1" with the server's IP address or hostname
             */
            socket = new Socket("127.0.0.1", 7777);
            // Get the input and output streams
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            out = new PrintWriter(socket.getOutputStream(), true);
            Scanner user = new Scanner(System.in); // Scanning for user input
            String input;
            while (true) {
                System.out.print("Enter String: ");
                input = user.next(); // Hold the input from the user
                if (input.equals("end")) {
                    System.out.println("You ended...");
                    break;
                } else {
                    out.println(input); // Send it to the server
                    System.out.println("Name: " + input + " Meaning: " + in.readLine());
                }
            }
        } 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 P10_5Client().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...