Lab Report 3
Lab Report 3
LAB REPORT 03
Course Title: Computer Networking Lab
Course Code: CSE 312 Section: 222 D2
Student Details
Name ID
[For Teachers use only: Don’t Write Anything inside this box]
OBJECTIVES/AIM:
• To Establish Communication Using TCP Protocol.
• To Develop a Server Application and Design a Client Application.
• To Support Multiple Client Connections.
• To gather basic knowledge of socket programming using threading.
• To Facilitate User Interaction and Input Validation.
• To Support Mathematical Operations.
IMPLEMENTATION:
Problem: Mathematical operations are very useful to be implemented using TCP socket
programming. Create two processes, a server and a client using JAVA codes. The client will send
two separate integer values and a mathematical operator to the server. The server receives these
integers and a mathematical operator, then performs a mathematical operation based on the user
input. The server then sends this answer to the client, who then displays it. The client sends requests
as many times as he wishes. However, The server can serve at most 5 clients in its lifetime. The
individual client ends its connection by saying “ENDS”. For example:
• If the client sends 10, 20, and Sum, the server sends 30 to the client.
• If the client sends 20, 5, and Subtract, the server sends 15 to the client.
• If the client sends 20, 5, and Multiplication, the server sends 100 to the client.
• If the client sends 20, 5, and Division, the server sends 4 to the client.
• If the client sends 16, 3, and Modules, the server sends 1 to the client.
ServerThread ClientThread
package javaapplication11; package javaapplication11;
@Override
public void run() {
try {
while (true) {
dos.writeUTF("Enter two integers and
an operator (Sum, Subtract, Multiplication,
Division, Modules), or type 'ENDS' to close
connection:");
try {
int num1 =
Integer.parseInt(parts[0]);
int num2 =
Integer.parseInt(parts[1]);
String operator =
parts[2].toLowerCase();
String result;
switch (operator) {
case "sum":
result = String.valueOf(num1 +
num2);
break;
case "subtract":
result = String.valueOf(num1 -
num2);
break;
case "multiplication":
result = String.valueOf(num1 *
num2);
break;
case "division":
result = (num2 != 0) ?
String.valueOf(num1 / num2) : "Error: Division
by zero.";
break;
case "modules":
result = String.valueOf(num1
% num2);
break;
default:
result = "Invalid operator. Use:
Sum, Subtract, Multiplication, Division, or
Modules.";
break;
}