This project is a client-server application that performs basic mathematical operations over a network. The server listens for client connections, receives mathematical operation requests, processes them, and sends back the computed results. The client sends requests and receives responses from the server, making it a simple yet effective demonstration of socket programming in Java.
This project was developed as part of a university assignment where the base code was already provided. Additional improvements and modifications were made to enhance functionality and demonstrate a deeper understanding of network programming.
- Establishing a TCP connection between a client and server.
- Receiving and parsing mathematical operation requests from clients.
- Performing basic arithmetic operations (addition, subtraction, multiplication, division).
- Sending results back to the client over the network.
- Handling errors such as invalid operations and division by zero.
- Server Initialization
- The server starts and listens for incoming client connections on a specified port.
- Once a client connects, the server reads input requests.
- Client Requests
- The client sends a request in the format:
OPERATION number1 number2(e.g.,ADD 5 3). - Supported operations:
ADD,SUB,MUL,DIV.
- The client sends a request in the format:
- Server Response
- The server processes the request and performs the corresponding arithmetic operation.
- The result is sent back to the client.
- If an invalid request is received, an error message is returned.
- Java Socket Programming (Client-Server Communication)
- Multithreading & Networking (Handling multiple client requests)
- String Parsing & Exception Handling (Validating and processing user inputs)
- Error Handling & Fault Tolerance (Handling invalid inputs and division by zero)
- Java (Core programming language)
- TCP Sockets (Client-Server communication protocol)
- I/O Streams (BufferedReader, PrintWriter for data exchange)
- Basic Arithmetic Operations (Addition, Subtraction, Multiplication, Division)
- Compile the Java files
javac MathServer.java MathClient.java
- Start the Server
java MathServer <port_number>
- Example:
java MathServer 5000
- Example:
- Run the Client
java MathClient <host> <port_number>
- Example:
java MathClient localhost 5000
- Example:
- Send Requests from the Client
- Example input:
ADD 10 5 - Expected response:
Result: 15.0 - Example invalid input:
DIV 10 0 - Expected response:
Error: Division by zero
- Example input:
This project provides a fundamental understanding of network communication and client-server interaction, applicable in:
- Distributed Computing – Processing requests across networked systems.
- Cloud-based API Services – Structuring data exchange between remote applications.
- Real-time Communication Systems – Handling requests in web applications.