Java Reference
In-Depth Information
BufferedReader is = new BufferedReader(
new InputStreamReader(sock.getInputStream( )));
PrintWriter os = new PrintWriter(sock.getOutputStream( ), true);
Example 13-4 reads a line of text from the “daytime” service, which is offered by full-
fledged TCP/IP suites (such as those included with most Unixes). You don't have to send
anything to the Daytime server; you simply connect and read one line. The server writes one
line containing the date and time and then closes the connection.
Running it looks like this. I started by getting the current date and time on the local host, then
ran the DaytimeText program to see the date and time on the server (machine darian is one
of my Unix servers):
C:\javasrc\network> date
Current date is Sun 01-23-2000
Enter new date (mm-dd-yy):
C:\javasrc\network> time
Current time is 1:13:18.70p
Enter new time:
C:\javasrc\network> java network.DaytimeText darian
Time on darian is Sun Jan 23 13:14:34 2000
The code is in class DaytimeText , shown in Example 13-4 .
Example 13-4. DaytimeText.java
public
public class
class DaytimeText
DaytimeText {
public
public static
static final
final short
short TIME_PORT = 13 ;
public
public static
void main ( String [] argv ) {
String hostName ;
iif ( argv . length == 0 )
hostName = "localhost" ;
static void
else
else
hostName = argv [ 0 ];
try
try {
Socket sock = new
new Socket ( hostName , TIME_PORT );
BufferedReader is = new
new
InputStreamReader ( sock . getInputStream ()));
String remoteTime = is . readLine ();
System . out . println ( "Time on " + hostName + " is " + remoteTime );
} catch
new BufferedReader ( new
catch ( IOException e ) {
Search WWH ::




Custom Search