0% found this document useful (0 votes)
77 views6 pages

Networking Lab 4

The document describes a lab experiment on implementing socket programming using threading in Java. It involves developing a multi-threaded server that can handle multiple client connections concurrently to perform arithmetic operations. The document provides details on the client and server code design and implementation, including establishing sockets, input/output streams, threading, and exception handling.

Uploaded by

Tahsin Ahmmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views6 pages

Networking Lab 4

The document describes a lab experiment on implementing socket programming using threading in Java. It involves developing a multi-threaded server that can handle multiple client connections concurrently to perform arithmetic operations. The document provides details on the client and server code design and implementation, including establishing sockets, input/output streams, threading, and exception handling.

Uploaded by

Tahsin Ahmmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Green University of Bangladesh

Department of Computer Science and Engineering (CSE)


Faculty of Sciences and Engineering
Semester: (Fall, Year:2023), B.Sc. in CSE (Day)

Lab Report NO 04

Course Title: Computer Networking Lab


Course Code: CSE 312 Section: 212-D2

Lab Experiment Name: Implementation of Socket Programming using


threading.

Student Details

Name ID
1. Tahsin Ahmmed 212002063

Submission Date : 16-12-2023


Course Teacher’s Name : Ms. Tanpia Tasnim

Lab Report Status


Marks: ………………………………… Signature:.....................
Comments:.............................................. Date:..............................
1. TITLE OF THE LAB REPORT EXPERIMENT
Implementation of Socket Programming using threading.
2. OBJECTIVES/AIM:
 Develop a Java program that utilizes socket communication to enable data exchange
between a server and multiple clients over a network.
 Explore and apply threading concepts in Java to facilitate concurrent handling of multiple
client connections within the server application.
 Investigate how threading can improve the scalability of the server by efficiently
managing and processing incoming client requests concurrently.
 Implement mechanisms to handle synchronization and ensure data integrity and
consistency when multiple threads access shared resources or variables.
 Design and implement the interaction between the server and clients, considering various
aspects such as request handling, response generation, and error management.
 Compare the performance and efficiency of the multi-threaded socket programming
approach with a single-threaded alternative, highlighting the benefits of threading in a
networked environment.
3. PROCEDURE / DESIGN:
Client Part:
 Establish a socket connection to the server on localhost using port 3444.
 Initialize input and output streams for communication.
 Enter an infinite loop for continuous interaction.
 Prompt the user to input two numbers and an arithmetic operator.
 Send the input to the server and receive the result.
 If the operator is "ENDS," close the connection and exit the loop.
 Close resources, including the scanner, input/output streams, and socket, when
communication is complete.
 Properly handle exceptions, such as IOException, during socket communication.
Server Part:
 Initialize a ServerSocket on port 3444, awaiting client connections.
 Display server information and indicate the server is connecting and waiting.
 Accept up to five clients, assigning a dedicated thread (ClientHandler) for each.
 Perform arithmetic operations based on client requests and send results.
 Gracefully close connections upon client request ("ENDS").
 Properly handle IOException during socket communication.
 Efficiently manages multiple client connections with dynamic arithmetic operations.
4. IMPLEMENTATION
Class: Client
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;

public class Client1 {


public static void main(String[] args) throws IOException {
Socket socket = new Socket("localhost", 3444);
System.out.println("connected");
Scanner sc = new Scanner(System.in);
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
DataInputStream dis = new DataInputStream(socket.getInputStream());

while (true) {
System.out.println("Enter first number:");
int num1 = sc.nextInt();
System.out.println("Enter second number:");
int num2 = sc.nextInt();
System.out.println("Enter operator
(Sum/Subtract/Multiplication/Division/Modules/ENDS):");
String operator = sc.next();
dos.writeInt(num1);
dos.writeInt(num2);
dos.writeUTF(operator);
int result = dis.readInt();
System.out.println("Result from server: " + result);
if (operator.equals("ENDS")) {
System.out.println("Closing the connection" + socket);
socket.close();
break;
}
}
}
}

Class: Server
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class Server1 {


public static void main(String args[]) throws IOException {
ServerSocket handshake = new ServerSocket(3444);
System.out.println("Server connected at " + handshake.getLocalPort());
System.out.println("Server is connecting\n");
System.out.println("Wait for the client\n");

for (int i = 0; i < 5; i++) {


Socket com_socket = handshake.accept();
System.out.println("A new client is connected " + com_socket);
DataOutputStream dos = new
DataOutputStream(com_socket.getOutputStream());
DataInputStream dis = new
DataInputStream(com_socket.getInputStream());
System.out.println("A new thread is assigning");
Thread new_tunnel = new ClientHandler(com_socket, dis, dos);
new_tunnel.start();
}

handshake.close();
}
}

class ClientHandler extends Thread {

final Socket soc;


final DataInputStream input;
final DataOutputStream output;

public ClientHandler(Socket s, DataInputStream dis, DataOutputStream dos)


{
this.soc = s;
this.input = dis;
this.output = dos;
}

public void run() {


while (true) {
try {
int num1 = input.readInt();
int num2 = input.readInt();
String operator = input.readUTF();
int result;
switch (operator) {
case "Sum":
result = num1 + num2;
break;
case "Subtract":
result = num1 - num2;
break;
case "Multiplication":
result = num1 * num2;
break;
case "Division":
result = num1 / num2;
break;
case "Modules":
result = num1 % num2;
break;
default:
result = 0;
}
output.writeInt(result);
if (operator.equals("ENDS")) {
System.out.println("Client " + this.soc + " requested to
end the connection.");
this.soc.close();
System.out.println("Connection closed");
break;
}
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
try {
this.output.close();
this.input.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}

5. TEST RESULT / OUTPUT


6. ANALYSIS AND DISCUSSION:

The implemented Java socket server exhibits a robust approach to handling multiple client
connections concurrently through threading. The server initializes a ServerSocket on a
designated port, continuously awaiting incoming client connections. Each client connection
triggers the creation of a dedicated thread (ClientHandler), allowing simultaneous
communication with multiple clients. The server engages in a dynamic interaction with clients,
providing date and time information upon request. Notably, the code structure is modular and
well-organized, enhancing readability and potential for future extensions. The implementation
showcases effective concurrency management, scalability, and thoughtful handling of client
termination requests, contributing to a versatile and efficient server application.

You might also like