FileDocCategorySizeDatePackage
SimpleDaytimeServer.javaAPI DocExample2058Sat Jan 24 10:44:30 GMT 2004je3.nio

SimpleDaytimeServer

public class SimpleDaytimeServer extends Object
A simple TCP server for the daytime service. See RFC867 for details. This implementation lacks meaningful exception handling and cannot handle UDP connections.

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)

	// RFC867 specifies port 13 for this service.  On Unix platforms,
	// you need to be running as root to use that port, so we allow
	// this service to use other ports for testing.
	int port = 13;  
	if (args.length > 0) port = Integer.parseInt(args[0]);

	// Create a channel to listen for connections on.
	ServerSocketChannel server = ServerSocketChannel.open();

	// Bind the channel to a local port.  Note that we do this by obtaining
	// the underlying java.net.ServerSocket and binding that socket.
	server.socket().bind(new InetSocketAddress(port));

	// Get an encoder for converting strings to bytes
	CharsetEncoder encoder = Charset.forName("US-ASCII").newEncoder();

	for(;;) {  // Loop forever, processing client connections
	    // Wait for a client to connect
	    SocketChannel client = server.accept();
	    
	    // Build response string, wrap, and encode to bytes
	    String date = new java.util.Date().toString() + "\r\n";
	    ByteBuffer response = encoder.encode(CharBuffer.wrap(date));

	    // Send the response to the client and disconnect.
	    client.write(response);
	    client.close();
	}