|
To access the contents, click the chapter and section titles.
Platinum Edition Using HTML 4, XML, and Java 1.2
The Java Socket ClassesJava, of course, was developed by Sun. Sun earned its reputation as a leading developer of UNIX workstationsmuch of the Internet runs on Sun servers. Its not surprising, therefore, that Java is designed from the ground up as a networking language. This overview shows how Java makes network programming easier by encapsulating connection functionality in socket classes:
How the Internet Uses SocketsYou can think of an Internet server as a set of sockets, each of which provides additional capabilities called services. Examples of services are electronic mail, Telnet for remote login, and the File Transfer Protocol (FTP) for transferring files around the network. If the server to which you are attached is a Web server, then you can retrieve Web pages as well. Ports and ServicesEach service is associated with a port. A port is a numeric address through which service requests (such as asking for a Web page) are processed. On a UNIX system, the particular services provided are in the /etc/services file. Here are a few lines from a typical /etc/services file:
The first column displays the system name of the service (such as daytime). The second column displays the port number and the protocol, separated by a slash (as in 13/udp). The third column displays an alias to the service, if any. For example, SMTP (the Simple Mail Transfer Protocol), also known as mail, provides the email service. Mapping Java Sockets to Internet SocketsSockets are based on a client/server model. One program (the server) provides the service at a particular IP address and port. The server listens for service requests, such as requests for Web pages, and fills the order. Any program that wants to be serviced (a client, such as a Web browser) needs to know the IP address and port to communicate with the server. An advantage of the socket model over other forms of data communication is that the server doesnt care where the client requests come from. As long as the client is sending requests according to the TCP/IP protocol, the requests will reach the serverprovided the server is up and the Internet isnt too busy. What the particular server program does with the request is another matter. This design also means that the client can be any type of computer. No longer are you restricted to UNIX, Macintosh, DOS, or Windows platforms. Any computer that supports TCP/IP can talk to any other computer that supports it through this socket model. This design is a potentially revolutionary development in computing. Instead of maintaining armies of programmers to port a system from one platform to another, you write it one timein Java. Any computer with a Java Virtual Machine can run it. Java socket classes fit nicely into this picture. You implement a server by creating subclasses of Thread and overriding the run() method. The Java Virtual Machine can then perform the thread management without the program having to worry. Thus, with a few lines of code, you can write a server that can handle as many data communication sessions as you want. And data transmission is just a matter of calling the Socket methods. Writing Your Own Client and ServerApplets are restricted from connecting to just any Web server. They can only connect back to the server from which they were downloaded. If you want to give an applet access to the rest of the Internet, youll need to place a server on the same machine that hosts the applet. You can use a commercial server such as a File Transfer Protocol (FTP) server, or you can write your own special-purpose server. When you write a server, you write a program that opens a socket (typically on a well-known port number) and wait for some client to connect. The client calls in from some unused port number (called an ephemeral port). As soon as the client and the server connect, its common for the server to propose that the conversation continue on a different port. This design frees up the well-known port number to handle a new connection. Table 40.1 shows some common, well-known port numbers. These services are offered both on TCP ports and on UDP ports. The RFC column refers to the Internet Request for Comments document, where youll find the specification for the service.
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |