Question: Write a java program which specifies at run time: a port number on which to listen for incoming connections. the name of a log file

Write a java 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 Gecko) Chrome/14.0.835.186 Safari/535.1

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

Accept-Encoding: gzip,deflate,sdch

Accept-Language: en-US,en;q=0.8

Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

You should be able to test your program with a web browser to see that it's doing what it should.

Remember that your program is probably not going to be able to listen on port 80. To tell a web browser to connect to a web server on an alternate port, append a ":" and the port number to the URL. For example, if you're running your server on cis-linux2.temple.edu port 9999, in the browser's location window, you'd type: http://cis-linux2.temple.edu:9999 and you could always try testing the server on your own computer, with the client using the address: http://127.0.0.1:9999

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!