Java Reference
In-Depth Information
Fig. 2.2
Retrieving the current machine's IP address
2.2
Using Sockets
As described in Chap. 1 , different processes (programs) can communicate with each
other across networks by means of sockets. Java implements both TCP/IP sockets
and datagram sockets (UDP sockets). Very often, the two communicating pro-
cesses will have a client/server relationship. The steps required to create client/
server programs via each of these methods are very similar and are outlined in the
following two sub-sections.
2.2.1
TCP Sockets
A communication link created via TCP/IP sockets is a connection-orientated link.
This means that the connection between server and client remains open throughout
the duration of the dialogue between the two and is only broken (under normal cir-
cumstances) when one end of the dialogue formally terminates the exchanges (via
an agreed protocol). Since there are two separate types of process involved (client
and server), we shall examine them separately, taking the server fi rst. Setting up a
server process requires fi ve steps…
1. Create a ServerSocket object .
The ServerSocket constructor requires a port number (1024-65535, for non-reserved
ones) as an argument. For example:
ServerSocket serverSocket = new ServerSocket(1234);
Search WWH ::




Custom Search