Java Reference
In-Depth Information
Another key point regarding the socket client is that it implements
DBClient
. This ensures
that our
rent
and
return
methods are available to any client using
DVDSocketServer
. The
DvdSocketClient.java
is displayed in Listing 7-1. The entire source code for the sockets pack-
age can be downloaded from the Apress web site (
http://www.apress.com
) in the Source Code
section—more details on what can be downloaded and how the code can be compiled and
packaged are listed in Chapter 9.
â–
Tip
On Linux and most Unix computers, you can also specify
localhost.localdomain
as the host-
name. You can also use the
loopback
addresses (127.0.0.0-127.0.0.254) instead of the hostname.
Accepted practice is to use 127.0.0.1 as the standard loopback address, leaving the other potential
loopback
addresses for specialist purposes (e.g., for testing multiple identical applications on different
addresses simultaneously).
Listing 7-1.
DVDSocketClient.java
public class DvdSocketClient implements DBClient {
/**
* The socket client that gets instaniated for the socket connection.
*/
private Socket socket = null;
/**
* The outputstream used to write a serialized object to a socket server.
*/
private ObjectOutputStream oos = null;
/**
* The inputstream used to read a serialized object (a response)
* from the socket server.
*/
private ObjectInputStream ois = null;
/**
* The IP address of the machine the client is going to attempt a
* connection.
*/
private String ip = null;
/**
* The port number we will be connecting on.
*/
private int port = 3000;
/**
* Default constructor.
*/
public DvdSocketClient () throws UnknownHostException, IOException {
this("localhost", "3000");
}