Introduction


Java can be used to write many types of networked programs. In traditional socket-based code, the programmer is responsible for the entire interaction between the client and server. In higher-level types, such as RMI, CORBA, and EJB, the software takes over increasing degrees of control. Sockets are often used for connecting to "legacy" servers; if you were writing a new application from scratch, you'd be better off using a higher-level service.

It may be helpful to compare sockets with the telephone system. Telephones were originally used for analog voice traffic, which is pretty unstructured. Then it began to be used for some "layered" applications; the first widely popular one was facsimile transmission, or fax. Where would fax be without the widespread availability of voice telephony? The second wildly popular layered application is dialup TCP/IP. This coexisted with the Web to become popular as a mass-market service. Where would dialup IP be without widely deployed voice lines? And where would the Internet be without dialup IP?

Sockets are like that too. The Web, RMI, JDBC, CORBA, and EJB are all layered on top of sockets.

Ever since the alpha release of Java (originally as a sideline to the HotJava browser) in May 1995, Java has been popular as a programming language for building network applications. It's easy to see why, particularly if you've ever built a networked application in C. First, C programmers have to worry about the platform they are on. Unix uses synchronous sockets, which work rather like normal disk files vis-a-vis reading and writing, while Microsoft OSes use asynchronous sockets, which use callbacks to notify when a read or write has completed. Java glosses over this distinction. Further, the amount of code needed to set up a socket in C is intimidating. Just for fun, Example 16-1 shows the "typical" C code for setting up a client socket. And remember, this is only the Unix part. And only the part that makes the connection. To be portable to Windows, it would need additional conditional code (using C's #ifdef mechanism). And C's #include mechanism requires that exactly the right files be included in exactly the right order; Java's import mechanism lets you use * to import a whole section of the API, and the imports can be listed in any order you like.

Example 16-1. C client setup
/*  * Simple demonstration of code to setup a client connection in C.  */ #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #include <stdio.h> #include <string.h> #include <fcntl.h> int main(int argc, char *argv[]) {     char* server_name = "localhost";     struct hostent *host_info;     int sock;     struct sockaddr_in server;     /* Look up the remote host's IP address */     host_info = gethostbyname(server_name);     if (host_info == NULL) {         fprintf(stderr, "%s: unknown host: %s\n", argv[0], server_name);         exit(1);     }     /* Create the socket */     if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {         perror("creating client socket");         exit(2);     }     /* Set up the server's socket address */     server.sin_family = AF_INET;     memcpy((char *)&server.sin_addr, host_info->h_addr,                      host_info->h_length);     server.sin_port = htons(80);     /* Connect to the server */     if (connect(sock,(struct sockaddr *)&server,sizeof server) < 0) {         perror("connecting to server");         exit(4);     }     /* Finally, we can read and write on the socket. */     /* ... */     (void) close(sock); }

In the first recipe, we'll see how to do the connect in essentially one line of Java (plus a bit of error handling). We'll then cover error handling and transferring data over a socket. Next, we'll take a quick look at a datagram or UDP client that implements most of the TFTP (trivial file transfer protocol) that has been used for two decades to boot diskless workstations. We'll end with a program that connects interactively to a text-based server such as Telnet or email.

A common theme through most of these client examples is to use existing servers so that we don't have to generate both the client and the server at the same time. With one exception, all of these are services that exist on any standard Unix platform. If you can't find a Unix server near you to try them on, let me suggest that you take an old PC, maybe one that's underpowered for running the latest Microsoft software, and put up a free, open source Unix system on it. My personal favorite is OpenBSD, and the market's overall favorite is Linux. Both are readily available on CD-ROM, can be installed for free over the Internet, and offer all the standard services used in the client examples, including the time servers and TFTP. Both have free Java implementations available.



Java Cookbook
Java Cookbook, Second Edition
ISBN: 0596007019
EAN: 2147483647
Year: 2003
Pages: 409
Authors: Ian F Darwin

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net