Answer to Question #274052 in Computer Networks for Muhammad Ahsan

Question #274052

<span lang="EN-GB">Main Objective of the exercise</span>

 

This is another client-server application. It builds on echoserver and has a more complex protocol based on the well-known series of (very bad) jokes. It is based on the standard Java introduction to distributed systems. The objective is to get it the simple client-server application working on the same machine and then on two different ones. Once you’ve done that the next step is to run a multi-client server version!

 


<span lang="EN-GB">What to do</span> – Single Client-Server Knock Knock Application

 

The following diagram shows the networked application.

 

As with the echoserver application, KKClient and KKServer are two processes (programs) that interact via the Socket API. You can run them on the same computer or on different computers (as you did in the last tutorial). You can see that the protocol is more extensive. The following shows the architecture.



1
Expert's answer
2021-12-02T14:16:10-0500

Client-Side Program:

import java.net.*;

 import java.io.*;

 import java.util.Scanner;

 public class ClientSide

 {

     // initializing socket and input output streams

     private DataOutputStream dataOut  = null;

     private Scanner sc                = null;

     private Socket skt                = null;

     // constructor to create a socket with given IP and port address

     public Client(String address, int port)

     {

         // Establishing connection with server

         try

         {

              // creating an object of socket

             skt = new Socket(address, port);

             System.out.println("Connection Established!! ");

             System.out.println("input \"Finish\" to terminate the connection. ");

             // taking input from user

             sc = new Scanner(System.in);

             // opening output stream on the socket

             dataOut = new DataOutputStream(skt.getOutputStream());

         }

         catch(UnknownHostException uh)

         {

             System.out.println(uh);

         }

         catch(IOException io)

         {

             System.out.println(io);

         }

         // to store the input messages given by the user

         String str = "";

         // The reading continues until "Finish" is input

         while (!str.equals("Finish"))

         {

             input = sc.nextLine(); // reading input

             try

             {

                 dataOut.writeUTF(str); // writing to the underlying output stream

             }

             // For handling errors while writing to output stream

             catch(IOException io)

             {

                 System.out.println(io);

             }

         }

         System.out.println(" Connection Terminated!! ");

         // for closing the connection

         try

         {

             dataOut.close();

             skt.close();

         }

         catch(IOException io)

         {

             System.out.println(io);

         }

     }

     public static void main(String argvs[])

     {

          // creating object of class Client

         ClientSide client = new ClientSide("localhost", 6666);

     }

 } 

Server-Side Program:

import java.net.*;

 import java.io.*;

 public class Server

 {

     //initializing input stream and socket

     private DataInputStream inStream = null;

     private Socket skt = null;

     private ServerSocket srvr = null;

     // constructor of the class Server

     public Server(int port)

     {

         // Starting the server and waiting for a client 

         try

         {

             srvr = new ServerSocket(port);

             System.out.println("Server starts");

             System.out.println("Waiting for a client to connect ... ");  

             skt = srvr.accept(); // waiting for  a client to send connection request

             System.out.println("Connected with a Client!! ");

             // Receiving input messages from the client using socket

             inStream = new DataInputStream( skt.getInputStream() );

             String str = ""; // variable for reading messages sent by the client

             // Untill "Finish" is sent by the client,

             // keep reading messages

             while (!str.equals("Finish"))

             {

                 try

                 {

                     // reading from the underlying stream

                     str = inStream.readUTF();

                     // printing the read message on the console

                     System.out.println( str );

                 }

                // For handling errors

                 catch(IOException io)

                 {

                     System.out.println( io );

                 }

             }

             // closing the established connection

             skt.close();

             inStream.close();

             System.out.println(" Connection Closed!! ");

         }

         // handling errors

         catch(IOException i)

         {

             System.out.println(i);

         }

     }

     public static void main(String argvs[])

     {

         // creating an object of the class ServerSide

         Server server = new Server(6666);

     }

 } 


The Knock Knock Server:

The server program begins by creating a new ServerSocket object to listen on a specific port . When running this server, choose a port that is not already dedicated to some other service.

Then program

  1. Gets the socket's input and output stream and opens readers and writers on them.
  2. Initiates communication with the client by writing to the socket (shown in bold).
  3. Communicates with the client by reading from and writing to the socket


The Knock Knock Client implements the client program that speaks to the Knock Knock Server.

When you start the client program, the server should already be running and listening to the port, waiting for a client to request a connection. So, the first thing the client program does is to open a socket that is connected to the server running on the specified host name and port:



Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS