Friday, July 12, 2013

Client/Server Computing

Networking is tightly integrated in Java. Socket-based communication is provided that enables programs to communicate through designated sockets. Sockets are the endpoints of logical connections between two hosts and can be used to send and receive data. Java treats socket communications much as it treats I/O operations; thus programs can read from or write to sockets as easily as they can read from or write to files. Network programming usually involves a server and one or more clients. The client sends requests to the server, and the server responds to the requests. The client begins by attempting to establish a connection to the server. The server can accept or deny the connection. Once a connection is established, the client and the server communicate through sockets.
The server must be running when a client starts. The server waits for a connection request from a client. The statements needed to create a server and a client are shown in figur 1.

Figur 1. The server and client exchange data through I/O streams on top of the socket.

To establish a server, you need to create a server socket and attach it to a port, which is where the server listens for connections. The port identifies the TCP service on the socket. Port numbers range from 0 to 65536, but port numbers 0 to 1024 are reserved for privileged services. For instance, the email server runs on port 25, and the Web server usually runs on port 80.

You can choose any port number that is not currently used by any other process. The following statement creates a server socket serverSocket:

ServerSocket serverSocket = new ServerSocket(port);

After a server socket is created, the server can use the following statement to listen for connections:

Socket socket = serverSocket.accept();

This statement waits until a client connects to the server socket. The client issues the following statement to request a connection to a server:

Socket socket = new Socket(serverName, port);

This statement opens a socket so that the client program can communicate with the server. serverName is the server's Internet host name or IP address. The following statement creates a socket at port 8000 on the client machine to connect to the host 130.254.204.36:

Socket socket = new Socket("130.254.204.36", 8000);

Alternatively, you can use the domain name to create a socket, as follows:

Socket socket = new Socket("drake.armstrong.edu";

After the server accepts the connection, communication between server and client is conducted the same as for I/O streams. The statements needed to create the streams and to exchange data between them are shown in Figure 2.

Figur 2. The server and client exchange data through I/O streams on top of the socket.

To get an input stream and an output stream, use the getInputStream() and getOutputStream() methods on a socket object. For example, the following statements create an InputStream stream called input and an OutputStream stream called output from a socket:

InputStream input = socket.getInputStream();

OutputStream output = socket.getOutputStream();

The InputStream and OutputStream streams are used to read or write bytes. You can use DataInputStream, DataOutputStream, BufferedReader, and PrintWriter to wrap on the InputStream and OutputStream to read or write data, such as int, double, or String. The following statements, for instance, create a DataInputStream stream, input, and a DataOutput stream, output, to read and write primitive data values:

DataInputStream input = new DataInputStream (socket.getInputStream());

DataOutputStream output = new DataOutputStream (socket.getOutputStream());

The server can use input.readDouble() to receive a double value from the client, and output.writeDouble(d) to send double value d to the client.

Source of original statements:
Liang, Y. Daniel.
Introduction to Java programming : comprehensive version/Y. Daniel Liang. 6 th ed.

0 comments:

Post a Comment