0% found this document useful (0 votes)
26 views7 pages

Java Socket Server-Client Program Guide

The document outlines the algorithms and source code for a client-server program using Java. The server program listens for client connections, reads messages, and responds until an exit command is received, while the client program establishes a connection, sends messages, and processes server responses. Both programs utilize ObjectInputStream and ObjectOutputStream for communication over sockets.

Uploaded by

ndr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views7 pages

Java Socket Server-Client Program Guide

The document outlines the algorithms and source code for a client-server program using Java. The server program listens for client connections, reads messages, and responds until an exit command is received, while the client program establishes a connection, sends messages, and processes server responses. Both programs utilize ObjectInputStream and ObjectOutputStream for communication over sockets.

Uploaded by

ndr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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]();
}

You might also like