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

You are asked to develop a Server program running on one computer, and a Client

ID: 3821785 • Letter: Y

Question

You are asked to develop a Server program running on one computer, and a Client program running on another computer. (When you test your server and client programs on one computer, you may use Loopback IP Address 127.0.0.1, with which any packets sent out from this machine will immediately loop back to itself.) The Server will accept input from keyboard for system initialization, selection of menu items, human command and instructions, etc. It will also display information on the monitor, periodically send commands to the Client, and receive feedback from the Client. After receiving a command from the Server, the Client will send feedback to the Server, and displays some information of your interest. Assignment tasks are described below: (1) When the Server is started, it initializes the settings of the server’s IP addresses, port Server Keyboard Monitor Client TCP/IP Port No.: 3247 Monitor Keyboard 3 number, and the client’s IP address, etc., through command window arguments, e.g., header file, arguments to main(), keyboard input, or input from a configuration file which is a pure text file. (2) Every 3 seconds, the Server sends the Client a command to ask for data, e.g., through a single letter “R” or “r” (request). (Timing control is required here. Using our examples in the lecture materials if you like.) (3) After receiving the command from the Server, the Client sends back to the Server an ACK consisting of the client’s time (hh:mm:ss:???) and a random integer number between 0 and 100 with a uniform distribution. The Client may also display some useful information on its monitor. (Use a random generator to generate such random numbers. For example, rand()%101 and use srand(time(NULL)) to get a seed for rand(), where time() function is defined in header file time.h). (4) The Server gets the feedback from the Client, and display the result. (5) The Server reads keyboard input of various command and instructions. An obvious command is to terminate the Server program, e.g., using a single letter “E” or “e” (exit). When the Sever is to be terminated, the Server should also notify the Client of the Server’s termination so that the Client also terminates properly.

Explanation / Answer


public class ServerPart
{

private static Socket socket1;

public static void main(String[] args)
{
try
{

int port = 2500;
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("Server Started and listening to the port 2500");
while(true)
{
  
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);
String returnMessage;
try
{
int numberInIntFormat = Integer.parseInt(number);
int returnValue = numberInIntFormat*2;
returnMessage = String.valueOf(returnValue) + " ";
}
catch(NumberFormatException e)
{

returnMessage = "Please send a proper number ";
}

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

client:-)

public class ClientPart
{

private static Socket socket;

public static void main(String args[])
{
try
{
String host = "localhost";
int port = 2500;
InetAddress address = InetAddress.getByName(host);
socket = new Socket(address, port);
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);

String number = "2";

String sendMessage = number + " ";
bw.write(sendMessage);
bw.flush();
System.out.println("Message sent to the server : "+sendMessage);
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String message = br.readLine();
System.out.println("Message received from the server : " +message);
}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
  
try
{
socket.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote