Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. Write a Java TCP socket program consisting of one client C and one local server S (localhost). Code for the client and server should

1. Write a Java TCP socket program consisting of one client C and one local server S (localhost). Code for the client and server should be separate and contained in the respective files F1 and F2 above. Modify and use the code in the files posted at the bottom: TCPWebClient18.java and TCPKRClient.java for the client C and TCPKRServer.java for the server S. Since we are testing behavior with no threads, the code should not have threads.

Do not use System.exit() in your code. Submit your socket code in the files F1 and F2 (named as above). All the code needed should be in these two files. Include a comment at the top of each file that says how you compiled and ran the code. Capture screenshots with client and server screens during an example run and put them in the file F3.

The client C

  1. asks the user to enter a web server Ws name as a string www.name.suf (for example, www.ieee.org)
  2. prints the message CLIENT START= followed by Cs local system time CT1
  3. makes a TCP socket connection to the local server S listening on port 11211
  4. sends Ws name to S and waits to receive Ws page from S
  5. receives and displays the text of Ws page sent to it by S
  6. prints the message CLIENT END= followed by Cs local system time CT2
  7. prints the message CLIENT DELAY= followed by the value of CT=CT2-CT1 in milliseconds
  8. prints the message ROUNDTRIP TIME= followed by the value of RT=CT-ST in milliseconds, where ST is the value of server delay sent to it by S.

The local server S (localhost)

  1. listens for and accepts the connection from C on port 11211
  2. receives and prints the message WEB SERVER= followed by Ws name sent by C
  3. prints the message IP ADDRESS= followed by Ws IP address
  4. prints the message SERVER START= followed by Ss local system time ST1
  5. uses the class HttpURLConnection to connect to the web server W on port 443
  6. receives Ws page from the web server W
  7. prints the message SERVER END= followed by Ss local system time ST2
  8. prints the message SERVER DELAY= followed by the value of ST=ST2-ST1 in milliseconds
  9. sends to C the page received from the web server W
  10. sends to C the value of ST

TCPWebClient18.java:

import java.io.*; import java.net.*; class TCPWebClient18 { public static void main(String argv[]) throws Exception { String inputLine; URL obj = new URL("https://www.umd.edu"); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); con.setRequestMethod("GET"); con.setRequestProperty("User-Agent","Mozilla/5.0"); BufferedReader in = new BufferedReader( new InputStreamReader(con.getInputStream())); while ((inputLine = in.readLine()) != null) System.out.println(inputLine); } }

TCPKRClient.java:

import java.io.*; import java.net.*; class TCPKRClient { public static void main(String argv[]) throws Exception { String sentence; String modifiedSentence; BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); Socket clientSocket = new Socket("localhost", 6789); DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream()); BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); sentence = inFromUser.readLine(); outToServer.writeBytes(sentence + ' '); modifiedSentence = inFromServer.readLine(); System.out.println("FROM SERVER: " + modifiedSentence); clientSocket.close(); } }

TCPKRServer.java:

import java.io.*; import java.net.*; class TCPKRServer { public static void main(String argv[]) throws Exception { String clientSentence; String capitalizedSentence; ServerSocket welcomeSocket = new ServerSocket(6789); while(true) { Socket connectionSocket = welcomeSocket.accept(); BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream()); clientSentence = inFromClient.readLine(); capitalizedSentence = clientSentence.toUpperCase() + ' '; outToClient.writeBytes(capitalizedSentence); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Analysis Using SQL And Excel

Authors: Gordon S Linoff

2nd Edition

111902143X, 9781119021438

More Books

Students also viewed these Databases questions