How to use Java to write the following: Remember that your program is probably n
ID: 3605731 • Letter: H
Question
How to use Java to write the following:
Remember that your program is probably not going to be able to listen on port 80. To tell a web brower to connect to a web server on an alternate port, append a ":" and the port number to the URL. For example, you could always try testing the server on your own computer, with the client using the address: http://127.0.0.1:9999, after you type in this (http://127.0.0.1:9999), the broswer window should return the public IP address and current of the host.
Write a program which specifies at run time . a port number on which to listen for incoming connections . the name of a log file The program listens on the TCP port specified. When a connection is made, the server sends back an HTTP 200 response and HTML text, which when shown in a browser, will display . "Your IP address is:" followed by the IP address of the machine that has connected. The IP address of the client may be obtained by using the getInetAddress method in the socket class . It should then print the current date and time. In order to obtain the current date and time, you can use the java Date class The program then writes to the log file: the time, IP address of the client, and all of the HTTP headers sent by the client For example, when I try the program on my own computer, I see in the browser: Your IP address is 129.32.95.12 The current time is: Mon Oct 17 15:17:22 EDT 2016 and then to the file, the program writes something like: Mon Oct 17 15:17:22 EDT 2016 IP: 129.32.95.12 GET/HTTP/1.1 Host: localhost: 9999 Connection: keep-alive User-Agent: Mozilla/5.0 (X11; Linux x86_64) ApplewebKit/535.1 (KHTML, like Accept: text/html,application/xhtml+xml,application/xml;q 0.9,*/*;q-0.8 Accept-Encoding: gzip,deflate, sdch Accept-Language: en-US,enq-0.8 Accept-Charset: ISO-8859-1,utf-8;q-9.7, * ; q=0.3 Gecko) Chrome/14.0.835.186 Safari/535.1 You should be able to test your program with a web browser to see that it's doing what it shouldExplanation / Answer
import java.net.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.io.*;
public class TcpScoketServer extends Thread {
private ServerSocket serverSocket;
public TcpScoketServer(int port) throws IOException {
serverSocket = new ServerSocket(port);
}
public void run() {
while (true) {
try {
FileWriter fr = new FileWriter("output.txt");
System.out.println("Waiting for Request on port " + serverSocket.getLocalPort() + "...");
Socket server = serverSocket.accept();
System.out.println("Just connected to " + server.getRemoteSocketAddress());
// Reading Request Detail DONE
InputStreamReader isr = new InputStreamReader(server.getInputStream());
BufferedReader reader = new BufferedReader(isr);
String line = reader.readLine();
while (!line.isEmpty()) {
fr.write(line + " ");
System.out.println(line);
line = reader.readLine();
}
// Reading Request Detail DONE
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("E, MMM dd yy HH:mm:ss z yyyy");
DataOutputStream out = new DataOutputStream(server.getOutputStream());
out.writeBytes("Your IP address is: " + server.getRemoteSocketAddress() + " ");
out.writeBytes("The current time is: " + formatter.format(date));
fr.close();
server.close();
break;
} catch (SocketTimeoutException s) {
System.out.println("Socket timed out!");
break;
} catch (IOException e) {
e.printStackTrace();
break;
}
}
}
public static void main(String[] args) {
try {
Thread t = new TcpScoketServer(9999);
t.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.