Algorithm/Flowchart of Server Program:
Step 1: Import all relevant packages
Step 2: Define the server class
Step 3: Define the main function
Step 4: Create SocketServer object using the server class
Step 5: Put in while loop and execute accept() function which returns the object of class socket
Step 6: Create an instance of objectInputStream class
Step 7: Read the data sent from client using objectInputStream
Step 8: Print the read data
Step 9 : Create an instance of objectOutputStream to write message to the socket
Step 10 : Input a message that is to be send to the client using keyboard
Step 11: Write the message that we have read in step 10 to socket of Writeobject
Step 12: Repeat steps 7, 8, 10 and 11 until either server/client sends exit message
Step 13: Close all opened resources
Algorithm/Flowchart of Client Program:
Step 1: Import all relevant packages
Step 2: Define main client class
Step 3: Define main function
Step 4: Create an object of socket class to establish socket connection to the server
Step 5: Create an instance of ObjectOutputStream class
Step 6: Write a message to the server using writeObject method and wait for the server to reply
Step 7: Read the message written by server using readObject method
Step 8: If the message says ‘bye’, close the connection. If not, receive and read the messages from
client using readObject class.
Step 9: Close all the opened resources
Source Code/ Program
1. Client program:
import [Link];
import [Link];
import [Link];
import [Link];// to encapsulate IP address
import [Link];
import [Link];
import [Link];
public class Exp4_Client
public static void main(String[] args) throws UnknownHostException,
IOException, ClassNotFoundException, InterruptedException
//get the localhost IP address, if server is running on some other IP, you need to
use that IP address
InetAddress host = [Link]();
Socket socket = null;
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
Scanner sc = new Scanner([Link]);
for(;;)
{
//Establish socket connection to server
socket = new Socket([Link](), 54);
//write to socket using ObjectOutputStream
oos = new ObjectOutputStream([Link]());
[Link]("Sending request to Socket Server");
String msg = [Link]();
if([Link]("bye"))
[Link]("bye");
break;
else
[Link](""+msg);
//read the server response message
ois = new ObjectInputStream([Link]());
String message = (String) [Link]();
[Link]("Message: " + message);
//close resources
[Link]();
[Link]();
// pause the execution of current thread for specified time in milliseconds
[Link](100);
2. Server program:
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class Exp4_Server
{
//static ServerSocket variable
private static ServerSocket server;
//socket server port on which it will listen
private static int port = 54;
public static void main(String args[]) throws IOException,
ClassNotFoundException
//create the socket server object
server = new ServerSocket(port);
Scanner sc = new Scanner([Link]);
//keep listens indefinitely until receives 'exit' call or program terminates
while(true)
[Link]("Waiting for the client request");
//creating socket and waiting for client connection
Socket socket = [Link]();
//read from socket to ObjectInputStream object
ObjectInputStream ois = new
ObjectInputStream([Link]());
//convert ObjectInputStream object to String
String message = (String) [Link]();
[Link]("Message Received: " + message);
//create ObjectOutputStream object
ObjectOutputStream oos= new ObjectOutputStream([Link]());
//write object to Socket
[Link]("Enter a message to client:");
message = [Link]();
[Link]("Hi Client "+message);
if([Link]("bye"))
break;
//close resources
[Link]();
[Link]();
[Link]();
//terminate the server if client sends exit request
if([Link]("exit")) break;
[Link]("Shutting down Socket server!!");
//close the ServerSocket object
[Link]();
}