CS计算机代考程序代写 Java distributed system COMP3221 Lab 3 Client-Server Communication
COMP3221 Lab 3 Client-Server Communication
The goal is to write a client-server communication using sockets. Your machine will be used both as a client and as a server, and the client will simply ask the time of the server sent back to him in String format. Finally, the client should write the information received on the standard output.
Exercise1: ¡°Whattimeisit?¡±server
The server, written in a class DateServer, should always be listening to some incoming con-
nection on a non-reserved port, e.g., 6013, and treating it. The server needs an OutputStream
to send information in response to some client request as described below. Although not nec-
essary here, you are generally welcome to check the Java API (http://docs.oracle.com/javase/7/docs/a to learn more about classes you may need that already exist, like OutputStream. Complete
the code below using Socket in order to accept an incoming connection and write it in file DateServer.java.
1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19 20 21
import java.net.*; import java.io.*;
public class DateServer {
public static void main(String[] args) {
try {
… // TODO: create a new socket
// now listen for connections
while (true) {
… // TODO: wait and accept a client connection
PrintWriter pout = new PrintWriter(client.getOutputStream(), true);
// write the Date to the socket
pout.println(new java.util.Date().toString());
… // TODO: close the client connection }
Duration: 15 min
} }
} catch System.err.println(ioe);
(IOException ioe) {
1
Exercise2: ¡°Whattimeisit?¡±client
The client, written in a class DateClient, simply initiates the connection by creating a socket targeting the local machine, always identified by the IP address ¡°127.0.0.1¡± and the service port chosen. It reads from this connection using an InputStreamReader object and buffer- izes this stream of information, before being able to print it as indicated in the incomplete code below.
1 …
2 // read the date from the socket
3 String line;
4 while ((line = bin.readLine()) != null)
5 System.out.println(line);
6 …
Write the client class in file DateClient.java. Compile them, then run the server in the background before running the client.
1 $ javac DateClient.java DateServer.java
2 $ java DateServer &
3 $ java DateClient
What happens if the client runs while no server is running?
Make sure to kill the server process that runs in the background when you are done. Duration: 15 min
Exercise 3: Logging Server
The goal is to design a new LoggingDateServer that will log each received request from a client in addition to answering the date to the client (as before). Each time the server receives a request from a client, it stores a line in a new file whose name contains a monotonically in- creasing number, writing for example files log0.txt, log1.txt, log2.txt, upon reception of the three first requests.
As I/O can take a non-negligible amount of time, what could be the problem in the proposed solution?
Write the code of the server so that it writes the client hostname and the sent date to the log file. Note that the client hostname can be obtained on the server side by calling socket.getInetAddress().getHostName(); where socket corresponds to the instance of the Socket. Test that it creates log files, when running the previous client.
1 $ javac LoggingDateServer.java
2 $ java LoggingDateServer &
3 $ java DateClient
Duration: 20 min
COMP3221 Client-Server Communication
Distributed Systems
Page 2 of 2