Java Reference
In-Depth Information
CHAPTER 7
■ ■ ■
Networking with Sockets
T his chapter describes how to build the Denny's DVDs networking layer by using sockets.
Sockets are a software abstraction that allows applications to communicate with each
other across a network. Any connection between two machines over IP (Internet Protocol)
requires the use of sockets. Java provides programmers with the ability to connect application
sockets, but as we will see in this chapter, much work needs to be done to ensure that clients
and servers can communicate effectively using standard sockets. To alleviate some of that
effort, Java also provides the Remote Method Invocation (RMI) framework, which is built on
top of sockets. As you learned in the previous chapter, however, using RMI introduces its own
problems. Either networking solution has its pros and cons, and it is up to you as a developer
to determine which one you feel is better for a given situation. In this chapter, we will detail a
simple sockets solution for our sample project, providing you with what you need to make an
informed decision regarding which solution to use in your Sun assignment.
Our major areas to cover include
An overview of sockets
Why you would want to use sockets
The basic information required for connecting sockets
How to build a TCP socket client
How to build a TCP socket server
Serialized objects with sockets
Socket lifecycles
Socket Overview
A socket is one of the endpoints in a communication link between two programs and is always
bound to a port. A socket connection is useful for connecting to remote machines, and send-
ing and receiving data. To create a socket connection, you will need the host address to which
you want to connect and the port number. Sockets are not specific to Java. However, all ver-
sions of Java since JDK 1.0 have provided facilities for creating sockets. As with RMI, the
discussion is demarcated by client and server considerations.
The server is a listening service that receives connection requests from socket clients.
When a request is granted, the server establishes a socket-to-socket connection. The client
199
Search WWH ::




Custom Search