Question

please can you make it simple. For example using scanner or hard coding when it is...

please can you make it simple. For example using scanner or hard coding when it is a good idea instead of arrays and that stuff.Please just make one program (or class) and explain step by step. Also it was given to me a txt.htm

1.- Write a client program and a server program to implement the following simplified HTTP protocol based on TCP service. Please make sure your program supports multiple clients. The webpage file CS3700.htm is provided. You may choose a port like 5678 and hard code it in both client and server programs. (Hints: for testing both programs on the same computer, you may want to put client program and server program at two different directories. Do NOT hard code the file name “CS3700.htm”!) • The ending signature of the entity body in the HTTP response message for the case “200 OK”: o When the HTTP Server program sends a HTTP response message out, it pads four continuous blank lines, i.e., “\r\n\r\n\r\n\r\n”, to the end of the .htm file as the ending signature of the entity body. o When the HTTP Client program read the HTTP response message line by line, it counts the number of continuous lines that are empty strings “” (NOT a null string). Once such number reaches 4, the entity body has been fully received. o It is assumed that the .htm file does not include such ending signature (in the real practice, length of the entity body may be included in the head line and used to determine the end of an entity body). • HTTP Client Program: 1. Display messages on the standard output to ask the user to input the DNS name/ip of your HTTP server. 2. Buildup the TCP connection to your HTTP server with the Host Name input by User at the given port, and display the RTT of establishing this TCP connection in millisecond (the difference between the moments right before and after creating the socket object). Catch the exception, terminate the program, and display error messages on the standard output if any. 3. Display messages on the standard output to ask the user to input the HTTP method type, name of the htm file requested, HTTP Version, and User-Agent, respectively (separately please!). (hint: all inputs can be strings.) 4. Use the above inputs from user to construct ONE HTTP request message and send it to the HTTP server program over the TCP connection. Your HTTP request message only needs to include the following lines. (The correctness of the format will be checked by the instructor): The request line (hint: the URL should include a ‘/’ in front of the htm file name) The header line for the field “Host:” The header line for the field “User-Agent:” 5. Receive and interpret the HTTP response message from the HTTP Server program line by line, display the RTT (File Transmission Time may be included) of HTTP query in millisecond (the difference between the moment right before HTTP request is sent and the moment right after HTTP response is received) as a single line (e.g., RTT = 1.089 ms), display the status line and header lines of the HTTP response message on the standard output, and save the data in the entity body to a .htm file to local directory if there is any. (Hint: (a) When one empty string “” (NOT a null string!) is read the FIRST TIME, it indicates the header lines are over and the entity body is going to start next line if the case is “200 OK”.) 6. Display a message on the standard output to ask the User whether to continue. If yes, repeat steps 3 through 6. Otherwise, close all i/o streams, TCP connection, and terminate the Client program.

This was given to me

import java.io.*;
import java.net.*;

public class TCPClient {
public static void main(String[] args) throws IOException {

Socket tcpSocket = null;
PrintWriter socketOut = null;
BufferedReader socketIn = null;

if (args.length != 1) {
System.out.println("Usage: java TCPClient <hostname>");
return;
}
  
try {
tcpSocket = new Socket(args[0], 4567);
socketOut = new PrintWriter(tcpSocket.getOutputStream(), true);
socketIn = new BufferedReader(new InputStreamReader(tcpSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: " + args[0]);
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to: " + args[0]);
System.exit(1);
}

BufferedReader sysIn = new BufferedReader(new InputStreamReader(System.in));
String fromServer;
String fromUser;

while ((fromUser = sysIn.readLine()) != null) {
       System.out.println("Client: " + fromUser);
socketOut.println(fromUser);
              
               if ((fromServer = socketIn.readLine()) != null)
               {
                   System.out.println("Server: " + fromServer);
               }
               else {
System.out.println("Server replies nothing!");
break;
               }
      
           if (fromUser.equals("Bye."))
                   break;

}

socketOut.close();
socketIn.close();
sysIn.close();
tcpSocket.close();
}
}

Homework Answers

Answer #1

import java.net.*;
import java.io.*;
import java.util.Date;


public class TcpServer {

public static final int LISTENING_PORT = 4567;

public static void main(String[] args) {

ServerSocket listener; // Listens for incoming connections.
Socket connection; // For communication with the connecting program.

/* Accept and process connections forever, or until some error occurs.
(Note that errors that occur while communicating with a connected
program are caught and handled in the sendResponse() routine, so
they will not crash the server.) */

try {
listener = new ServerSocket(LISTENING_PORT);
System.out.println("Listening on port " + LISTENING_PORT);
while (true) {
// Accept next connection request and handle it.
connection = listener.accept();
sendResponse(connection);
}
}
catch (Exception e) {
System.out.println("Sorry, the server has shut down.");
System.out.println("Error: " + e);
return;
}

} // end main()



private static void sendResponse(Socket client) {
try {
System.out.println("Connection from " +
client.getInetAddress().toString() );
Date now = new Date(); // The current date and time.
PrintWriter outgoing; // Stream for sending data.
outgoing = new PrintWriter( client.getOutputStream() );
outgoing.println( now.toString() );
  
////write http responsse
// <status-line>
//<general-headers>
//<response-headers>
//<entity-headers>
//<empty-line>
//[<message-body>]
//[<message-trailers>]
  
outgoing.flush(); // Make sure the data is actually sent!
client.close();
}
catch (Exception e){
System.out.println("Error: " + e);
}
} // end


} //end class

-----------------------------------------------------------------------------------------

import java.io.*;
import java.net.*;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class TcpClient {
public static void main(String[] args) throws IOException {
Scanner read=new Scanner(System.in);

   Socket tcpSocket = null;
PrintWriter socketOut = null;
BufferedReader socketIn = null;
System.out.print("pls enter ip or dns sname of host server");

String Servername= read.nextLine();
  
  
  
  
try {
tcpSocket = new Socket(Servername, 4567);
socketOut = new PrintWriter(tcpSocket.getOutputStream(), true);
socketIn = new BufferedReader(new InputStreamReader(tcpSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: " + Servername);
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to: " + Servername);
System.exit(1);
}
BufferedReader sysIn = new BufferedReader(new InputStreamReader(System.in));
String fromServer;
String httpmethod;
String htmfilename;
String useragent;
String httpversion;
System.out.println("enter http method");
httpmethod=read.nextLine();
System.out.println("name of htm file");
htmfilename=read.nextLine();
System.out.println("enter http version");
httpmethod=read.nextLine();
System.out.println("enter user agent");
useragent=read.nextLine();
socketOut.println(httpmethod+" /"+htmfilename+"Host:"+Servername+"useragent:"+useragent);
  
  
if ((socketIn.readLine()) != null)
{
   while(socketIn.readLine()!=null)
   {
while( socketIn.readLine() != null)
{
   fromServer=socketIn.readLine();
   System.out.println(fromServer);
}
//write data to a file using file stream
   }
}
else {
System.out.println("Server replies nothing!");
  
}
  

socketOut.close();
socketIn.close();
sysIn.close();
tcpSocket.close();
  
}}

[please write http respose in server class]

[and save it in .htm file in client class]

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
1. Please use only the C as the language of programming. 2. Please submit/upload on Canvas,...
1. Please use only the C as the language of programming. 2. Please submit/upload on Canvas, the following les for each of your programs: (1) the client and the server source les each (2) the client and the serve executable les each (3) a brief Readme le that shows the usage of the program. 3. Please appropriately comment your program and name all the identiers suitable, to enable enhanced readability of the code. Problems 1. Write an ftp client that...
The goal of this assignment is to implement a simple client-server system. You have to use...
The goal of this assignment is to implement a simple client-server system. You have to use Python. The basic functionality of the system is a remote calculator. You first will be sending expressions through the client to the server. The server will then calculate the result of the expression and send it back to the client where it is displayed. The server should also send back to the client the string "Socket Programming" as many times as the absolute value...
Client / Server using named pipes with thread and fork() in C/C++ **Note** You will need...
Client / Server using named pipes with thread and fork() in C/C++ **Note** You will need to write a client program and a server program for this question to be correct. ** **Cannot use socket** client the client application that will get the command from the user and pass the command to be executed from the command line, parses the command and puts a binary representation of the parse into a shared memory segment. At this point, the client will...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import java.io.*; //This imports input and output (io) classes that we use 3 //to read and write to files. The * is the wildcard that will 4 //make all of the io classes available if I need them 5 //It saves me from having to import each io class separately. 6 /** 7 This program reads numbers from a file, calculates the 8 mean (average)...
Task #4 Calculating the Mean Now we need to add lines to allow us to read...
Task #4 Calculating the Mean Now we need to add lines to allow us to read from the input file and calculate the mean. Create a FileReader object passing it the filename. Create a BufferedReader object passing it the FileReader object. Write a priming read to read the first line of the file. Write a loop that continues until you are at the end of the file. The body of the loop will: convert the line into a double value...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT