Corrections to Chapter 10 of Java Network Programming, The Socket Class

p. 307: The first code fragment tries to use a non-existent public InetAddress constructor. It should read:

try {
  InetAddress OReilly  = InetAddress.getByName("www.oreilly.com");
  Socket OReillySocket = new Socket(OReilly, 80);
  // send and receive data...
}
catch (UnknownHostException e) {
  System.err.println(e);
}
catch (IOException e) {
  System.err.println(e);
}
p. 308: The code fragment at the bottom of the page tries to use a non-existent public InetAddress constructor. It should instead read:

try {
  InetAddress fddi = InetAddress.getByName("fddisunsite.oit.unc.edu");
  Socket OReillySocket = new Socket("www.oreilly.com", 80, fddi, 0);
  // work with the sockets...
}
catch (UnknownHostException e) {
  System.err.println(e);
}
catch (IOException e) {
  System.err.println(e);
}
p. 309: The code fragment toward the bottom of the page tries to use a non-existent public InetAddress constructor. It should instead read:

try {
  InetAddress metalab  = InetAddress.getByName("metalab.unc.edu");
  InetAddress oreilly  = InetAddress.getByName("www.oreilly.com");
  Socket oreillySocket = new Socket(oreilly, 80, metalab, 0);
}
catch (UnknownHostException e) {
  System.err.println(e);
}
catch (IOException e) {
  System.err.println(e);
}
pp. 323-324: In Example 10-7, the finally block is attached to the wrong try block. It should be inside the for loop. Here's the corrected program:

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


public class PortScanner {

  public static void main(String[] args) {
    
    String host = "localhost";

    if (args.length > 0) {
      host = args[0];
    }

    try {
      InetAddress theAddress = InetAddress.getByName(host);
      for (int i = 1; i < 65536; i++) {
        Socket connection = null;
        try {
          connection = new Socket(host, i);
          System.out.println("There is a server on port " 
           + i + " of " + host);
        }
        catch (IOException e) {
          // must not be a server on this port
        }
        finally {
          try {
            if (connection != null) connection.close(); 
          }
          catch (IOException e) {}
        }
      } // end for
    } // end try
    catch (UnknownHostException e) {
      System.err.println(e);
    }
    
  }  // end main
  
}  // end PortScanner

p. 312: The first code fragment; uses a deprecated Socket constructor. Change

Socket theSocket = new Socket("java.sun.com", 80, true);

to

Socket theSocket = new Socket("java.sun.com", 80);

That is, delete the third argument.

p. 327: A read() call on a socket with a non-zero SO_TIMEOUT value may throw a java.io.InterruptedIOException, not a java.lang.InterruptedException.


[ Java Network Programming Corrections | Java Network Programming Home Page | Table of Contents | Examples | Order from Amazon ] ]

Copyright 2000, 2001, 2003 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified January 4, 2003