Java Network Programming
Authors: Harold E. R.
Published year: 2003
Pages: 10-11/164
Buy this book on amazon.com >>
     

About the Examples

Most methods and classes described in this book are illustrated with at least one complete working program, simple though it may be. In my experience, a complete working program is essential to showing the proper use of a method. Without a program, it is too easy to drop into jargon or to gloss over points about which the author may be unclear in his own mind. The Java API documentation itself often suffers from excessively terse descriptions of the method calls. In this book, I have tried to err on the side of providing too much explication rather than too little. If a point is obvious to you, feel free to skip over it. You do not need to type in and run every example in this book, but if a particular method does give you trouble, you are guaranteed to have at least one working example.

Each chapter includes at least one (and often several) more complex programs that demonstrate the classes and methods of that chapter in a more realistic setting. These often rely on Java features not discussed in this book. Indeed, in many of the programs, the networking components are only a small fraction of the source code and often the least difficult parts . Nonetheless, none of these programs could be written as easily in languages that didn't give networking the central position it occupies in Java. The apparent simplicity of the networked sections of the code reflects the extent to which networking has been made a core feature of Java, and not any triviality of the program itself. All example programs presented in this book are available online, often with corrections and additions. You can download the source code from http://www.cafeaulait.org/books/jnp3/.

This book assumes you are using Sun's Java Development Kit. I have tested all the examples on Linux and many on Windows and MacOS X. Almost all the examples given here should work on other platforms and with other compilers and virtual machines that support Java 1.2 (and most on Java 1.1, as well). The occasional examples that require Java 1.3, 1.4, or 1.5 are clearly noted.

     

Conventions Used in This Book

Body text is Times Roman, normal, like you're reading now.

A monospaced typewriter font is used for:

  • Code examples and fragments

  • Anything that might appear in a Java program, including keywords, operators, data types, method names, variable names, class names, and interface names

  • Program output

  • Tags that might appear in an HTML document

A bold monospaced font is used for:

  • Command lines and options that should be typed verbatim on the screen

An italicized font is used for:

  • New terms where they are defined

  • Pathnames, filenames, and program names (however, if the program name is also the name of a Java class, it is given in a monospaced font, like other class names)

  • Host and domain names (java.oreilly.com)

  • URLs (http://www.cafeaulait.org/slides/)

  • Titles of other chapters and books ( JavaI/O )

Significant code fragments and complete programs are generally placed into a separate paragraph, like this:

Socket s = new Socket("java.oreilly.com", 80);

if (!s.getTcpNoDelay( )) s.setTcpNoDelay(true);

When code is presented as fragments rather than complete programs, the existence of the appropriate import statements should be inferred. For example, in the above code fragment you may assume that java.net.Socket was imported.

Some examples intermix user input with program output. In these cases, the user input will be displayed in bold, as in this example from Chapter 9:

%

telnet rama.poly.edu 7

Trying 128.238.10.212...

Connected to rama.poly.edu.

Escape character is '^]'.

This is a test

This is a test

This is another test

This is another test

9876543210

9876543210

^]

telnet>

close

Connection closed.

The Java programming language is case-sensitive. Java.net.socket is not the same as java.net.Socket . Case-sensitive programming languages do not always allow authors to adhere to standard English grammar. Most of the time, it's possible to rewrite the sentence in such a way that the two do not conflict, and when possible I have endeavored to do so. However, on those rare occasions when there is simply no way around the problem, I have let standard English come up the loser. In keeping with this principle, when I want to refer to a class or an instance of a class in body text, I use the capitalization that you'd see in source code, generally an initial capital with internal capitalizationfor example, ServerSocket .

Throughout this book, I use the British convention of placing punctuation inside quotation marks only when punctuation is part of the material quoted. Although I learned grammar under the American rules, the British system has always seemed far more logical to me, even more so than usual when one must quote source code where a missing or added comma, period, or semicolon can make the difference between code that compiles and code that doesn't.

Finally, although many of the examples used here are toy examples unlikely to be reused, a few of the classes I develop have real value. Please feel free to reuse them or any parts of them in your own code. No special permission is required. As far as I am concerned , they are in the public domain (although the same is most definitely not true of the explanatory text!). Such classes are placed somewhere in the com.macfaq package, generally mirroring the java package hierarchy. For instance, Chapter 4's SafePrintWriter class is in the com.macfaq.io package. When working with these classes, don't forget that the compiled .class files must reside in directories matching their package structure inside your class path , and that you'll have to import them in your own classes before you can use them. The book's web page at http://www.cafeaulait.org/books/jnp3/ includes a jar file containing all these classes that can be installed in your class path.

Indicates a tip, suggestion, or general note.


Indicates a warning or caution.


Java Network Programming
Authors: Harold E. R.
Published year: 2003
Pages: 10-11/164
Buy this book on amazon.com >>

Similar books on Amazon