Dayclient and Dayserver - implement the Unix 'daytime' protocol. Java code example - Click here to copy ->>>
Can't find what you're looking for? Try our search:
|
|
Really working examples categorized by API, package, class. You can compile and run our examples right away!
Not from source code for Java projects - only working examples! Copy, compile and run!
|
------------------
These two classes show an extremely simple example of java.net socket programming. They implement the Unix 'daytime' protocol, an extremely simple protocol that consists entirely of the server sending its current local time and date to the client as an ASCII string. The server, to keep it very simple, does not use multiple threads. This code requires JDK 1.1 or later, but can easily be adapted to JDK 1.0.
| Code: |
import java.io.*;
import java.net.*;
/**
* Tiny socket client for the Unix 'daytime' protocol.
*/
public class Dayclient
{
public static void main(String[] args)
{
Socket csocket;
BufferedReader timestream;
if (args.length < 1) {
System.err.println("Please supply a host name or IP address");
System.exit(0);
}
try
{
csocket = new Socket(args[0],13);
timestream = new BufferedReader(new InputStreamReader(csocket.getInputStream()));
String thetime = timestream.readLine();
System.out.println("it is "+ thetime + " at " + args[0]);
csocket.close();
}
catch(IOException e)
{ System.err.println(e);}
}
}
|
| Code: |
import java.util.Date;
import java.io.*;
import java.net.*;
/**
* Tiny socket server for the Unix 'daytime' protocol.
*/
public class Dayserver
{
public static void main(String[] args)
{
ServerSocket theserver;
Socket asocket;
PrintWriter p;
BufferedReader timestream;
Socket csocket;
try
{
theserver = new ServerSocket(13);
try
{
while(true)
{
System.out.println("Waiting for customers...");
asocket = theserver.accept();
p = new PrintWriter(asocket.getOutputStream());
p.println(new Date());
p.close();
asocket.close();
// csocket.close();
// timestream.close();
}
}
catch(IOException e)
{ theserver.close();
System.err.println(e);}
}
catch(IOException e)
{ System.err.println(e);}
}
}
|
|
|
References.
The list of classes which were used on this page you can find below. The
links to Java API contain official SUN documentation about all used classes.
[ Go Back ]
|