Nothing Special   »   [go: up one dir, main page]

Assignment 3

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

UNIVERSITY OF ENGINNERING AND

TECHNOLOGY, TAXILA

Subject: OS THEORY
Assignment No # 02
Dated: 27/02/24 ()

Submitted By:
Wajeeha Islam (22-SE-18)
Kinza Aslam (22-SE-76)
(OMEGA)

Submitted To:

Sir Mubashir

Department of Software Engineering,


UET TAXILA
1) Change Java code at slide# 52 and 53, page#148 and 149 of the book, in
such a way that client passes server three parameters (two integer numbers
and one operation type). Server performs specified operation and return
result to client. Client prints the result.
2)At least two different connection methods (Wi-fi, hotspot, cable)
3)Implement the same using UDP and Multicast socket.
4)While using multicast socket server should send result to both clients
5)Ideally 2 to 3 students per group.
6)Also you are required to identify that if there is a limit on message size or
not?
7)Submission 27th Feb, Ms Teams, come with laptops in class,
demonstration by all groups. If any group doesn’t bring laptop, implies 03
marks in assignment. For absent group zero marks.
8)Put reminder today

TCP Socket Code and Output:

• Server code:

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class MyServer {
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(22);
while(true) {
Socket s = ss.accept();
DataInputStream din = new
DataInputStream(s.getInputStream());
DataOutputStream dout = new
DataOutputStream(s.getOutputStream());
String str=din.readUTF();
int num1 = Integer.parseInt(str);
System.out.println("Client 1st parameter: "+num1);
str=din.readUTF();
int num2 = Integer.parseInt(str);
System.out.println("Client 2nd parameter: "+num2);
str=din.readUTF();
char opt = str.charAt(0);
System.out.println("Client 3rd parameter: "+opt);
int result=operation(num1, num2, opt);
if(result==-9999)
dout.writeUTF("Invalid Operator!");
else
dout.writeUTF(String.valueOf(result));
dout.flush();
dout.close();
din.close();
s.close();
}
}catch(IOException ioe) {
System.err.println(ioe);
}
}
public static int operation(int num1, int num2, char opt) { // Operation
Method to perform arithmetic calculation
int result=0;
switch(opt) {
case '+':
result=num1+num2;
break;
case '-':
result=num1-num2;
break;
case '*':
result=num1*num2;
break;
case '/':
result=num1/num2;
break;
default:
result=-9999;
}
return result;
}
}
• Client code:

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
public class MyClient {
public static void main(String[] args) {
try {
Socket client = new Socket("192.168.137.1", 22);
DataInputStream din = new
DataInputStream(client.getInputStream());
DataOutputStream dout = new
DataOutputStream(client.getOutputStream());
BufferedReader bfr = new BufferedReader(new
InputStreamReader(System.in));
String str="";
System.out.print("First Parameter: ");
str=bfr.readLine();
dout.writeUTF(str);
dout.flush();
System.out.print("Second Parameter: ");
str=bfr.readLine();
dout.writeUTF(str);
dout.flush();
System.out.print("Third Parameter: ");
str=bfr.readLine();
dout.writeUTF(str);
dout.flush();
String result=din.readUTF();
System.out.println("Result: "+result);
dout.close();
client.close();
} catch(IOException ioe)
{
System.err.println(ioe);
}
}
}
CLIENTS CODE:

SERVER CODE

UDP Socket Code


Datagram and Datagram packet are the two main classes use for the UDP socket-based
communication. The codes for Server-client communication via UDP socket is as follow:

• Server Code

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class Server {
public static void main(String[] args) throws Exception {
DatagramSocket ds = new DatagramSocket(3000);
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, 1024);
ds.receive(dp);
String str = new String(dp.getData(), 0, dp.getLength());
System.out.println("Client 1st passed parameter: " + str);
int num1 = Integer.parseInt(str);
ds.receive(dp);
str = new String(dp.getData(), 0, dp.getLength());
System.out.println("Client 2nd passed parameter: " + str);
int num2 = Integer.parseInt(str);
ds.receive(dp);
str = new String(dp.getData(), 0, dp.getLength());
System.out.println("Client 3rd passed parameter: " + str);
char opt = str.charAt(0);
int intResult = operation(num1, num2, opt);
InetAddress clientAddress = dp.getAddress(); // Get the client's IP
int clientPort = dp.getPort(); // Get the client's port
String response;
if (intResult == -9999) {
response = "Invalid Operator!";
} else {
response = String.valueOf(intResult);
}
byte[] responseBytes = response.getBytes();
dp = new DatagramPacket(responseBytes, responseBytes.length,
clientAddress, clientPort);
ds.send(dp);
ds.close();
}
public static int operation(int num1, int num2, char opt) {
int result = 0;
switch (opt) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
default:
result = -9999;
}
return result;
}}

• Client Code

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.*;
public class MyClientUDP {
public static void main(String[] args) throws Exception {
DatagramSocket ds = new DatagramSocket(5000);
InetAddress ip = InetAddress.getByName("192.168.137.1");
BufferedReader bfr = new BufferedReader(new
InputStreamReader(System.in));
System.out.print("First Parameter: ");
String str=bfr.readLine();
DatagramPacket dp = new DatagramPacket(str.getBytes(), str.length(),
ip, 3000);
ds.send(dp);
System.out.print("Second Parameter: ");
str=bfr.readLine();
dp = new DatagramPacket(str.getBytes(), str.length(), ip, 3000);
ds.send(dp);
System.out.print("Third Parameter: ");
str=bfr.readLine();
dp = new DatagramPacket(str.getBytes(), str.length(), ip, 3000);
ds.send(dp);
byte[] buf = new byte[1024];
dp = new DatagramPacket(buf, 1024);
ds.receive(dp); //Receiving Result
String result = new String(dp.getData(), 0, dp.getLength());
System.out.println("Result: "+result);
ds.close();
}}
SERVER SIDE:

CLIENT SIDE:

Multicast Socket
Multicast Socket class will provide the functionality in UDP socket-based multicasting
communication. In multicast, the server would be able to send message to more than one
client at once.
• Server Code:

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
public class MulticastServer {
public static void main(String[] args) {
String multicastIP = "230.0.0.10";
int port = 5000;
try (MulticastSocket socket = new MulticastSocket(port)) {
InetAddress group = InetAddress.getByName(multicastIP);
socket.joinGroup(group);
byte[] buf = new byte[256];
while (true) {
DatagramPacket packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
String received = new String(packet.getData(), 0,
packet.getLength());
System.out.println("Received message: " + received);
// Process the received expression and send the result back
String[] params = received.split("\\+");
if (params.length == 2) {
int num1 = Integer.parseInt(params[0]);
int num2 = Integer.parseInt(params[1]);
int result = num1 + num2;
String resultMessage = "Result: " + result;
DatagramPacket resultPacket = new
DatagramPacket(resultMessage.getBytes(),
resultMessage.length(), group, port);
socket.send(resultPacket);
}}
} catch (IOException e) {
e.printStackTrace();
}}}

• Client1 Code:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class MulticastClient1 {
public static void main(String[] args) {
String multicastIP = "230.0.0.10";
int port = 5000;
try (DatagramSocket socket = new DatagramSocket()) {
InetAddress group = InetAddress.getByName(multicastIP);
BufferedReader reader = new BufferedReader(new
InputStreamReader(System.in));
while (true) {
System.out.print("Enter expression (e.g., 5+3): ");
String expression = reader.readLine();
// Send the expression to the server
DatagramPacket packet = new
DatagramPacket(expression.getBytes(), expression.length(), group, port);
socket.send(packet);
// Receive and display the result from the server
byte[] buf = new byte[256];
DatagramPacket resultPacket = new DatagramPacket(buf,
buf.length);
socket.receive(resultPacket);
String resultMessage = new String(resultPacket.getData(),
0, resultPacket.getLength());
System.out.println("Received result: " + resultMessage);
}
} catch (IOException e) {
e.printStackTrace();
}}}

• Client2 Code:

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
public class Client2 {
public static void main(String[] args) {
String multicastIP = "230.0.0.10";
int port = 5000;
try (MulticastSocket socket = new MulticastSocket(port)) {
InetAddress group = InetAddress.getByName(multicastIP);
socket.joinGroup(group);
byte[] buf = new byte[256];
while (true) {
// Receive and display the result from the server
DatagramPacket resultPacket = new DatagramPacket(buf,
buf.length);
socket.receive(resultPacket);
String resultMessage = new String(resultPacket.getData(), 0,
resultPacket.getLength());
System.out.println("Received result: " + resultMessage);
}
} catch (IOException e) {
e.printStackTrace();
}
}}

SERVER SIDE:

CLIENT-1 SIDE:

CLIENT-2 SIDE

You might also like