by David Wood

Pour On The Java.  Hold The Programming.  Click Here.  Jamba.

Java's Socket Implementation

This week, we're discussing the use and merit of Java's socket implementation.

Once again, we're going to wax poetic and say that this is a charmingly simple implementation of this basic network capability. This is a very powerful feature to have with such a relatively low cost in complexity; a gain that's attributable to both good logical design and an excellent system for language documentation.

Much the same as the URLConnection is started with a URL, the Socket class is typically started with an InetAddress. This InetAddress class is simpler than a URL; it contains only a single Internet address in the form of an IP number (an IP number is a numerical address that every computer on the Internet must have). The InetAddress class is also designed to work differently from the URL class--rather than creating one by just typing "new InetAddress("foo.bar.com")", it is constructed via specific static methods on the class. If we just type "InetAddress.getByName("foo.bar.com")", we get an InetAddress object returned with foo.bar.com's IP address.

The InetAddress class has a few other notable features. It can return a String hostname corresponding to its IP address, and process hostnames that have multiple IP addresses by returning an array of InetAddress instances. There's also a method for returning the IP address of the "local" machine (the machine the applet is running on). Check out the API for the details.

Socket also contains a shortcut constructor that allows you to just specify a hostname (as a String). In either case, along with clarifying the host in question, the socket also needs to know what port number to connect to. For those unfamiliar with the concept of a port number, it's quite simple. In general, when computers communicate with each other on a network, they find it handy to be able to call a specific program on the target computer that they'd like to talk to, especially since a computer may be running more than one program that listens to the network at once.

This is done by assigning each program a number, and having the caller specify the number in every communication. This number is otherwise known (in Internet and UNIX circles) as the port number. Over the decades, a standard has evolved for the type of programs that can operate on different ports. Telnet connections always take place on port 23, and mail connections on port 25. The standard Web connection occurs on port 80, and a URL allows for another port to be specified after the hostname--"http://foo.bar.com:8000" for example.

Once you've created your socket, you have several methods for recalling information that went into creating it, including getInputStream() and getOutputStream(), the two methods for sending and receiving data over the network connection you've just created. Now you can "print" data to another computer somewhere else in the world, and "read" data back from it the same way you might print to the screen and read from the keyboard.

It's worth mentioning that this is only half of the socket networking facility in Java; there's another class called ServerSocket that handles the receiving of these kinds of connections instead of sending them. We'll certainly get around to talking about writing servers in Java. For now, though, we're ready to try out a few socket programs to see how they work. Tune in next week to see for yourself.

Past installments of Java Jolt

http://www.internet.com/