NetworkCalc is a simple client-server application using Java sockets that allows multiple clients to send arithmetic expressions to a centralized server, which evaluates the expressions and returns the computed results. ("Calc" is slang for calculator, btw.)
This project demonstrates basic networking, concurrency, and protocol design in Java.
- Java 21 or later.
First, clone or download the project files, then open a terminal or command prompt in the project root directory.
On a Unix system, compile the server and client source files using:
makeOn Windows, you may use:
.\build.bat compileThis will produce MathServer.class and MathClient.class in the bin/ project sub-directory.
Unix:
make run-serverWindows:
.\build.bat run-serverThis will start the server listening on port 12345.
Note: A
logs/sub-directory will be created automatically, and a freshserver.logfile will be generated for each server run.
Shutting Down the Server:
To gracefully terminate the server, simply type quit in the terminal where the server is running and press Enter. This will immediately close all active connections and exit the application.
In a new terminal window, run:
Unix:
make run-clientWindows:
.\build.bat run-clientYou will be prompted to enter your name once the client is running. After connecting to the server, the client will randomly generate and send three arithmetic expressions at random intervals to the server. Then, it will disconnect automatically.
You can open multiple clients at once to simulate multiple users connecting and interacting with the server simultaneously.
The app uses the following message types for communication between client and server.
Each message is implicitly terminated with a newline character \n.
-
Connection Request (JOIN):
- Format:
JOIN:<ClientName> - Example:
JOIN:Alice
- Format:
-
Calculation Request (CALC):
- Format:
CALC:<ClientName>:<ArithmeticExpression> - Example:
CALC:Alice:12+7*3
- Format:
-
Disconnection Request (LEAVE):
- Format:
LEAVE:<ClientName> - Example:
LEAVE:Alice
- Format:
-
Acknowledgment (ACK):
- Format:
ACK:<ClientName>:<ServerMessage> - Example:
ACK:Alice:Welcome
- Format:
-
Calculation Response (RES):
- Format:
RES:<ClientName>:<Result> - Example:
RES:Alice:33
- Format:
-
Error Message (ERR):
- Format:
ERR:<ErrorDescription> - Example:
ERR:Invalid Expression Format
- Format:
The server writes events to the console and into logs/server.log in the following format:
-
Log Entry Format:
[YYYY-MM-DD hh:mm:ss] <EVENT> - <ClientName>: <Details> -
Examples:
- Connection:
[2025-04-15 14:30:25] CONNECT - Alice: Connected from 127.0.0.1:54321 - Calculation Request:
[2025-04-15 14:31:00] CALC_REQUEST - Alice: Expression received: 12+7*3 - Calculation Response:
[2025-04-15 14:31:05] CALC_RESPONSE - Alice: 12+7*3 = 33 - Disconnection:
[2025-04-15 14:32:00] DISCONNECT - Alice: Client disconnected after 120 seconds
- Connection: