Java Programming/Client Server

In 1990s, the trend was moving away from Mainframe computing to Client/Server as the price of Unix servers dropped. The database access and some business logic were centralized on the back-end server, collecting data from the user program was installed on the front-end users' "client" computers. In the Java world there are three main ways the front-end and the back-end can communicate.
 * The client application uses JDBC (Java DataBase Connectivity API) to connect to the data base server, (Limited business logic on the back-end, unless using Stored procedures).
 * The client application uses RMI (Remote Method Invocation) to communicate with the back-end.
 * The client application uses a socket connection to communicate with the back-end.

Socket Connection Example


This page shows an example of a socket connection.

Create a Server
The Java language was developed having network computing in mind. For this reason it is very easy to create a server program. A server is a piece of code that runs all the time listening on a particular port on the computer for incoming requests. When a request arrives, it starts a new thread to service the request. See the following example:

Listening on a port

 * ComServer : class is for listening on a port for a client.


 * ServerSocket( iPortNumber ) : Creates a server socket, bound to the specified port.
 * serverSocket.accept : Listens for a connection to be made to this socket and accepts it. The method blocks until a connection is made. It returns a new Socket.

Service One Client

 * ComServerThread : This class extended from a Thread is responsible to service one client. The Socket connection will be open between the client and server. A simple protocol has to be defined between the client and server, the server has to understand what the client wants from the server. The client will send a terminate command, for which the server will terminate the socket connection. The ComServerThread class is responsible to handle all client requests, until the client sends a terminate command.


 * COM_DATA tDataFromClient : This variable will contain the data object from the client.
 * COM_DATA tDataToClient : This variable will contain the data object to be sent to the client.
 * sendDataToClient : This method sends the data object to the client.
 * getDataFromClient : This method gets the data object from the client.
 * processInput( tDataFromClient ) : This method of the class  interprets the client commands and returns the data object that will be sent back to the client.

Handling the request; implements the communication protocol

 * ComProtocol : This class implements, and encapsulates the communication logic (protocol). The protocol is the following:
 * The client initiate the connection.
 * The server accepts it and sends an acknowledgment notifying that it's ready
 * The client sends a request
 * The server response based on the request
 * The client sends a  request
 * The server acknowledge the  request and disconnects the socket connection
 * The client gets the acknowledgment to the
 * The client sends a  request
 * The server acknowledge the  request and disconnects and also stops listening of other clients.
 * The client gets the acknowledgment to the
 * The server acknowledge the  request and disconnects and also stops listening of other clients.
 * The client gets the acknowledgment to the

The Data object that goes through the network

 * COM_DATA : is data structure class that is transmitted through the network. The class contains only data.

Create the Client
A client code for a server/service is usually an API that a user application uses to interface to the server. With the help of a client API the user application does not have to know how to connect to the server to get services. The following is the client class for the above server:
 * ComClient : This class is the client API. The application is using this class to communicate with the server.


 * getServerData( COM_DATA tServData ) : This method reads the data from the server and copies the values to  object.
 * sendDataToServer( COM_DATA tServData ) : This method sends the  object through the network to the server.
 * oIn.readObject : This method returns the data object sent by the server.
 * oOut.writeObject( tServData ) : This method sends the data object to the server.