Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. MULTI-FUNCTION CLIENT AND SERVER: This problem involves the development of a

ID: 3746124 • Letter: 1

Question

1. MULTI-FUNCTION CLIENT AND SERVER: This problem involves the development of a simple client-server application. Upon execution, the client should print a menu of possible commands, and prompt the user to enter the number of a command which should be executed on the server. Then, the client should send the number to the server (which is waiting to read requests on a predefined UDP port). The server should read the number from the client and execute the appropriate command, returning the results (as a string) to the client The client should print the reply to the console and then be ready to accept additional commands from the user The command that should be available are: 1. Current time 2. PID (process ID) of the server 3. Random number between 1 and 20, inclusive Note: The Application Layer Protocol should be clearly defined, and must exchange data in ASClI format only. The server should be connectionless and iterative, i.e. only process one request at a time (see net2/server.c). Don't worry about making the communication reliable, e.g. by adding timeouts, etc

Explanation / Answer

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.io.OutputStreamWriter;

import java.net.ServerSocket;

import java.net.Socket;

public class Server

{

    private static Socket socket;

    public static void main(String[] args)

    {

        try

        {

            int port = 25000;

            ServerSocket serverSocket = new ServerSocket(port);

            System.out.println("Server Started and listening to the port 25000");

            //Server is running always. This is done using this while(true) loop

            while(true)

            {

                //Reading the message from the client

                socket = serverSocket.accept();

                InputStream is = socket.getInputStream();

                InputStreamReader isr = new InputStreamReader(is);

                BufferedReader br = new BufferedReader(isr);

                String number = br.readLine();

                System.out.println("Message received from client is "+number);

                //Multiplying the number by 2 and forming the return message

                String returnMessage;

                try

                {

                    int numberInIntFormat = Integer.parseInt(number);

                    int returnValue = numberInIntFormat*2;

                    returnMessage = String.valueOf(returnValue) + " ";

                }

                catch(NumberFormatException e)

                {

                    //Input was not a number. Sending proper message back to client.

                    returnMessage = "Please send a proper number ";

                }

                //Sending the response back to the client.

                OutputStream os = socket.getOutputStream();

                OutputStreamWriter osw = new OutputStreamWriter(os);

                BufferedWriter bw = new BufferedWriter(osw);

                bw.write(returnMessage);

                System.out.println("Message sent to the client is "+returnMessage);

                bw.flush();

            }

        }

        catch (Exception e)

        {

            e.printStackTrace();

        }

        finally

        {

            try

            {

                socket.close();

            }

            catch(Exception e){}

        }

    }