We often come across a client-server architecture, it consists of a service requester machine called client and service provider machine called server. The services are exchanged between these machines using simple message passing. The message passing is of two kinds, synchronous and asynchronous. In asynchronous message passing, the machines exchange messages without any predefined order. In synchronous message passing, the machines exchange messages with a predefined order and time sequences.
Java SE provides us with all things required to communicate messages between machines. Below is the program which performs synchronous message passing between machines.
The program is divided into two java classes, class SimpleClient is to be stored on client machine and class SimpleServer is to be stored on server machine.The process of message passing starts with server being ready to accept clients, and then client can request to connect to server and both client-server can exchange messages thereafter.The client can close the connection by using ctrl-c or a blank message.
Note:In the below class files i have used the same machine as both client as well as server so we need to open two terminals/command prompts and assume one terminal/command prompt shell as client and another terminal/command prompt shell as server.If you would like to do it on different machines edit the SimpleClient class file line no.18, to the server machine’s IP address.
The process of compilation: Compile the SimpleClient and SimpleServer class files on client and server machines respectively.
Client: javac SimpleClient.java
Server: javac SimpleServer.java
The process of execution:
Follow the below steps in client – server machines in the order specified:
Server: java SimpleServer
Client: java SimpleClient
SimpleClient.java
import java.net.*;
import java.io.*;
public class SimpleClient {
Socket clientSocket;
BufferedReader clientInput;
PrintWriter clientOutput;
BufferedReader keyboardInput;
String hostname;
int port;
String inputMessage,outputMessage;
static boolean connection = false;
public SimpleClient()
{
try
{
//localhost or 127.0.0.1
hostname = "127.0.0.1";
port = 9090;
//Creating socket for communicating with server
clientSocket = new Socket(hostname,port);
//Obtaining the input stream of the client to clientInput object
clientInput = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
//Obtaining the output stream of the client to clientOutput object
clientOutput = new PrintWriter(clientSocket.getOutputStream(),true);
//Obtaining the input stream of keyboard
keyboardInput = new BufferedReader(new InputStreamReader(System.in));
connection = true;
}
catch(SocketException e)
{
System.out.println("Something went wrong : "+e.getMessage());
e.printStackTrace();
}
catch(IOException e)
{
System.out.println("Something went wrong : "+e.getMessage());
e.printStackTrace();
}
catch(Exception e)
{
System.out.println("Something went wrong : "+e.getMessage());
e.printStackTrace();
}
}
public void send()
{
try
{
System.out.print("Client : ");
//Receiving input from keyboard
outputMessage = keyboardInput.readLine();
if(outputMessage != null && outputMessage.length() != 0)
{
//Sending the input received from keyboard to server
clientOutput.println(outputMessage);
}
else
{
//Sending null and calling closeall method
clientOutput.println("");
clientOutput.flush();
closeall();
}
}
catch(SocketException e)
{
System.out.println("Something went wrong : "+e.getMessage());
e.printStackTrace();
}
catch(IOException e)
{
System.out.println("Something went wrong : "+e.getMessage());
e.printStackTrace();
}
catch(Exception e)
{
System.out.println("Something went wrong : "+e.getMessage());
e.printStackTrace();
}
}
public void receive()
{
try
{
//Receiving message from server
inputMessage = clientInput.readLine();
if(inputMessage != null)
{
//Displaying message on output screen of client
System.out.println("Server : "+inputMessage);
}
}
catch(SocketException e)
{
System.out.println("Something went wrong : "+e.getMessage());
e.printStackTrace();
}
catch(IOException e)
{
System.out.println("Something went wrong : "+e.getMessage());
e.printStackTrace();
}
catch(Exception e)
{
System.out.println("Something went wrong : "+e.getMessage());
e.printStackTrace();
}
}
public void closeall()
{
try
{
System.out.println("Closing all I/O devices");
keyboardInput.close();
clientOutput.close();
clientInput.close();
clientSocket.close();
connection = false;
}
catch(SocketException e)
{
System.out.println("Something went wrong : "+e.getMessage());
e.printStackTrace();
}
catch(IOException e)
{
System.out.println("Something went wrong : "+e.getMessage());
e.printStackTrace();
}
catch(Exception e)
{
System.out.println("Something went wrong : "+e.getMessage());
e.printStackTrace();
}
}
public static void main(String[] args) {
SimpleClient client = new SimpleClient();
while(connection != false)
{
client.receive();
client.send();
}
if(connection != false)
client.closeall();
}
}
SimpleServer.java 
import java.net.*;
import java.io.*;
public class SimpleServer {
ServerSocket connectionSocket;
Socket serverSocket;
BufferedReader serverInput;
PrintWriter serverOutput;
BufferedReader keyboardInput;
int port;
String inputMessage,outputMessage;
static boolean connection = false;
public SimpleServer()
{
port = 9090;
try
{
//Initializing Server Socket for obtaining client requests
connectionSocket = new ServerSocket(port);
System.out.println("Server started and is ready to accept clients");
//Accepting client connection request and obtaining the socket for communicating with client
serverSocket = connectionSocket.accept();
System.out.println("Client connection accepted");
//Obtaining the input stream of the server to serverInput object
serverInput = new BufferedReader(new InputStreamReader(serverSocket.getInputStream()));
//Obtaining the output stream of the server to serverOutput object
serverOutput = new PrintWriter(serverSocket.getOutputStream(),true);
//Obtaining the input stream of keyboard
keyboardInput = new BufferedReader(new InputStreamReader(System.in));
connection = true;
serverOutput.println("Server is ready to exchange messages.Start typing");
}
catch(SocketException e)
{
System.out.println("Something went wrong : "+e.getMessage());
e.printStackTrace();
}
catch(IOException e)
{
System.out.println("Something went wrong : "+e.getMessage());
e.printStackTrace();
}
catch(Exception e)
{
System.out.println("Something went wrong : "+e.getMessage());
e.printStackTrace();
}
}
public void send()
{
try
{
System.out.print("Server : ");
//Receiving Input from keyboard
outputMessage = keyboardInput.readLine();
//Sending the input received from keyboard to client
serverOutput.println(outputMessage);
}
catch(SocketException e)
{
System.out.println("Something went wrong : "+e.getMessage());
e.printStackTrace();
}
catch(IOException e)
{
System.out.println("Something went wrong : "+e.getMessage());
e.printStackTrace();
}
catch(Exception e)
{
System.out.println("Something went wrong : "+e.getMessage());
e.printStackTrace();
}
}
public void receive()
{
try
{
//Receiving message from client
inputMessage = serverInput.readLine();
if(inputMessage != null && inputMessage.length() != 0)
{
//Displaying message on output screen of server
System.out.println("Client : "+inputMessage);
}
else
{
closeall();
}
}
catch(SocketException e)
{
System.out.println("Something went wrong : "+e.getMessage());
e.printStackTrace();
}
catch(IOException e)
{
System.out.println("Something went wrong : "+e.getMessage());
e.printStackTrace();
}
catch(Exception e)
{
System.out.println("Something went wrong : "+e.getMessage());
e.printStackTrace();
}
}
public void closeall()
{
try
{
System.out.println("Closing all I/O devices");
keyboardInput.close();
serverOutput.close();
serverInput.close();
serverSocket.close();
connectionSocket.close();
connection = false;
}
catch(SocketException e)
{
System.out.println("Something went wrong : "+e.getMessage());
e.printStackTrace();
}
catch(IOException e)
{
System.out.println("Something went wrong : "+e.getMessage());
e.printStackTrace();
}
catch(Exception e)
{
System.out.println("Something went wrong : "+e.getMessage());
e.printStackTrace();
}
}
public static void main(String[] args) {
SimpleServer server = new SimpleServer();
while(connection != false)
{
server.send();
server.receive();
}
if(connection != false)
server.closeall();
}
}
If there are any bugs or modifications to be done, just leave your comments below.


1 comment
Comments feed for this article
December 15, 2010 at 4:21 pm
bet365
hi I was luck to look for your blog in google
your topic is marvelous
I obtain a lot in your topic really thanks very much
btw the theme of you website is really excellent
where can find it