Write a Java program that implements a simple client server application

Write a Java program that implements a simple client-server application

In this program, we will learn to implements a client and server application

Aim:

To Write a Java program that implements a simple client server application. The client sends data to a server. The server receives the data, uses it to produce a result, and then sends the result back to the client. The client displays the result on the console. For ex: The data sent from the client is the radius of a circle, and the result produced by the server is the area of the circle. (Use java.net)

Source Code for client server application:

/*Program for the implementation of the client-server application*/

import java.io.*;
import java.net.*;
public class Server
{
public static void main(String rr[])throws IOException
{
ServerSocket ss=new ServerSocket(8000);
Socket s=ss.accept();
DataInputStream dis=new DataInputStream(s.getInputStream());
PrintStream ps=new PrintStream(s.getOutputStream());
String str=dis.readLine();
double r=new Double(str).doubleValue();
System.out.println("Radius Given by client:"+r);
double area=3.14*r*r;
ps.println(area);
ss.close();
}
}

/*Program for the implementation of the client server application*/

import java.io.*;
import java.net.*;
public class Client
{
public static void main(String rr[])throws Exception
{
DataInputStream dis1,dis2;
String host;
PrintStream ps;
if(rr.length==0)
host="LocalHost";
else
host=rr[0];
Socket s=new Socket(host,8000);
System.out.println("Enter the radius:");
dis1=new DataInputStream(System.in);
dis2=new DataInputStream(s.getInputStream());
double r=new Double(dis1.readLine());
ps=new PrintStream(s.getOutputStream());
ps.println(r);
String a=dis2.readLine();
System.out.println("Area Calculated by server:"+a);
s.close();
}
}

Expected Output of client server application:

 Write a Java program that implements a simple client/server application
client server application

FAQ:

How do I run a client-server program in Java?

First, we have to connect with the host using the port number. Connection in server-side: ServerSocket ss=new ServerSocket(8000);
Socket s=ss.accept();
Connection in client side: Socket s=new Socket(host,8000);

What is client and server in Java?

Client means the System which is requesting to the server for data and Server means backend or system which serves the data.

What is port number in Java?

Port number is a unique identification number that is used to connect the two different systems to share data.

What is TCP and UDP?

TCP is a connection-oriented protocol and UDP is a connectionless protocol. TCP establishes a connection between client and server before to share data. UDP does not have to establish a connection before sending data.

Recommended Post:

Find the solution to the salesforce Question and many more

Pramod Kumar Yadav is from Janakpur Dham, Nepal. He was born on December 23, 1994, and has one elder brother and two elder sisters. He completed his education at various schools and colleges in Nepal and completed a degree in Computer Science Engineering from MITS in Andhra Pradesh, India. Pramod has worked as the owner of RC Educational Foundation Pvt Ltd, a teacher, and an Educational Consultant, and is currently working as an Engineer and Digital Marketer.



Leave a Comment