home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Platinum Edition Using HTML 4, XML, and Java 1.2
(Publisher: Macmillan Computer Publishing)
Author(s): Eric Ladd
ISBN: 078971759x
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


The Java Socket Classes

Java, of course, was developed by Sun. Sun earned its reputation as a leading developer of UNIX workstations—much of the Internet runs on Sun servers. It’s 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:

  Socket is the basic object in Internet communication, which supports the TCP/IP protocol. The Transmission Control Protocol/Internet Protocol (TCP/IP) is a reliable stream network connection. The Socket class provides methods for stream I/O, which make reading from and writing to Socket easy.
  ServerSocket is an object used for Internet server programs for listening to client requests. ServerSocket does not actually perform the service; instead, it creates a Socket object on behalf of the client. The communication is performed through that object.
  DatagramSocket is an object that uses the User Datagram Protocol (UDP). Datagram sockets are technically unreliable because no connection is involved. You send them out hoping they reach their destination, but you have no guarantee that a server is even listening. In addition, the networking software will not guarantee the delivery of UDP packages. However, communication using datagram sockets is faster because no connection is made between the sender and receiver. Think of UDP like a telegram, sent out in the hope that it will be delivered. TCP, in contrast, is more like a telephone call—you know the message is delivered because you delivered it yourself, through a connection. Streaming audio and video often use UDP.
  SocketImpl is an abstract class that enables you to implement your own flavor of data communication. As with all abstract classes, you subclass SocketImpl and implement its methods, as opposed to instantiating SocketImpl itself.

How the Internet Uses Sockets

You 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 Services

Each 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:

daytime 13/udp
ftp 21/tcp
telnet 23/tcp telnet
smtp 25/tcp mail
www 80/tcp

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 Sockets

Sockets 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 doesn’t 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 server—provided the server is up and the Internet isn’t 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 time—in 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 Server

Applets 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, you’ll 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, it’s 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 you’ll find the specification for the service.

ON THE WEB
ftp://ds.internic.net/rfc/ You can download any RFCs that you’d like to read from this FTP server. Check the login message you get when you connect—FTP sites are on each continent. If you’re not on the East Coast of North America, one may be closer to you than ds.internic.net.


Previous Table of Contents Next


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.