Java sockets 101
Presented by developerWorks, your source for great tutorials
ibm.com/developerWorks
Table of Contents
If you're viewing this document online, you can click any of the topics below to link directly to that section.
1. Tutorial tips 2
2. Socket basics 3
3. An undercover socket 8
4. A simple example 11
5. A multithreaded example 18
6. A pooled example 21
7. Sockets in real life 27
8. Summary 31
9. Appendix 32
Java sockets 101 Page 1
Section 1. Tutorial tips
Should I take this tutorial?
Sockets, which provide a mechanism for communication between two computers, have been
around since long before the Java language was a glimmer in James Gosling's eye. The
language simply lets you use sockets effectively without having to know the details of the
underlying operating system. Most books that focus on Java coding either fail to cover the
topic, or leave a lot to the imagination. This tutorial will tell you what you really need to know
to start using sockets effectively in your Java code. Specifically, we'll cover:
* What sockets are
* Where they fit into the structure of programs you're likely to write
* The simplest sockets implementation that could possibly work -- to help you
understand the basics
* A detailed walkthrough of two additional examples that explore sockets in multithreaded
and pooled environments
* A brief discussion of an application for sockets in the real world
If you can describe how to use the classes in the java.net package, this tutorial is probably
a little basic for you, although it might be a good refresher. If you have been working with
sockets on PCs and other platforms for years, the initial sections might bore you. But if you
are new to sockets, and simply want to know what they are and how to use them effectively
in your Java code, this tutorial is a great place to start.
Getting help
For questions about the content of this tutorial, contact the authors, Roy Miller (at
Roy Miller and Adam Williams are Software Developers at RoleModel Software, Inc. They
have worked jointly to prototype a socket-based application for the TINI Java platform from
Dallas Semiconductor. Roy and Adam are currently working on porting a COBOL financial
transaction system to the Java platform, using sockets.
Prior to joining RoleModel, Roy spent six years with Andersen Consulting (now Accenture)
developing software and managing projects. He co-authored Extreme Programming Applied:
Playing to Win (Addison-Wesley XP Series) scheduled for publication in October 2001.
Presented by developerWorks, your source for great tutorials
ibm.com/developerWorks
Java sockets 101 Page 2
Section 2. Socket basics
Introduction
Most programmers, whether they're coding in the Java language or not, don't want to know
much about low-level details of how applications on different computers communicate with
each other. Programmers want to deal with higher-level abstractions that are easier to
understand. Java programmers want objects that they can interact with via an intuitive
interface, using the Java constructs with which they are familiar.
Sockets live in both worlds -- the low-level details that we'd rather avoid and the abstract
layer we'd rather deal with. This section will explore just enough of the low-level details to
make the abstract application understandable.
Presented by developerWorks, your source for great tutorials
ibm.com/developerWorks
Java sockets 101 Page 3
Computer networking 101
Computers operate and communicate with one
another in a very simple way. Computer chips are
a collection of on-off switches that store and
transmit data in the form of 1s and 0s. When
computers want to share data, all they need to do
is stream a few million of these bits and bytes back
and forth, while agreeing on speed, sequence,
timing, and such. How would you like to worry
about those details every time you wanted to
communicate information between two
applications?
To avoid that, we need a set of packaged protocols
that can do the job the same way every time. That
would allow us to handle our application-level work
without having to worry about the low-level
networking details. These sets of packaged
protocols are called stacks. The most common
stack these days is TCP/IP. Most stacks (including
TCP/IP) adhere roughly to the International
Standards Organization (ISO) Open Systems
Interconnect Reference Model (OSIRM). The
OSIRM says that there are seven logical layers in
a reliable framework for computer networking (see
the diagram). Companies all over have contributed
something that implements some of the layers in
this model, from generating the electrical signals
(pulses of light, radio frequency, and so on) to
presenting the data to applications. TCP/IP maps
to two layers in the OSI model, as shown in the
diagram.
We won't go into the details of the layers too much,
but we want you to be aware of where sockets fit.
Presented by developerWorks, your source for great tutorials
ibm.com/developerWorks
Java sockets 101 Page 4
Where sockets fit
Sockets reside roughly at the Session Layer of the
OSI model (see the diagram). The Session Layer
is sandwiched between the application-oriented
upper layers and the real-time data
communication lower layers. The Session Layer
provides services for managing and controlling
data flow between two computers. As part of this
layer, sockets provide an abstraction that hides the
complexities of getting the bits and bytes on the
wire for transmission. In other words, sockets allow
us to transmit data by having our application
indicate that it wants to send some bytes. Sockets
mask the nuts and bolts of getting the job done.
When you pick up your telephone, you provide
sound waves to a sensor that converts your voice
into electrically transmittable data. The phone is a
human's interface to the telecommunications
network. You aren't required to know the details of
how your voice is transported, only the party to
whom you would like to connect. In the same
sense, a socket acts as a high-level interface that
hides the complexities of transmitting 1s and 0s
across unknown channels.
Presented by developerWorks, your source for great tutorials
ibm.com/developerWorks
Java sockets 101 Page 5