Question: Java: Briefly explain the code in each area with /*comment*/ above it import java.io.*; import java.net.*; public class HttpHeader { public static void main(String args[])
Java: Briefly explain the code in each area with /*comment*/ above it
import java.io.*;
import java.net.*;
public class HttpHeader {
public static void main(String args[]) throws java.io.IOException {
if (args.length < 1) {
System.err
.println("Usage: java HttpHeader [document name]");
System.exit(0);
}
final int PORT = 80;
Socket sock = null;
BufferedReader in = null;
PrintWriter out = null;
String requestedDocument = "";
if (args.length == 2)
requestedDocument = args[1];
try {
/* Comment Here */
sock = new Socket(args[0], PORT);
/* Comment Here */
in = new BufferedReader(
new InputStreamReader(sock.getInputStream()));
out = new PrintWriter(
new OutputStreamWriter(sock.getOutputStream()));
/* Comment Here */
String message = "GET /" + requestedDocument
+ " HTTP/1.1 Host: " + args[0] + " ";
out.print(message);
out.flush();
/* Comment Here */
String line;
while ((line = in.readLine()) != null) {
if (line.length() == 0)
break;
System.out.println(line);
}
}
/* Comment Here */
catch (Exception e) {
System.err.println(e);
} finally { // close all streams
if (in != null)
in.close();
if (out != null)
out.close();
if (sock != null)
sock.close();
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
