In my previous posts, I shared about connection less communication with socket API. This post is about connection oriented communication with socket API.
In the case of connection less communication, the packets are received at the receiver at random. Which means, if you consider packets received consecutively, the collection may contain packets in a random order and may be from many senders.
Connection oriented communication is also called Stream-mode communication where the packets are written to the stream by a sender and the receiver reads these packets from the stream.
In java, to implement Connection Oriented socket communication, two classes are used
1. ServerSocket (to establish and maintain the connection)
2. Socket (for data exchange)
Demo1:
The following program demonstrates a basic Connection Oriented communication between two processes, ConnectionAcceptor and ConnectionRequestor, running on two different hosts. ConnectionAcceptor process accepts a connection from ConnectionRequestor process and sends back a response provided in the command line arguments. ConnectionRequestor process sends a connection request to the ConnectionRequestor process, gets a response and prints the received response.
Code (ConnectionAcceptor):
import java.net.*;
import java.io.*;
/*
* ****** CONNECTION ORIENTED SOCKET PROGRAMMING *****
* Author : @NaveenKumar
* Program : Connection Acceptor (Server)
* Date : 29/10/2013
* File Name : ConnectionAcceptor.java
*/
public class ConnectionAcceptor
{
public static void main(String args[])
{
if(args.length != 2)
{
System.out.println("Error in required no. of parameters");
}// end of if
else
{
try{
int portNo = Integer.parseInt(args[0]);
String message = args[1];
ServerSocket connectionSocket = new
ServerSocket(portNo);
System.out.println("Waiting for connection");
Socket dataSocket = connectionSocket.accept();
System.out.println("Connection Accepted");
OutputStream outStream =
dataSocket.getOutputStream();
PrintWriter socketOutput = new PrintWriter(new OutputStreamWriter(outStream));
socketOutput.println(message);
socketOutput.flush();
System.out.println("Message sent");
try{
Thread.sleep(5000);
System.out.println("Waiting time is over");
}catch(Exception e)
{
e.printStackTrace();
}
socketOutput.println(message);
socketOutput.flush();
dataSocket.close();
System.out.println("Data socket closed");
connectionSocket.close();
System.out.println("connection socket closed");
}catch(Exception ex){
ex.printStackTrace();
}//end of catch
}//end of else
}// end of main
}// end of class ConnectionAcceptor
Code (Connection Requestor):
import java.net.*;
import java.io.*;
/*
* ****** CONNECTION ORIENTED SOCKET PROGRAMMING *****
* Author : @NaveenKumar
* Program : Connection Requestor (client)
* Date : 29/10/2013
* File Name : ConnectionRequestor.java
*/
public class ConnectionRequestor
{
public static void main(String args[])
{
if(args.length != 2)
{
System.out.println("Insufficient parameters");
}// end of if
else
{
try{
InetAddress acceptorHost =InetAddress.getByName(args[0]);
int acceptorPort = Integer.parseInt(args[1]);
Socket mySocket = new Socket(acceptorHost, acceptorPort);
System.out.println("Connection request granted");
InputStream inStream = mySocket.getInputStream();
BufferedReader socketInput = new BufferedReader(new
InputStreamReader(inStream));
System.out.println("waiting to read");
String message = socketInput.readLine();
System.out.println("Message received:\t"+message);
mySocket.close();
System.out.println("Data Socket closed");
}catch(Exception ex){
ex.printStackTrace();
}//end of catch
}// end of else
}// end of main
} // end of ConnectionRequestor
Executing Procedure:
1. Deploy the above programs in two different nodes which are connected on a network
2. Open terminal (command prompt) on both the nodes
3. navigate to the above java file locations
4. On terminal 1, javac ConnectionAcceptor.java (press enter),
java ConnectionAcceptor 3000 hai (press enter)
5. On the other terminal, javac ConnectionRequestor.java (press enter)
java ConnectionRequestor 192.168.10.1 3000 (press enter)
192.168.10.1 is the IP address of the node that is running the ConnectionAcceptor process and 3000 is the port number on which it is listening.
'hai' is the response send back to the ConnectionRequestor process by ConnectorAcceptor process.
Demo 2:
Now, a modified version of the above ConnectionAcceptor is as follows. The modification provided is that, instead of accepting one single connection, it can accept multiple connections. The ConnectionAcceptor process is not terminated.
for better understanding, the new ConnectionAcceptor is renamed as Server.
Code (Server):
import java.net.*;
import java.io.*;
/*
* ****** CONNECTION ORIENTED SOCKET PROGRAMMING *****
* Author : @NaveenKumar
* Program : Server
* Date : 29/10/2013
* File Name : Server.java
*/
public class Server
{
public static void main(String args[])
{
if(args.length != 2)
{
System.out.println("Error in required no. of parameters");
}// end of if
else
{
try{
int portNo = Integer.parseInt(args[0]);
String message = args[1];
ServerSocket connectionSocket = new ServerSocket(portNo);
System.out.println("Waiting for connection");
int i,j;
i = j = 1;
while(i==j)
{
Socket dataSocket = connectionSocket.accept();
System.out.println("Connection Accepted");
OutputStream outStream = dataSocket.getOutputStream();
PrintWriter socketOutput = new PrintWriter(new
OutputStreamWriter(outStream));
socketOutput.println(message);
socketOutput.flush();
System.out.println("Message sent");
try{
Thread.sleep(5000);
System.out.println("Waiting time is over");
}catch(Exception e)
{
e.printStackTrace();
}
socketOutput.println(message);
socketOutput.flush();
dataSocket.close();
System.out.println("Data socket closed");
}
connectionSocket.close();
System.out.println("connection socket closed");
}catch(Exception ex){
ex.printStackTrace();
}//end of catch
}//end of else
}// end of main
}// end of class Server
Code (Client):
import java.net.*;
import java.io.*;
/*
* ****** CONNECTION ORIENTED SOCKET PROGRAMMING *****
* Author : @NaveenKumar
* Program : Client
* Date : 29/10/2013
* File Name : Client.java
*/
public class Client
{
public static void main(String args[])
{
if(args.length != 2)
{
System.out.println("Insufficient parameters");
}// end of if
else
{
try{
InetAddress acceptorHost = InetAddress.getByName(args[0]);
int acceptorPort = Integer.parseInt(args[1]);
Socket mySocket = new Socket(acceptorHost, acceptorPort);
System.out.println("Connection request granted");
InputStream inStream = mySocket.getInputStream();
BufferedReader socketInput = new BufferedReader(new
InputStreamReader(inStream));
System.out.println("waiting to read");
String message = socketInput.readLine();
System.out.println("Message received:\t"+message);
mySocket.close();
System.out.println("Data Socket closed");
}catch(Exception ex){
ex.printStackTrace();
}//end of catch
}// end of else
}// end of main
} // end of Client
Execution:
One Server process running on a host on a network, multiple Client processes running on multiple hosts on the network.
1. Deploy the above programs in different hosts which are connected on a network
2. Open terminal (command prompt) on both the nodes
3. navigate to the above java file locations
4. On terminal 1, javac Server.java (press enter),
java Server 3000 hai (press enter)
5. On the other terminals, javac Client.java (press enter)
java Client 192.168.10.1 3000 (press enter)
192.168.10.1 and 3000 are the IP address and port number of the Server Process.
Try executing multiple clients simultaneously and observe the happenings.
If you dont have multiple hosts available, then for demonstration purpose, you can do execution on localhost itself as demonstrated in the class.
In the case of connection less communication, the packets are received at the receiver at random. Which means, if you consider packets received consecutively, the collection may contain packets in a random order and may be from many senders.
Connection oriented communication is also called Stream-mode communication where the packets are written to the stream by a sender and the receiver reads these packets from the stream.
In java, to implement Connection Oriented socket communication, two classes are used
1. ServerSocket (to establish and maintain the connection)
2. Socket (for data exchange)
Demo1:
The following program demonstrates a basic Connection Oriented communication between two processes, ConnectionAcceptor and ConnectionRequestor, running on two different hosts. ConnectionAcceptor process accepts a connection from ConnectionRequestor process and sends back a response provided in the command line arguments. ConnectionRequestor process sends a connection request to the ConnectionRequestor process, gets a response and prints the received response.
Code (ConnectionAcceptor):
import java.net.*;
import java.io.*;
/*
* ****** CONNECTION ORIENTED SOCKET PROGRAMMING *****
* Author : @NaveenKumar
* Program : Connection Acceptor (Server)
* Date : 29/10/2013
* File Name : ConnectionAcceptor.java
*/
public class ConnectionAcceptor
{
public static void main(String args[])
{
if(args.length != 2)
{
System.out.println("Error in required no. of parameters");
}// end of if
else
{
try{
int portNo = Integer.parseInt(args[0]);
String message = args[1];
ServerSocket connectionSocket = new
ServerSocket(portNo);
System.out.println("Waiting for connection");
Socket dataSocket = connectionSocket.accept();
System.out.println("Connection Accepted");
OutputStream outStream =
dataSocket.getOutputStream();
PrintWriter socketOutput = new PrintWriter(new OutputStreamWriter(outStream));
socketOutput.println(message);
socketOutput.flush();
System.out.println("Message sent");
try{
Thread.sleep(5000);
System.out.println("Waiting time is over");
}catch(Exception e)
{
e.printStackTrace();
}
socketOutput.println(message);
socketOutput.flush();
dataSocket.close();
System.out.println("Data socket closed");
connectionSocket.close();
System.out.println("connection socket closed");
}catch(Exception ex){
ex.printStackTrace();
}//end of catch
}//end of else
}// end of main
}// end of class ConnectionAcceptor
Code (Connection Requestor):
import java.net.*;
import java.io.*;
/*
* ****** CONNECTION ORIENTED SOCKET PROGRAMMING *****
* Author : @NaveenKumar
* Program : Connection Requestor (client)
* Date : 29/10/2013
* File Name : ConnectionRequestor.java
*/
public class ConnectionRequestor
{
public static void main(String args[])
{
if(args.length != 2)
{
System.out.println("Insufficient parameters");
}// end of if
else
{
try{
InetAddress acceptorHost =InetAddress.getByName(args[0]);
int acceptorPort = Integer.parseInt(args[1]);
Socket mySocket = new Socket(acceptorHost, acceptorPort);
System.out.println("Connection request granted");
InputStream inStream = mySocket.getInputStream();
BufferedReader socketInput = new BufferedReader(new
InputStreamReader(inStream));
System.out.println("waiting to read");
String message = socketInput.readLine();
System.out.println("Message received:\t"+message);
mySocket.close();
System.out.println("Data Socket closed");
}catch(Exception ex){
ex.printStackTrace();
}//end of catch
}// end of else
}// end of main
} // end of ConnectionRequestor
Executing Procedure:
1. Deploy the above programs in two different nodes which are connected on a network
2. Open terminal (command prompt) on both the nodes
3. navigate to the above java file locations
4. On terminal 1, javac ConnectionAcceptor.java (press enter),
java ConnectionAcceptor 3000 hai (press enter)
5. On the other terminal, javac ConnectionRequestor.java (press enter)
java ConnectionRequestor 192.168.10.1 3000 (press enter)
192.168.10.1 is the IP address of the node that is running the ConnectionAcceptor process and 3000 is the port number on which it is listening.
'hai' is the response send back to the ConnectionRequestor process by ConnectorAcceptor process.
Demo 2:
Now, a modified version of the above ConnectionAcceptor is as follows. The modification provided is that, instead of accepting one single connection, it can accept multiple connections. The ConnectionAcceptor process is not terminated.
for better understanding, the new ConnectionAcceptor is renamed as Server.
Code (Server):
import java.net.*;
import java.io.*;
/*
* ****** CONNECTION ORIENTED SOCKET PROGRAMMING *****
* Author : @NaveenKumar
* Program : Server
* Date : 29/10/2013
* File Name : Server.java
*/
public class Server
{
public static void main(String args[])
{
if(args.length != 2)
{
System.out.println("Error in required no. of parameters");
}// end of if
else
{
try{
int portNo = Integer.parseInt(args[0]);
String message = args[1];
ServerSocket connectionSocket = new ServerSocket(portNo);
System.out.println("Waiting for connection");
int i,j;
i = j = 1;
while(i==j)
{
Socket dataSocket = connectionSocket.accept();
System.out.println("Connection Accepted");
OutputStream outStream = dataSocket.getOutputStream();
PrintWriter socketOutput = new PrintWriter(new
OutputStreamWriter(outStream));
socketOutput.println(message);
socketOutput.flush();
System.out.println("Message sent");
try{
Thread.sleep(5000);
System.out.println("Waiting time is over");
}catch(Exception e)
{
e.printStackTrace();
}
socketOutput.println(message);
socketOutput.flush();
dataSocket.close();
System.out.println("Data socket closed");
}
connectionSocket.close();
System.out.println("connection socket closed");
}catch(Exception ex){
ex.printStackTrace();
}//end of catch
}//end of else
}// end of main
}// end of class Server
Code (Client):
import java.net.*;
import java.io.*;
/*
* ****** CONNECTION ORIENTED SOCKET PROGRAMMING *****
* Author : @NaveenKumar
* Program : Client
* Date : 29/10/2013
* File Name : Client.java
*/
public class Client
{
public static void main(String args[])
{
if(args.length != 2)
{
System.out.println("Insufficient parameters");
}// end of if
else
{
try{
InetAddress acceptorHost = InetAddress.getByName(args[0]);
int acceptorPort = Integer.parseInt(args[1]);
Socket mySocket = new Socket(acceptorHost, acceptorPort);
System.out.println("Connection request granted");
InputStream inStream = mySocket.getInputStream();
BufferedReader socketInput = new BufferedReader(new
InputStreamReader(inStream));
System.out.println("waiting to read");
String message = socketInput.readLine();
System.out.println("Message received:\t"+message);
mySocket.close();
System.out.println("Data Socket closed");
}catch(Exception ex){
ex.printStackTrace();
}//end of catch
}// end of else
}// end of main
} // end of Client
Execution:
One Server process running on a host on a network, multiple Client processes running on multiple hosts on the network.
1. Deploy the above programs in different hosts which are connected on a network
2. Open terminal (command prompt) on both the nodes
3. navigate to the above java file locations
4. On terminal 1, javac Server.java (press enter),
java Server 3000 hai (press enter)
5. On the other terminals, javac Client.java (press enter)
java Client 192.168.10.1 3000 (press enter)
192.168.10.1 and 3000 are the IP address and port number of the Server Process.
Try executing multiple clients simultaneously and observe the happenings.
If you dont have multiple hosts available, then for demonstration purpose, you can do execution on localhost itself as demonstrated in the class.