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

Q2: The Server while receiving a connection request should display the remote cl

ID: 664591 • Letter: Q

Question

Q2: The Server while receiving a connection request should display the remote client’s IP address and port number.

Hints and resources:

Your client program should have the facility to accept inputs from (i) console and (ii) file, so ask user where the input is coming from accordingly.

An example server and client program in Java is uploaded and will be demonstrated in class.

The following website has example of socket concept and code, but in C and Python: http://www.ittc.ku.edu/~jpgs/courses/nets/lecture-lab-socket-print.pdf

You are welcome to reuse the port number demonstrated in lecture slides or materials posted in D2L.

Test you application by launching just one server and one client application

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){}

        }

    }

}