IBM DeveloperWorks Java Sockets.pdf

(203 KB) Pobierz
22942705 UNPDF
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 socke t ................................................. 7
4. A simple example ...................................................... 11
5. A multithreaded example ............................................. 19
6. A pooled example ...................................................... 23
7. Sockets in real life ...................................................... 29
8. Summary ................................................................ 33
9. Appendix ................................................................. 35
Java sockets 101
Page 1 of 40
Presented by developerWorks, your source for great tutorials ibm.com/developerWorks
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.
Java sockets 101
Page 2 of 40
Presented by developerWorks, your source for great tutorials ibm.com/developerWorks
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.
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
Java sockets 101
Page 3 of 40
22942705.001.png
Presented by developerWorks, your source for great tutorials ibm.com/developerWorks
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.
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
Java sockets 101
Page 4 of 40
22942705.002.png
Presented by developerWorks, your source for great tutorials ibm.com/developerWorks
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.
Exposing sockets to an
application
When you write code that uses sockets, that code
does work at the Presentation Layer . The
Presentation Layer provides a common
representation of information that the Application
Layer can use. Say you are planning to connect
your application to a legacy banking system that
understands only EBCDIC. Your application
domain objects store information in ASCII format.
In this case, you are responsible for writing code at
the Presentation Layer to convert data from
EBCDIC to ASCII, and then (for example) to
provide a domain object to your Application Layer.
Your Application Layer can then do whatever it
wants with the domain object.
The socket-handling code you write lives only at
the Presentation Layer. Your Application Layer
doesn't have to know anything about how sockets
work.
What are sockets?
Now that we know the role sockets play, the question remains: What is a socket? Bruce Eckel
describes a socket this way in his book Thinking in Java :
The socket is the software abstraction used to represent the "terminals" of a connection
between two machines. For a given connection, there's a socket on each machine, and you
can imagine a hypothetical "cable" running between the two machines with each end of the
"cable" plugged into a socket. Of course, the physical hardware and cabling between
machines is completely unknown. The whole point of the abstraction is that we don't have to
know more than is necessary.
In a nutshell, a socket on one computer that talks to a socket on another computer creates a
Java sockets 101
Page 5 of 40
22942705.003.png 22942705.004.png
Zgłoś jeśli naruszono regulamin