Corrections to Chapter 13 of Java Network Programming, 3rd Edition, UDP Datagrams and Sockets

pp. 426: Figure 13-1 shows the destination port twice. The second one should be a checksum. Here's the corrected picture:

Structure of a UDP datagram

pp. 439-441: Example 13-3 and 13-4 share a mutual bug. Both assume they're using the local host's default encoding. However, if these two programs run on 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.


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

Copyright 2004-2005 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified February 22, 2005