UNPKG

epubjs

Version:

Render ePub documents in the browser, across many devices

39 lines (38 loc) 4.66 kB
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"><head><title>Chapter 13. Network Programming</title><link rel="stylesheet" href="core.css" type="text/css"/><meta name="generator" content="DocBook XSL Stylesheets V1.74.0"/></head><body><div class="chapter" title="Chapter 13. Network Programming"><div class="titlepage"><div><div><h1 class="title"><a id="learnjava3-CHP-13"/>Chapter 13. Network Programming</h1></div></div></div><p>The network is the soul of Java. Most of what is interesting about Java centers on the potential for dynamic, networked applications. As Java’s networking APIs have matured, Java has also become the language of choice for implementing traditional client/server applications and services. In this chapter, we start our discussion of the <code class="literal">java.net</code> package, which contains the fundamental classes for communications and working with networked resources (we’ll finish this discussion in <a class="xref" href="ch14.html" title="Chapter 14. Programming for the Web">Chapter 14</a>). This chapter next discusses the <a id="I_indexterm13_id764743" class="indexterm"/><code class="literal">java.rmi</code> package, which provides Java’s native, high-level, Remote Method Invocation (RMI) facilities. Finally, building on the material in <a class="xref" href="ch12.html" title="Chapter 12. Input/Output Facilities">Chapter 12</a>, we complete our discussion of the <code class="literal">java.nio</code> package, which is highly efficient for implementing large servers.</p><p>The classes of <code class="literal">java.net</code> 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). <a class="xref" href="ch13.html#learnjava3-CHP-13-FIG-1" title="Figure 13-1. The java.net package">Figure 13-1</a> shows the <code class="literal">java.net</code> package.</p><p>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 tool in the general networking toolbox—you can use sockets for any kind of communications between client and server or peer applications on the Net, but you have to implement your own application-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.</p><p>Java RMI is a powerful tool that leverages Java object serialization, allowing you to transparently work with Java objects on remote machines almost as if they were local. With RMI, it is easy to write distributed applications in which clients and servers work with each other’s data as full-fledged Java objects rather than raw streams or packets of data.</p><div class="figure"><a id="learnjava3-CHP-13-FIG-1"/><div class="figure-contents"><div class="mediaobject"><a id="I_13_tt834"/><img src="httpatomoreillycomsourceoreillyimages1707644.png" alt="The java.net package"/></div></div><p class="title">Figure 13-1. The java.net package</p></div><p>In contrast to RMI, which can only be used between two Java applications, <span class="emphasis"><em>web services</em></span> is the term for the more general technology that provides platform-independent, loosely coupled invocation of services on remote servers using web standards such as HTTP and XML. We talk about web services in Chapters <a class="xref" href="ch14.html" title="Chapter 14. Programming for the Web">14</a> and <a class="xref" href="ch15.html" title="Chapter 15. Web Applications and Web Services">15</a> when we discuss programming for the Web.</p><p>In this chapter, we’ll provide some simple, practical examples of both high- and low-level Java network programming using sockets and RMI. In <a class="xref" href="ch14.html" title="Chapter 14. Programming for the Web">Chapter 14</a>, we’ll look at the other half of the <code class="literal">java.net</code> package, which lets clients work with web servers and services via URLs. <a class="xref" href="ch15.html" title="Chapter 15. Web Applications and Web Services">Chapter 15</a> covers Java servlets and the tools that allow you to write web applications and services for web servers.</p></div></body></html>