6.7. Creating a TCP Server

One of the most enjoyable parts of networking is writing a network server. Clients send requests and respond to data sent back, but the server performs most of the real work. This next example is of a daytime server (which you can test using the client described in Section 6.5).

Code for DaytimeServer

 import java.net.*; import java.io.*; // Chapter 6, Listing 2 public class DaytimeServer { public static final int SERVICE_PORT = 13; public static void main(String args[]) { try { // Bind to the service port, to grant clients // access to the TCP daytime service ServerSocket server = new ServerSocket (SERVICE_PORT); System.out.println ("Daytime service started"); // Loop indefinitely, accepting clients for (;;) { // Get ...

Get Java™ Network Programming and Distributed Computing now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.