0% found this document useful (0 votes)
4 views3 pages

Java Socket Server Example Code

The document contains Java code for a simple socket server and client implementation. The server listens for client connections, receives messages, and responds accordingly, while the client connects to the server, sends messages, and handles responses. The server can be terminated by sending an 'exit' message from the client.

Uploaded by

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

Java Socket Server Example Code

The document contains Java code for a simple socket server and client implementation. The server listens for client connections, receives messages, and responds accordingly, while the client connects to the server, sends messages, and handles responses. The server can be terminated by sending an 'exit' message from the client.

Uploaded by

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

Java Socket Server

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

/**

* This class implements java Socket server

**/

public class SocketServerExample {

//static ServerSocket variable

private static ServerSocket server;

//socket server port on which it will listen

private static int port = 9876;

public static void main(String args[]) throws IOException, ClassNotFoundException{

//create the socket server object

server = new ServerSocket(port);

//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]("Hi Client "+message);

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

Java Socket Client

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

/**

* This class implements java socket client

* */

public class SocketClientExample {


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

InetAddress host = [Link]();

Socket socket = null;

ObjectOutputStream oos = null;

ObjectInputStream ois = null;

for(int i=0; i<5;i++){

//establish socket connection to server

socket = new Socket([Link](), 9876);

//write to socket using ObjectOutputStream

oos = new ObjectOutputStream([Link]());

[Link]("Sending request to Socket Server");

if(i==4)[Link]("exit");

else [Link](""+i);

//read the server response message

ois = new ObjectInputStream([Link]());

String message = (String) [Link]();

[Link]("Message: " + message);

//close resources

[Link]();

[Link]();

[Link](100);

You might also like