Network Programming

The network is the soul of Java. Most of what is interesting about Java centers around the potential for dynamic, networked apps. As Java's networking APIs have matured, Java has also become the language of choice for implementing traditional client-server apps and services. In this chapter, we start our discussion of the package, which contains the fundamental classes for communications and working with networked resources (we'll finish this discussion in ). This chapter next discusses the java.rmi package, which provides Java's powerful, high-level, Remote Method Invocation (RMI) facilities. Finally, building on the material in , we complete our discussion of the java.nio package, which is highly efficient for implementing large servers. The classes of fall into two general categories: the Sockets API for working with low-level Internet protocols and higher-level, web-oriented APIs that work with uniform resource locators (URLs). Screenshot-1 shows the package. Java's Sockets API provides access to the standard network protocols used for communications between hosts on the Internet. Sockets are the mechanism underlying all other kinds of portable networked communications. Sockets are the lowest-level toolyou can use sockets for any kind of communications between client and server or peer apps on the Net, but you have to implement your own app-level protocols for handling and interpreting the data. Higher-level networking tools, such as remote method invocation, HTTP, and web services are implemented on top of sockets. Java RMI is a powerful tool that leverages Java object serialization, allowing you to transparently work with objects on remote machines almost as if they were local. With RMI, it is easy to write distributed apps in which clients and servers work with each other's data as full-fledged Java objects rather than raw streams or packets of data. Technologies like Enterprise JavaBeans build on RMI and extend its semantics, so it is very important to understand the concepts involved.

Screenshot-1. The package
Java ScreenShot

In contrast to RMI, which ties Java apps together in an object-to-object fashion, web services provide for more loosely coupled invocation of services on remote servers using web standards such as HTTP and XML. We talk about web services in s 14 and 15 when we discuss coding for the Web. In this chapter, we'll provide some simple, practical examples of both high- and low-level Java network coding using sockets and RMI. In , we'll look at the other half of the package, which lets clients work with web servers and services via URLs. covers Java servlets and the tools that allow you to write web apps and services for web servers.

Java ScreenShot