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

Exp 2

The document outlines a practical implementation of a multithreaded application for basic mathematical operations using Java. It explains the concept of threads, their differences from processes, and provides code for both a server and a client that handle mathematical operations concurrently. The output demonstrates the server's ability to process requests and return results for operations like addition, subtraction, multiplication, and division.

Uploaded by

maazchogle79
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)
19 views6 pages

Exp 2

The document outlines a practical implementation of a multithreaded application for basic mathematical operations using Java. It explains the concept of threads, their differences from processes, and provides code for both a server and a client that handle mathematical operations concurrently. The output demonstrates the server's ability to process requests and return results for operations like addition, subtraction, multiplication, and division.

Uploaded by

maazchogle79
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

Name: Ansari Mohd Taqui

ROLL NO : 21CO15
BATCH 03
PRACTICAL NO : 02

AIM : Implementation of multithreaded application (basic mathematical operations).


THEORY:

thread is also known as a lightweight process. The idea is to achieve parallelism by dividing a
process into multiple threads. For example, in a browser, multiple tabs can be different
threads. MS Word uses multiple threads: one thread to format the text, another thread to
process inputs, etc.

The primary difference is that threads within the same process run in a shared memory space,
while processes run in separate memory spaces.

Threads are not independent of one another like processes are, and as a result threads share
with other threads their code section, data section, and OS resources (like open files and
signals). But, like a process, a thread has its own program counter (PC), register set, and stack
space.

A multi-threaded program contains two or more parts that can run concurrently and each part
can handle a different task at the same time making optimal use of the available resources
especially when your computer has multiple CPUs.

Code with output:

Java code:
>Server.java import java.util.*; import java.io.*; import java.net.*; public class Server
{ public static void main(String args[]) throws Exception{

// Server server = new Server();

ServerSocket MyServer = new ServerSocket(5200); Socket ss = null; while(true) { ss = null;


try {

ss = MyServer.accept();

DataInputStream din =new DataInputStream(ss.getInputStream());

DataOutputStream dout=new DataOutputStream(ss.getOutputStream()); Thread t = new


ClientHandler(ss, din, dout); t.start(); }

catch(Exception E){ continue;


}

class ClientHandler extends Thread{

DataInputStream in;

DataOutputStream out; Socket socket; int sum; float res; boolean conn;

public ClientHandler(Socket s, DataInputStream din, DataOutputStream dout) { this.socket =


s; this.in = din; this.out = dout; this.conn = true; try { this.out.writeUTF("Service:\nAdd: +
num num\nSubtract: - num num\nMultiply: * num num\nDivision: / num num");
this.out.flush();

catch(Exception e){

System.out.println(e);

} } public void run(){ while(conn == true){ try{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String input[]


= this.in.readUTF().split(" "); switch(input[0]) { case "+":

sum = Integer.parseInt(input[1]) + Integer.parseInt(input[2]);


this.out.writeUTF(Integer.toString(sum)); break; case "-":

sum = Integer.parseInt(input[1]) Integer.parseInt(input[2]);


this.out.writeUTF(Integer.toString(sum)); break; case "*":

sum = Integer.parseInt(input[1]) * Integer.parseInt(input[2]);


this.out.writeUTF(Integer.toString(sum)); break; case "/":

res = Integer.parseInt(input[1]) /

Float.parseFloat(input[2]); this.out.writeUTF(Integer.toString(sum)); break; default: }

this.out.writeUTF("Terminating"); conn = false;

// String ip=(((InetSocketAddress)
this.socket.getRemoteSocketAddress()).getAddress()).toString().rep lace("/","");
this.out.flush();

System.out.println("Response to " + this.socket + ": " + sum);

catch(Exception E){

System.out.println(E);

} } closeConn();

} public void closeConn(){ try{ this.out.close(); this.in.close(); this.socket.close();

catch(Exception E){

System.out.println(E);

}}

>Client.java

import java.io.*; import java.util.*; import java.net.*; public class Client

public static void main(String args[])throws Exception

String send="",r="";

Socket MyClient = new Socket("127.0.0.1",5200); DataInputStream din=new

DataInputStream(MyClient.getInputStream());

DataOutputStream dout = new

DataOutputStream(MyClient.getOutputStream());

Scanner sc = new Scanner(System.in);


r=din.readUTF(); System.out.println(r); while(!send.equals("stop")) {

System.out.print("Task: "); send = sc.nextLine(); dout.writeUTF(send); dout.flush();


r=din.readUTF();

System.out.println("Answer: "+ r);

} dout.close(); din.close();

MyClient.close();

OUTPUT : Server.java

PS C:\Users\ADMIN\Desktop\21co15\ javac Server.java

PS C:\Users\ADMIN\Desktop\21co15\ java Server

Client.java

PS C:\Users\ADMIN\Desktop\21co15\ javac Client.java

PS C:\Users\ADMIN\Desktop\21co15\ java Client

Service:

Add+num num

Subtract: - num num

Multiply : * num num

Division: / num num

Task: + 10 10

Answer: 20

You might also like