Java Reference
In-Depth Information
Display 19.5 Date and Time Server (part 1 of 2)
1
import java.util.Date;
2
import java.net.ServerSocket;
3
import java.net.Socket;
4
import java.io.DataOutputStream;
5
import java.io.BufferedReader;
6
import java.io.InputStreamReader;
7
import java.io.IOException;
8 public class DateServer
9{
10
public static void main(String[] args)
11
{
12
Date now = new Date();
13
try
14
{
15
System.out.println("Waiting for a connection on port 7654.");
16
ServerSocket serverSock = new ServerSocket(7654);
17
Socket connectionSock = serverSock.accept();
18
BufferedReader clientInput = new BufferedReader(
19
new InputStreamReader(connectionSock.getInputStream()));
20
DataOutputStream clientOutput = new DataOutputStream(
21
connectionSock.getOutputStream());
22
System.out.println("Connection made, waiting for client " +
23
"to send their name.");
24
String clientText = clientInput.readLine();
25
String replyText = "Welcome, " + clientText +
26
", Today is " + now.toString() + "\n";
27
clientOutput.writeBytes(replyText);
28
System.out.println("Sent: " + replyText);
29
clientOutput.close();
30
clientInput.close();
31
connectionSock.close();
32
serverSock.close();
33
}
34
catch (IOException e)
35
{
System.out.println(e.getMessage());
36
}
37
}
38
}
(continued)
Search WWH ::




Custom Search