home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Platinum Edition Using HTML 4, XML, and Java 1.2
(Publisher: Macmillan Computer Publishing)
Author(s): Eric Ladd
ISBN: 078971759x
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Table 40.1 Common Internet Services and Their Port Numbers

Service Name TCP Port UDP Port RFC Description

echo 7 7 862 Server returns whatever the client sends.
discard 9 9 863 Server discards whatever the client sends.
daytime 13 13 867 Server returns the time and date in a human-readable format.
chargen 19 864 TCP server sends a continual stream of characters until the client terminates the connection.
chargen 19 864 UDP server sends a datagram containing a random number of characters each time the client sends a datagram.
time 37 37 868 Server returns the time as a 32-bit binary number—the number of seconds since midnight on January 1, 1900, UTC.


NOTE:  TCP/IP port numbers are managed by the Internet Assigned Numbers Authority (IANA). IANA has specified that well-known port numbers are always between 1 and 1023. A Telnet server listens on TCP port 23, for example, and the Trivial File Transfer Protocol (TFTP) server listens on UDP port 69. Most TCP/IP implementations allocate ephemeral port numbers between 1024 and 5000, but that design isn’t specified by IANA.

Java provides two kinds of sockets: client sockets, implemented in the Socket class, and server sockets, implemented in the ServerSocket class.

Understanding the Client Socket Class

To connect to a host, your client program should include a line such as

Socket theConnection = new Socket(hostname, portNumber);

(Remember the security restrictions that apply to applets.)

  To learn more about the restrictions browsers place on applets, see “Executable Content and Security,” p. 1142.

The Socket constructor throws an IOException if it has a problem. Otherwise, you can presume that the Socket is open and ready for communication:

BufferedReader theReader = new BufferedReader(
  new InputStreamReader(theConnection.getInputStream()));
BufferedWriter theWriter = new BufferedWriter(
  new OutputStreamWriter(theConnection.getOutputStream()));

Now you can read and write theReader and theWriter in the usual fashion. When you’re done with theConnection, call

theConnection.close();

This step will also close all the streams, readers, and writers you have associated with this Socket.

Understanding ServerSockets

If you choose to write a server, you’ll need to write a ServerSocket. Such a socket binds a specified port. To bind port 8000, for example, you write

ServerSocket theServerConnection = new ServerSocket(8000);

When you install a server, it’s a good idea to check /etc/services to make sure the port number is free. Likewise, after you choose a port number, add a corresponding entry in /etc/services to notify other programmers who might want to add a server to this machine.

This code tells the underlying operating system that you intend to offer a service on port 8000. (You aren’t listening to that port quite yet.) If the runtime environment is able to bind to the specified port, it does so and sets the allowable backlog to a default of 50. (This means that once you have 50 pending requests to connect, all subsequent requests are refused. You can specify a different backlog value in the ServerSocket constructor.) If the runtime environment cannot bind to the port (which happens if the port is already allocated to another service), you’ll get an IOException.

After you’ve bound the port, you can attach the port and start listening for connections by calling accept():

Socket aSocket = theServerConnection.accept();

When the connection is made, accept() unblocks and returns a Socket. You can open streams, readers, and writers on the Socket the same as you did from the client program.

Using Client and Server Sockets

Listing 40.2 shows a server framework. Our simple server sets up the ServerSocket, then implements four steps:

1.  Waits for a client to connect
2.  Accepts the client connection
3.  Sends a message to the client
4.  Tears down the connection

Listing 40.2 TServer.javaUse This Framework as the Basis for Your Own Server


 import java.net.*;
 import java.io.*;

 public class TServer extends Thread {
   private static final int PORTNUMBER = 8013;
   private ServerSocket fServerSocket;

   public TServer() {
     super(“TServer”);
     try {
       fServerSocket = new ServerSocket(PORTNUMBER);
       System.out.println(“TServer up and running...”);
     } catch (IOException e) {
       System.err.println(“Exception: couldn’t make server socket.”);
       System.exit(1);
     }
   }

   public void run() {
     Socket theClientSocket;

     while (true) {

       // wait for a client to connect
       if (fServerSocket == null)
         return;
       try {
         theClientSocket = fServerSocket.accept();

         // accept the client connection

         // send a message to the client
         PrintWriter theWriter = new PrintWriter(new
           OutputStreamWriter(theClientSocket.getOutputStream()));
         theWriter.println(new java.util.Date().toString());
         theWriter.flush();

         // tear down the connection
         theWriter.close();
         theClientSocket.close();
       } catch (IOException e) {
         System.err.println(“Exception: ” + e.getMessage());
         System.exit(1);
       }
     }
   }

   public static void main(String[] args) {
     TServer theServer = new TServer();
     theServer.start();
   }
 }


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.