Simple Calculator using Java Socket Programming
Last Updated :
02 Jun, 2022
Prerequisite: Socket Programming in Java
First, we understand the basics of java socket programming. Java Socket is used to communicate between two different JREs. Java socket can be connection-oriented or connection-less. In java, we have a package called "java.net". In this package, we have two classes Socket Class and Server Class. Those classes are used to create connection-oriented or connection-less programs. In this article, We will see how the java server will perform the basic operations and send back the result to the java clientÂ
Java Client
First, we create and write client-side socket code. In client socket program must know two information
- IP Address of Server and,
- Port Number
Java
// A Java program for a Client
import java.io.*;
import java.net.*;
import java.util.*;
public class Client {
private Socket s = null;
public Client(String address, int port)
{
try {
// In this Code Input Getting from a user
Scanner sc = new Scanner(System.in);
s = new Socket(address, port);
System.out.println("Connected");
// Create two objects first is dis and dos for
// input and output
DataInputStream dis
= new DataInputStream(s.getInputStream());
DataOutputStream dos
= new DataOutputStream(s.getOutputStream());
// Making a Loop
while (true) {
System.out.println(
"Enter the operation in the form operand operator operand");
System.out.println("Example 3 + 5 ");
String inp = sc.nextLine();
// Check the user input if user enter over
// then
// connect is stopped by server and user
if (inp.equals("Over"))
break;
dos.writeUTF(inp);
String ans = dis.readUTF();
System.out.println("Answer = " + ans);
}
}
catch (Exception e) {
System.out.println("Error in Connection");
}
}
public static void main(String args[])
{
// Connection With Server port 5000
Client client = new Client("127.0.0.1", 5000);
}
}
Java Server
In this Java Server, we are performing simple calculations example addition, subtraction, etc. In this Code first, we receive input from the client-side. We pass this information to the "Stringtokenizer class" this class help to extract the exact "operand and operation" to perform the operation. when the result is ready java server sends it back to the result client
Java
// A Java program for a Server
import java.io.*;
import java.net.*;
import java.util.*;
public class Server {
// initialize socket and input stream
private Socket socket = null;
// constructor with port
public Server(int port)
{
try {
// Making a ServerSocket object
// for receiving Client Request
ServerSocket ss = new ServerSocket(port);
Socket s = ss.accept();
// dis and dos object for receiving
// input from client send output to client
DataInputStream dis
= new DataInputStream(s.getInputStream());
DataOutputStream dos
= new DataOutputStream(s.getOutputStream());
while (true) {
String input = dis.readUTF();
if (input.equals("bye"))
break;
System.out.println("Equation Received");
int result = 0;
StringTokenizer st
= new StringTokenizer(input);
int oprnd1
= Integer.parseInt(st.nextToken());
String operation = st.nextToken();
int oprnd2
= Integer.parseInt(st.nextToken());
// Calculator Operation Perform By Server
if (operation.equals("+")) {
result = oprnd1 + oprnd2;
}
else if (operation.equals("-")) {
result = oprnd1 - oprnd2;
}
else if (operation.equals("/")) {
result = oprnd1 / oprnd2;
}
else if (operation.equals("*")) {
result = oprnd1 * oprnd2;
}
System.out.println("Sending the Result");
dos.writeUTF(Integer.toString(result));
}
}
catch (Exception e) {
System.out.println("Error");
}
}
public static void main(String args[])
{
// Server Object and set port number 5000
Server server = new Server(5000);
}
}
Output:
Â
Similar Reads
Basic Calculator Program Using Java Create a simple calculator which can perform basic arithmetic operations like addition, subtraction, multiplication, or division depending upon the user input. Example: Enter the numbers:Â 2Â 2 Enter the operator (+,-,*,/) + The final result: 2.0 + 2.0 = 4.0ApproachTake two numbers using the Scanner
2 min read
How to Implement a Simple Chat Application Using Sockets in Java? In this article, we will create a simple chat application using Java socket programming. Before we are going to discuss our topic, we must know Socket in Java. Java Socket connects two different JREs (Java Runtime Environment). Java sockets can be connection-oriented or connection-less. In Java, we
4 min read
Simple Bill Splitter Application using Java Servlets Pre-requisite: Java Servlets Servlets is a Java Technology for server-side programming generally used to create web applications. It is a module that runs inside a Java-enabled web server. Here, you will see the implementation by developing a Bill Splitter Application. In this Java Servlets app, th
4 min read
Calculator Using RMI(Remote Method Invocation) in Java RMI (Remote Method Invocation) is an API used to access objects running on another JVM(Server-side). It is mainly used for the creation of distributed systems and is provided in Java Rome. Stub and Skeleton are the two objects used for handling communication between client and server. The following
3 min read
Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read