Chapter 5. Networking

Sun Microsystems has long used the slogan “The Network Is The Computer.” It’s no surprise, therefore, that they designed Java to be a network-centric language. The java.net package provides powerful and easy-to-use networking capabilities. The examples in this chapter demonstrate those capabilities at a number of different levels of abstraction. They show you how to:

  • Use the URL class to parse URLs and download the network resources specified by a URL

  • Use the URLConnection class to gain more control over the downloading of network resources

  • Write client programs that use the Socket class to communicate over the network

  • Use the Socket and ServerSocket classes to write servers

  • Send and receive low-overhead datagram packets

Java 1.4 introduced the New I/O API of java.nio and its subpackages. This new API is channel-based instead of stream-based, and can be used for local file I/O as well as network I/O. java.nio is particularly suited to high-performance servers, and does not make the java.net package obsolete. The New I/O API is covered in Chapter 6.

Get Java Examples in a Nutshell, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.