Home  I/O  Buffering  Character streams  NIO intro  Buffers  Buffer performance

Introduction to networking

Figure 1: Illustration of incoming and outgoing ports, as though they were numbered telephones.

In this section we'll give a fairly high-level overview of networking, assuming you have little if any prior knowledge.

We can start by imagining each computer on the Internet as an office with a number of telephones. Each time a program running on a computer needs to communicate with another computer (e.g. to download a web page, connect to a chat server, send an e-mail etc), it needs to get the "office number" to call. Either it knows this already, or more often than not, it just has the "office name" (such as somewebsite.com). So the first stage is often to look up that computer's number (IP address) in the Internet "phone directory". The Internet phone directory is actually the Domain Name System (DNS), and we'll gloss over its details for now; suffice to say that our program ends up with an IP address or "office number"— essentially a four-byte number— which it now knows it needs to connect to.

Office numbers aren't the end of the story, however. Each office actually has a number of extensions that we could call. Each extension provides a particular "service". For example, for sending an e-mail, we might need to call extension 25; extension 80 might be where we call to fetch a web page, extension 443 to securely send a credit card number and get the confirmation web page, etc. For the connection to go through, there generally has to be an operator (piece of software) waiting for calls on the given extension. We generally know which extension to call either becuase it's a well-known extension number used throughout the Internet (e.g. if a computer gives out web pages, it will generally give them out on "extension" 80), or because the particular software we are using and connecting with has defined which extension number to use. Note that each extension number can actually receive multiple calls simultaneously. (It wouldn't be much good if a busy web server could only receive a connection from one computer at a time.)

In addition to incoming extensions, each office has a separate series of numbered telephones for outgoing calls. So when we connect to, say, extension 80 of such-and-such machine, we pick up one of our free outgoing telephones and dial the office number and extension. Our office number and telephone number are transmitted to the receiver of the call. The analogy isn't perfect, but you can imagine this as being useful if the connection gets cut off for some reason: the person can now call our office back and say "hello, I was in the middle of a call from telephone 67; please reconnect me". In actual fact, the separate outgoing telephone numbers are also needed so that a single office (computer) can make a number of simultaneous connections to the same destination and extension and distinguish between the different connections. For example, our web browser could pick up phone number (say) 435 to download the main page, and simultaneously pick up 436 to download an image from that page. In this case, both phones from "our office" would actually be connected to extension 80 of "the other office", and we assume that each office has magic telephone lines that allow an arbitrary number of callers to connect to the same extension...

To the user or program, this is essentially how most networking works. Just as with the telephone system, there are some details that we've skipped over because they're not essential to most network programming. For example, when we say we want to connect to a computer with a particular IP address, there needs to be an infrastructure in place to "route the call". But just as an average phone user doesn't need to know about telephone exchanges to place a call, a network programmer doesn't generally need to know too much about network infrastructure to make a simple network connection.

Sockets and ports

In network programming, the telephones in our analogy are called ports. Each end of a network connection (an "operator at a telephone" if you like) is called a socket and has a local and destination IP address and port number. In a typical networking application, the applications at the ends of a connection can be classified into either a client, that will make a number of outgoing connections, or a server, that will generally sit receiving a number of incoming connections.

Next: a simple Java server

Keeping in mind this very simple overview of networking, on the next page, we'll look how how to write a simple server in Java.

Written by Neil Coffey. Copyright © Javamex UK 2008. All rights reserved.