Errata

Java Network Programming

Errata for Java Network Programming

Submit your own errata for this product.

The errata list is a list of errors and their corrections that were found after the product was released. If the error was corrected in a later version or reprint the date of the correction will be displayed in the column titled "Date Corrected".

The following errata were submitted by our customers and approved as valid errors by the author or editor.

Color key: Serious technical mistake Minor technical mistake Language or formatting error Typo Question Note Update

Version Location Description Submitted By Date submitted Date corrected
Printed
Page 1
273

CookieStore.java:
add:
if( null == setCookies)
return;
before:
for (String next : setCookies) {
try {
Cookie cookie = Cookie.bake(next, uri);
...
or there will be exception when server does not set cookies.

Note from the Author or Editor:
On p. 273 immediately after the line
List<String> setCookies = repsonseHeaders.get("Set-Cookie");

add this line:

if (setCookies == null) return;

wzg  Dec 19, 2010  Jul 01, 2011
Printed
Page 97
Example 4-1; SafeBufferedReader, can lose data if the last line

does not end with a line break character. Here's the corrected version:

package com.macfaq.io;

import java.io.*;

public class SafeBufferedReader extends BufferedReader {

public SafeBufferedReader(Reader in) {
this(in, 1024);
}

public SafeBufferedReader(Reader in, int bufferSize) {
super(in, bufferSize);
}

private boolean lookingForLineFeed = false;

public String readLine() throws IOException {
StringBuffer sb = new StringBuffer("");
while (true) {
int c = this.read();
if (c == -1) { // end of stream
if (sb.length() == 0) return null;
return sb.toString();
}
else if (c == '
') {
if (lookingForLineFeed) {
lookingForLineFeed = false;
continue;
}
else {
return sb.toString();
}
}
else if (c == '
') {
lookingForLineFeed = true;
return sb.toString();
}
else {
lookingForLineFeed = false;
sb.append((char) c);
}
}
}

}

Anonymous    Jul 01, 2011
Printed
Page 108-109
Example 5-1 should be titled DigestThread, not FileDigestThread.

Anonymous    Jul 01, 2011
Printed
Page 118

The paragraph at the top of the page should read:

one additional field, an InstanceCallbackDigestUserInterface object called callback. At the end of
the run() method, the digest is passed to callback's receiveDigest() method. The
InstanceCallbackDigestUserInterface object itself is set in the constructor.

Anonymous    Jul 01, 2011
Printed
Page 118
At the beginning of the last paragraph change

The CallbackDigestUserInterface class shown in Example 5-8
to
The InstanceCallbackDigestUserInterface class shown in Example 5-8

Anonymous    Jul 01, 2011
Printed
Page 162
The caption for Example 6-7,

should be "Determining whether an IP address is v4 or v6

Anonymous    Jul 01, 2011
Printed
Page 164
In the fifth paragraph

change
"Organization multicast addresses begin with FF05 or FF15"
to
"Site-wide multicast addresses begin with FF05 or FF15".

Anonymous    Jul 01, 2011
Printed
Page 165
In the caption for Example 6-8, change

"(Java 1.4 only)" to
"(Java 1.4 and later)"

Anonymous    Jul 01, 2011
Printed
Page 171
In the first code fragment on the page, change

NetworkInterface.getByName(local) to
NetworkInterface.getByInetAddress(local)

Anonymous    Jul 01, 2011
Printed
Page 180
There are three errors in Example 6-13.

First, change new BufferedReader to new SafeBufferedReader.
Then in the ,code>main() method, change e.printStackTrace() to ex.printStackTrace().
Finally, several lines from the processLogFile() method were omitted.
The complete, corrected program is:

import java.io.*;
import java.util.*;
import com.macfaq.io.SafeBufferedReader;

public class PooledWeblog {

private BufferedReader in;
private BufferedWriter out;
private int numberOfThreads;
private List entries = Collections.synchronizedList(new LinkedList());
private boolean finished = false;
private int test = 0;

public PooledWeblog(InputStream in, OutputStream out, int
numberOfThreads) {
this.in = new SafeBufferedReader(new InputStreamReader(in));
this.out = new BufferedWriter(new OutputStreamWriter(out));
this.numberOfThreads = numberOfThreads;
}

public boolean isFinished() {
return this.finished;
}

public int getNumberOfThreads() {
return numberOfThreads;
}

public void processLogFile() {

for (int i = 0; i < numberOfThreads; i++) {
Thread t = new LookupThread(entries, this);
t.start();
}

try {
String entry = in.readLine();
while (entry != null) {

if (entries.size() > numberOfThreads) {
try {
Thread.sleep((long) (1000.0/numberOfThreads));
}
catch (InterruptedException e) {}
continue;
}

synchronized (entries) {
entries.add(0, entry);
entries.notifyAll();
}

entry = in.readLine();
Thread.yield();

} // end while

}
catch (IOException e) {
System.out.println("Exception: " + e);
}

this.finished = true;

// finish any threads that are still waiting
synchronized (entries) {
entries.notifyAll();
}

}

public void log(String entry) throws IOException {
out.write(entry + System.getProperty("line.separator", "
"));
out.flush();
}

public static void main(String[] args) {

try {
PooledWeblog tw = new PooledWeblog(new FileInputStream(args[0]),
System.out, 100);
tw.processLogFile();
}
catch (FileNotFoundException e) {
System.err.println("Usage: java PooledWeblog logfile_name");
}
catch (ArrayIndexOutOfBoundsException e) {
System.err.println("Usage: java PooledWeblog logfile_name");
}
catch (Exception ex) {
System.err.println(ex);
ex.printStackTrace();
}

} // end main

}

Anonymous    Jul 01, 2011
Printed
Page 190
Remove HTTPS from the list of unsupported protocols.

That is, change
"This browser supports HTTP, HTTPS, FTP, mailto, file, gopher, doc, netdoc, verbatim,
systemresource, and jar but not HTTPS, ldap, Telnet, jdbc, rmi, jndi, finger or daytime."
to
"This browser supports HTTP, HTTPS, FTP, mailto, file, gopher, doc, netdoc, verbatim,
systemresource, and jar but not ldap, Telnet, jdbc, rmi, jndi, finger or daytime."

Anonymous    Jul 01, 2011
Printed
Page 218
In Example 7-10, the line

System.out.println("The host is " + u.getUserInfo());

should be

System.out.println("The host is " + u.getHost());

As a result, on p. 219 in the output, "The host is null" should be "The host is www.xml.com"

Anonymous    Jul 01, 2011
Printed
Page 226
Change

"Finally, the name-value pairs are simply the NAME attributes of the INPUT elements, except for any INPUT elements whose
TYPE attributes has the value submit."
to
"Finally, the names in the name-value pairs are simply the values of the NAME attributes of the
INPUT elements. The values of the pairs are whatever the user types into the form."

Anonymous    Jul 01, 2011
Printed
Page 252
2nd non-code line

Reference to "Example 8-5's ParserGetter class". Example 8-5 doesn't have a ParserGetter class. Example 8-6 contains the class.

Note from the Author or Editor:
On p. 252 change "Example 8-5's ParserGetter class" to "Example 8-6's ParserGetter class"

Richard Levey  Nov 14, 2009  Jul 01, 2011
Printed
Page 257
8-9,

This is a really trivial one, that isn't even really an erratum, but I do want to fix it in the next printing for appearance's sake.
In the fourth line of code, change

for (int i =0;

to

for (int i = 0;

That is add a space after the equals sign.

Anonymous    Jul 01, 2011
Printed
Page 268
In the second paragraph change "can set include" to "can include".

Anonymous    Jul 01, 2011
Printed
Page 271

In Example 8-11, the matches() method should also check the domain. That is, it should read:

public boolean matches(URI u) {

if (isExpired()) return false;

String path = u.getPath();
if (path == null) path = "/";

String domain = u.getHost();

if (path.startsWith(this.path)
&& domain.equals(thisdomain)) {
return true;
}

return false;
}

Anonymous    Jul 01, 2011
Printed
Page 296
first paragraph

The first sentence reads "Java 1.4 adds an isClosed() method that returns true
is the socket has been closed,"
should read
"...returns true if the socket has been closed,"
^^

Anonymous    Jul 01, 2011
Printed
Page 299

Change
if (s.getTcpSoLinger() == -1) s.setSoLinger(true, 240);
to
if (s.getSoLinger() == -1) s.setSoLinger(true, 240);

Anonymous    Jul 01, 2011
Printed
Page 301
line 2

"However, again thegggg client ..."
Should be:
"However, again the client ..."

Anonymous    Jul 01, 2011
Printed
Page 334
third line of code from bottom

println() error message should be
"Port must be between 0 and 65535".
^^

Anonymous    Jul 01, 2011
Printed
Page 343
catch (IOException ex) near end of page

Code example reads:

} catch(IOException ex) {
e.PrintStackTrace();
}

There is no "e" in that Exception...

Note from the Author or Editor:
Change e.printStackTrace to ex.printStackTrace on p. 343

Anonymous    Jul 01, 2011
Printed
Page 349
In the main() method, on the last line of the page,

"if (args.length >= 2)" should be "if (args.length > 2)".

Anonymous    Jul 01, 2011
Printed
Page 358
Sample Code - Top of page

The block of the second 'catch' uses the variable 'e' instead of 'ex'.
So the code should read:
catch (IOException ex) {
System.out.println("Server could not start because of an "
+ ex.getClass());
System.out.println(ex);
}

Anonymous    Jul 01, 2011
Printed
Page 367
2nd & 3rd paragaraphs

"java.policy" should be "java.security" in both paragraphs

Anonymous    Jul 01, 2011
Printed
Page 368
at the beginning of page

I noticed a small error in O'Reilly Java Network Programming 3rd Edition.
The error is at the beginning of page 368 of the hardcopy.

I think the method public abstract Socket createSocket(InetAddress host, int port,
InetAddress interface, int localPort) shouldn't throw UnknownHostException.

Although the next method public abstract Socket createSocket(Socket proxy, String
host, int port, boolean autoClose) should throw that UnknownHostException.

So I think the two methods should be printed like this:-

public abstract Socket createSocket(InetAddress host, int port, InetAddress
interface, int localPort) throws IOException

public abstract Socket createSocket(Socket proxy, String host, int port, boolean
autoClose) throws IOException, UnknownHostException


Thanks again for that nice book.

Tareq Jahhaf.

Note from the Author or Editor:
On p. 368 remove ", UnknownHostExeption" from the third line and place it at the end of the fifth line (right after IOException)

Anonymous  Jul 02, 2008  Jul 01, 2011
Printed
Page 426
figure 13-1 of a UDP datagram has an error.

There are two fields in the diagram both shown as "destination port (0-65535)". There should
be only one such field, the 2nd of the duplicates should instead be indicated as being a checksum.

Anonymous    Mar 22, 2013
Printed
Page 426
In Figure 13-1 there's an extraneous letter I at the bottom

lefthand corner of the picture. That shouldn't be there.

Anonymous    Jul 01, 2011
Printed
Page 428
1st paragraph

The last two constructors for the DatagramPacket class have typos. In particular, these last two should take a SocketAddress destination as the last parameter, not have have int port as the last parameter. So they should look like:

DatagramPacket(byte[] buf, int length,
SocketAddress address)

DatagramPacket(byte[] buf, int offset, int length,
SocketAddress address)

FYI, I cut the definitions above directly from the Javadoc at http://download.oracle.com/javase/1,5.0/docs/api/index.html?java/net/InetSocketAddress.html

Regards,
Mike

Note from the Author or Editor:
This erratum is correct. On p. 428 change ", int port) // Java 1.4" to simply ") // Java 1.4" both places it occurs (in the 6th and 8th lines of the code fragment at the top of the page.

Mike Figg  Jul 01, 2011  Mar 22, 2013
Printed
Page 439-441
Example 13-3 and 13-4 share a mutual bug. Both assume

they're using the local host's default encoding. However, is these two
programs run of different hosts (for instance, one is a Japanese system
and one is a French system) that may well not be the same for each. The
encoding needs to be explicitly specified in both programs. On p. 439 in
Example 13-3 change

byte[] data = theLine.getBytes()
to
byte[] data = theLine.getBytes("UTF-8")

On p. 441 in Example 13-4 change

String s = new String(packet.getData(), 0, packet.getLength())
to
String s = new String(packet.getData(), 0, packet.getLength(), "UTF-8")

This will allow the two hosts to exchange data regardless of differing encodings.

Anonymous    Jul 01, 2011
Printed
Page 444
in paragraph 3

It's in the hardcopy on page 444 in paragraph 3.
The title of the paragraph is:-
public InetAddress getRemoteSocketAddress( )
I think the right should be:-
public SocketAddress getRemoteSocketAddress( )
The method getRemoteSocketAddress() is in class SocketAddress not in InetAddress.

Note from the Author or Editor:
Change public InetAddress getRemoteSocketAddress( )
to public SocketAddress getRemoteSocketAddress( )

Anonymous    Jul 01, 2011
Printed
Page 491
In example 14-2, the ,code>for loop should run from 1 to 10, not

1 to 9. That is,

for (int i = 1; i < 10; i++) {

should be:

for (int i = 1; i <= 10; i++) {

Anonymous    Jul 01, 2011
Printed
Page 519
ex 15-8 source code

QueryString constructor needs parameter

The first lines of that example
should be

import java.net.*;
import java.io.*;
import com.macfaq.net.*;

public class FormPoster {

private URL url;
// from Chapter 7, Example 7-9
private QueryString query = null;

public FormPoster (URL url) {
if (!url.getProtocol().toLowerCase().startsWith("http")) {
throw new IllegalArgumentException(
"Posting only works for http URLs");
}
this.url = url;
}

public void add(String name, String value) {
if (query == null) query = new QueryString(name, value);
query.add(name, value);
}

The rest of the example doesn't change.

Anonymous    Jul 01, 2011
Printed
Page 544
in Example 15-11

package java.net
should be
package java.net;

Anonymous    Jul 01, 2011
Printed
Page 544
in the final paragraph;

change
getOutputStream()
to
getBody().

Anonymous    Jul 01, 2011
Printed
Page 545
in Example 15-13

public abstract class CacheRequest

should be

public abstract class CacheResponse

Anonymous    Jul 01, 2011
Printed
Page 549
In the second paragraph change

Example 15-13
to
Example 15-15.

Anonymous    Jul 01, 2011
Printed
Page 594
first paragraph

In the first text paragraph
"java.protocolhandler.pkgs" should be "java.protocol.handler.pkgs"

Anonymous    Jul 01, 2011