import java.io.*;
import java.net.*;
import java.util.*;
public class UselessHTTPServer04 {
public static void main(String args[]) throws Exception {
int port = Integer.parseInt(args[0]);
ServerSocket serverSock=new ServerSocket(port);
while(true) {
Socket conn = serverSock.accept();
Scanner scanin = new Scanner(conn.getInputStream());
String line=null;
int nlines=0;
while (true) {
line = scanin.nextLine();
if(line.length()==0) break;
nlines = nlines + 1;
System.out.println("line "+nlines+": "+line);
}
String reply="HTTP/1.0 404 Not Found\r\n" +
"Connection: close\r\n" +
"Content-Type: text/html\r\n" +
"\r\n" +
"<h1>Sorry, work in progress</h1>\r\n";
OutputStream outs = conn.getOutputStream();
outs.write(reply.getBytes());
conn.close();
}
}
}
How do you add code after the for
loop that prints the lines but before the 404 message is sent. The
code should create an additional scanner just for the first request
line. Should use it to extract the first two words and store them
in the new string variables command and resource.