0% found this document useful (0 votes)
0 views

Lab Report Cn

Uploaded by

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

Lab Report Cn

Uploaded by

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

COMPUTER NETWORKS LAB REPORT

Name: Gurumayum Radhakrishnan Sharma


Student ID: DC2020BTE0120
Program: B. Tech CSE-6

Course Code: CSNT6086


LABWORK 14/10/23
1. Write a client server program to establish connection and send a
string message.

SERVER:
import java.util.Scanner;
import java.net.*;
import java.io.*;

//Server
public class socketlab1b {

public static void main(String args[])


{

try{
ServerSocket ss= new ServerSocket(6666);
Socket s= ss.accept();
DataInputStream i= new DataInputStream(s.getInputStream());

String message= new String();


message= i.readUTF();

System.out.println("Message Received: "+message);

i.close();
ss.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}

OUTPUT:

CLIENT:
import java.util.Scanner;
import java.net.*;
import java.io.*;

//client
public class socketlab1 {

public static void main(String args[])


{
String a= new String();
a="Hello";

try{
Socket s=new Socket("localhost",6666);
DataOutputStream o= new DataOutputStream(s.getOutputStream());

o.writeUTF(a);
o.flush();

o.close();
s.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}

OUTPUT:
2. Write a client server program to establish connection and send user
defined message.

SERVER:
import java.util.Scanner;
import java.net.*;
import java.io.*;

//Server
public class socketlab2b {

public static void main(String args[])


{

try{
ServerSocket ss= new ServerSocket(6666);
Socket s= ss.accept();
DataInputStream i= new DataInputStream(s.getInputStream());

String message= new String();


message= i.readUTF();

System.out.println("Message Received: "+message);

i.close();
ss.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}

OUTPUT:

CLIENT:
import java.util.Scanner;
import java.net.*;
import java.io.*;

//client
public class socketlab2 {

public static void main(String args[])


{
Scanner sc= new Scanner(System.in);
String a= new String();
System.out.println("Send a Message!: ");
a= sc.nextLine();

try{
Socket s=new Socket("localhost",6666);
DataOutputStream o= new DataOutputStream(s.getOutputStream());

o.writeUTF(a);
o.flush();
System.out.println("Message Sent!");
o.close();
s.close();
sc.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
OUTPUT:
LABWORK 15/2/23
1. Write a client Server program in Java where the user (client will
send 2 numbers to
Server and the Server would calculate the product of the numbers and
display the
result.

SERVER:
import java.util.Scanner;
import java.net.*;
import java.io.*;

//Server
public class socketlab3b {

public static void main(String args[])


{

try{
ServerSocket ss= new ServerSocket(6666);
Socket s= ss.accept();
DataInputStream i= new DataInputStream(s.getInputStream());

int a,b;
a= i.readInt();
b= i.readInt();
System.out.println("Numbers Received: "+a+" ,"+b);
int c= a*b;
System.out.println("Product= "+c);

i.close();
ss.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}

OUTPUT:
CLIENT:
import java.util.Scanner;
import java.net.*;
import java.io.*;

//client
public class socketlab3 {

public static void main(String args[])


{
int a,b;
Scanner sc= new Scanner(System.in);
try{
Socket s=new Socket("localhost",6666);
DataOutputStream o= new DataOutputStream(s.getOutputStream());
System.out.println("Enter 2 numbers:");
a= sc.nextInt();
b= sc.nextInt();

o.writeInt(a);
o.flush();

o.writeInt(b);
o.flush();

o.close();
s.close();
sc.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}

OUTPUT:
2. Write a client Server program in Java where the client sends a
message to Server and
Server would print the message. The client sends ‘stop’ and the server
will close the
connection when it receives the ‘stop’ message.

SERVER:
import java.util.Scanner;
import java.net.*;
import java.io.*;

//Server
public class socketlab4b {

public static void main(String args[])


{

try{
ServerSocket ss= new ServerSocket(6666);
Socket s= ss.accept();
DataInputStream i= new DataInputStream(s.getInputStream());
while(true)
{
String message= new String();
message= i.readUTF();
if(message.equalsIgnoreCase("stop"))
{
break;
}
else
System.out.println("Message Received: "+message);
}
i.close();
s.close();
ss.close();

}
catch(Exception e)
{
System.out.println(e);
}
}
}

OUTPUT:
CLIENT:
import java.util.Scanner;
import java.net.*;
import java.io.*;

//client
public class socketlab4 {

public static void main(String args[])


{
Scanner sc= new Scanner(System.in);
String a= new String();

try{
Socket s=new Socket("localhost",6666);
DataOutputStream o= new DataOutputStream(s.getOutputStream());
while(true)
{
System.out.println("Send a Message! ");
a= sc.nextLine();
o.writeUTF(a);
o.flush();
System.out.println("Message Sent!");
if(a.equalsIgnoreCase("stop"))
break;
}
o.close();
s.close();
sc.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}

OUTPUT:
LABWORK 21/2/23
1. Write a client Server program in Java where the user/client will
send 1 number to
Server demonstrating the number of times a particular character needs
to be printed
eg: if the client sends 5 the Server would display “*” 5 times as the
result.

SERVER:
import java.util.Scanner;
import java.net.*;
import java.io.*;

//Server
public class socketlab5b {

public static void main(String args[])


{

try{
ServerSocket ss= new ServerSocket(6666);
Socket s= ss.accept();
DataInputStream i= new DataInputStream(s.getInputStream());

int a;
a= i.readInt();

System.out.println("Number Received: "+a);


String stars="*".repeat(a);
System.out.println("Stars: "+stars);
i.close();
ss.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}

OUTPUT:
CLIENT:
import java.util.Scanner;
import java.net.*;
import java.io.*;

//client
public class socketlab5 {

public static void main(String args[])


{
int a;
Scanner sc= new Scanner(System.in);
try{
Socket s=new Socket("localhost",6666);
DataOutputStream o= new DataOutputStream(s.getOutputStream());
System.out.println("Enter a number:");
a= sc.nextInt();

o.writeInt(a);
o.flush();

o.close();
s.close();
sc.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}

OUTPUT:
2. Write a client Server program in Java where the client sends an
array of numbers to
Server and Server would sort them in ascending order and print it.

SERVER:
import java.util.Scanner;
import java.net.*;
import java.io.*;

//Server
public class socketlab6b {

public static void sort(int a[], int n)


{
int temp;
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(a[i]>=a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
}
public static void main(String args[])
{

try{
ServerSocket ss= new ServerSocket(6666);
Socket s= ss.accept();
DataInputStream i= new DataInputStream(s.getInputStream());

int a[]=new int[100];


int n= i.readInt();
for(int j=0;j<n;j++)
{
a[j]= i.readInt();
}

System.out.println("Received array: ");


for(int j=0;j<n;j++)
{
System.out.print(a[j]+"|");
}
sort(a,n);
System.out.println("\nSorted array: ");
for(int j=0;j<n;j++)
{
System.out.print(a[j]+"|");
}

i.close();
ss.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}

OUTPUT:

CLIENT:
import java.util.Scanner;
import java.net.*;
import java.io.*;
//client
public class socketlab6 {

public static void main(String args[])


{
int a[]=new int[100];
Scanner sc= new Scanner(System.in);
try{
Socket s=new Socket("localhost",6666);
DataOutputStream o= new DataOutputStream(s.getOutputStream());
System.out.println("Enter size of array:");
int n=sc.nextInt();
System.out.println("Enter "+n+" numbers:");
o.writeInt(n);
o.flush();
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
o.writeInt(a[i]);
o.flush();
}

o.close();
s.close();
sc.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}

OUTPUT:
LABWORK 22/2/23
1. Write a Client Server program in Java where the client will send an
array of numbers
and the server would be calculating the second largest element in the
array and
display it.

SERVER:
import java.util.Scanner;
import java.net.*;
import java.io.*;

//Server
public class socketlab7b {

public static int secondlargest(int a[], int n)


{
int temp;
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(a[i]>=a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
return a[(n-1)-1];
}
public static void main(String args[])
{

try{
ServerSocket ss= new ServerSocket(6666);
Socket s= ss.accept();
DataInputStream i= new DataInputStream(s.getInputStream());

int a[]=new int[100];


int n= i.readInt();
for(int j=0;j<n;j++)
{
a[j]= i.readInt();
}

System.out.println("Received array: ");


for(int j=0;j<n;j++)
{
System.out.print(a[j]+"|");
}
int b= secondlargest(a,n);
System.out.println("\nSecond largest element: "+b);
i.close();
ss.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}

OUTPUT:

CLIENT:
import java.util.Scanner;
import java.net.*;
import java.io.*;

//client
public class socketlab7 {
public static void main(String args[])
{
int a[]=new int[100];
Scanner sc= new Scanner(System.in);
try{
Socket s=new Socket("localhost",6666);
DataOutputStream o= new DataOutputStream(s.getOutputStream());
System.out.println("Enter size of array:");
int n=sc.nextInt();
System.out.println("Enter "+n+" numbers:");
o.writeInt(n);
o.flush();
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
o.writeInt(a[i]);
o.flush();
}

o.close();
s.close();
sc.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}

OUTPUT:
2. Write a Client Server program in Java where the client will send a
String to the Server
and the Server will print the ASCII value of the characters of the
String.

SERVER:
import java.util.Scanner;
import java.net.*;
import java.io.*;

//Server
public class socketlab8b {

public static void main(String args[])


{

try{
ServerSocket ss= new ServerSocket(6666);
Socket s= ss.accept();
DataInputStream i= new DataInputStream(s.getInputStream());

String message= new String();


message= i.readUTF();

System.out.println("Message Received: "+message);


System.out.println("Ascii representation of each character ");
for(int j=0;j<message.length();j++)
{
char c=message.charAt(j);
int ascii= (int)c;
System.out.println(c+":"+ascii);
}

i.close();
ss.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}

OUTPUT:
CLIENT:
import java.util.Scanner;
import java.net.*;
import java.io.*;

//client
public class socketlab8 {

public static void main(String args[])


{
Scanner sc= new Scanner(System.in);
String a= new String();
System.out.println("Send a Message!: ");
a= sc.nextLine();

try{
Socket s=new Socket("localhost",6666);
DataOutputStream o= new DataOutputStream(s.getOutputStream());

o.writeUTF(a);
o.flush();
System.out.println("Message Sent!");
o.close();
s.close();
sc.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}

OUTPUT:
LABWORK 28/3/23

1. Write a client Server program, where the client will send 2 numbers
and the server
would calculate the numbers and send the result back to the client.

SERVER:
import java.util.Scanner;
import java.net.*;
import java.io.*;

//Server
public class socketlab9b {

public static void main(String args[])


{

try{
ServerSocket ss= new ServerSocket(6666);
Socket s= ss.accept();
DataInputStream i= new DataInputStream(s.getInputStream());

int a,b;
a= i.readInt();
b= i.readInt();

System.out.println("Numbers Received: "+a+" ,"+b);


int c= a+b;
DataOutputStream o= new DataOutputStream(s.getOutputStream());
o.writeInt(c);
System.out.println("Results sent to client");

o.close();
i.close();
ss.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}

OUTPUT:
CLIENT:
import java.util.Scanner;
import java.net.*;
import java.io.*;

//client
public class socketlab9 {

public static void main(String args[])


{
int a,b;
Scanner sc= new Scanner(System.in);
try{
Socket s=new Socket("localhost",6666);
DataOutputStream o= new DataOutputStream(s.getOutputStream());
System.out.println("Enter 2 numbers:");
a= sc.nextInt();
b= sc.nextInt();

o.writeInt(a);
o.flush();

o.writeInt(b);
o.flush();

System.out.println("Numbers sent");
DataInputStream i= new DataInputStream(s.getInputStream());
int c= i.readInt();
System.out.println("Sum of the two numbers: "+c);

i.close();
o.close();
s.close();
sc.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}

OUTPUT:
2. Write a client Server program, where the client will send 1 value
and the server would
display the number of “*” corresponding to the values in the Server
window. and send it back to the client.

SERVER:
import java.util.Scanner;
import java.net.*;
import java.io.*;

//Server
public class socketlab10b {

public static void main(String args[])


{

try{
ServerSocket ss= new ServerSocket(6666);
Socket s= ss.accept();
DataInputStream i= new DataInputStream(s.getInputStream());

int a;
a= i.readInt();

System.out.println("Number Received: "+a);


String stars="*".repeat(a);
System.out.println("Results sent to client");
DataOutputStream o= new DataOutputStream(s.getOutputStream());
o.writeUTF(stars);

o.close();
i.close();
ss.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}

OUTPUT:
CLIENT:
import java.util.Scanner;
import java.net.*;
import java.io.*;

//client
public class socketlab10 {

public static void main(String args[])


{
int a;
Scanner sc= new Scanner(System.in);
try{
Socket s=new Socket("localhost",6666);
DataOutputStream o= new DataOutputStream(s.getOutputStream());
System.out.println("Enter a number:");
a= sc.nextInt();

o.writeInt(a);
o.flush();

DataInputStream i= new DataInputStream(s.getInputStream());


String stars= new String();
stars= i.readUTF();
System.out.println("Received message from server: "+stars);

i.close();
o.close();
s.close();
sc.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}

OUTPUT:
LABWORK 4/4/2023
1. Write a Client Server program to implement the following:
A restaurant comprises of a Food ordering system where the communication between
the client and the server should take place in the following way
a. Greetings to the client by the Server.
b. Displays the Menu of the Food available (5 items with prices).
c. Client can select one or more than one food from the menu.
d. Server confirms availability on each selection.
e. Client confirms order.
f. Server calculates the bill and sends to the client.
g. Greets the client goodbye and calls for a termination request.

SERVER:
import java.io.*;
import java.net.*;
import java.util.Scanner;
//server
public class socketlab11b {
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
try{
ServerSocket ss= new ServerSocket(6666);
Socket s= ss.accept();
DataInputStream i= new DataInputStream(s.getInputStream());
DataOutputStream o= new DataOutputStream(s.getOutputStream());

o.writeUTF("Hello! How may I have your order?");


o.flush();
int flag=1;
int bill=0;
String orders="\n\n\n****************\nORDER SUMMARY\n******************\n";
while(flag==1)
{
int n=i.readInt();
if(n==6)
{
System.out.println("\nServer Closing...\n");
s.close();
ss.close();
i.close();
o.close();
System.exit(0);
}
if(n<0||n>=6)
{
o.writeUTF("\nUNKNOWN ORDER!\n");
o.flush();
bill=0;
orders="";
continue;
}
int qty=i.readInt();
int state=i.readInt();

switch(n)
{
case 1: bill=bill+qty*70;
orders=orders+"Chicken Chow\tX"+qty+"\n";
break;
case 2: bill=bill+qty*90;
orders=orders+"Chicken Fried rice\tX"+qty+"\n";
break;
case 3: bill=bill+qty*50;
orders=orders+"Veg Chow\tX"+qty+"\n";
break;
case 4: bill=bill+ qty*80;
orders=orders+"Veg Fried Rice\tX"+qty+"\n";
break;
case 5: bill=bill+qty*15;
orders=orders+"Coffee\tX"+qty+"\n";
break;
}

System.out.println("Ordered: "+orders);
System.out.println("Current bill: "+bill);

switch(state)
{
case 1: o.writeInt(bill);
o.flush();
o.writeUTF(orders);
o.flush();
flag=0;
break;
case 2: flag=1;
break;
case 3: bill=0;
orders="";
flag=1;
break;
}
}

System.out.println("Orders Successful. Closing Server");


o.writeUTF("SERVER: GOODBYE!");
o.flush();
s.close();
ss.close();
i.close();
o.close();
}
catch(Exception e){
System.out.println(e);
}
}
}

OUTPUT:
CLIENT:
import java.io.*;
import java.net.*;
import java.util.Scanner;
//client
public class socketlab11 {
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
try{
Socket s= new Socket("localhost",6666);
DataInputStream i= new DataInputStream(s.getInputStream());
DataOutputStream o= new DataOutputStream(s.getOutputStream());

String message= new String();


message=i.readUTF();
System.out.println("\nServer: "+message+"\n");
int flag=1;
String orders=new String();
int bill=0;
while(flag==1)
{
System.out.println("\n\n**************MENU***********\n1.Chicken Chow -
Rs 70\n2.Chicken Fried Rice - Rs 90\n3.Veg Chow - Rs 50\n4.Veg Fried Rice - Rs
80\n5.Coffee - Rs 15\n\nSelect an order number or press 6 to exit: ");
int n= sc.nextInt();
o.writeInt(n);
o.flush();
if(n==6)
{
System.out.println("EXITING...");
s.close();
i.close();
o.close();
System.exit(0);
}
if(n<0||n>=6)
{
String reply=i.readUTF();
System.out.println(reply+"Please Select a valid order");
continue;
}
System.out.println("Enter quantity: ");
int qty= sc.nextInt();
System.out.println("\n\n1.Confirm order\n2.Order another\n3.Reset order\n");
int state=sc.nextInt();

o.writeInt(qty);
o.flush();
o.writeInt(state);
o.flush();

switch(state)
{
case 1: bill=i.readInt();
orders=i.readUTF();
flag=0;
break;
case 2: flag=1;
break;
case 3: bill=0;
orders="";
flag=1;
break;
}
}

System.out.println(orders);
System.out.println("SUBTOTAL: "+bill);
String exit= i.readUTF();
System.out.println(exit);
s.close();
i.close();
o.close();
}
catch(Exception e){
System.out.println(e);
}
}
}
OUTPUT:
LABWORK 25/4/2023
1. Write
a client Server program to develop an Airline Booking system. The following are
the requirements:
a. The
client will establish a communication
b. The
Server displays the list of seats in the client as well as server console.
c. The
Client picks one seat and sends to Server.
d. The
Server removes that seat number and displays the remaining seat numbers to th e
client console.
e. Clients
terminates the connection with a “Bye”

SERVER:

import java.util.Scanner;
import java.net.*;
import java.io.*;
//server
public class socketlab12b{

public static String[] RemoveElement(String[] seats,String selectedseat)


{
if(seats==null)
{
System.out.println("All seats are booked");
return seats;
}

String[] updatedseats= new String[seats.length-1];

for(int j=0,k=0;j<seats.length;j++)
{
if(seats[j].equals(selectedseat))
continue;

updatedseats[k++]=seats[j];
}
return updatedseats;
}
public static void main(String args[])
{
try{
ServerSocket ss= new ServerSocket(6666);
Socket s= ss.accept();
System.out.println("Server ON!");
DataInputStream i= new DataInputStream(s.getInputStream());
DataOutputStream o= new DataOutputStream(s.getOutputStream());
String seats[] = {"1A","1B","1C","2A","2B","2C","3A","3B","3C"};
int n= seats.length;

System.out.println("AVAILABLE SEATS");
for(int k=0;k<n;k++)
{
System.out.print(seats[k]+" | ");
}
// System.out.println(Arrays.toString(seats));
while(true)
{
String selectedseat= new String();
selectedseat= i.readUTF();
if(selectedseat.equalsIgnoreCase("bye"))
break;
seats= RemoveElement(seats,selectedseat);
int m=seats.length;
o.writeInt(m);
o.flush();
for(int k=0;k<seats.length;k++)
{
o.writeUTF(seats[k]);
o.flush();
}
System.out.println("Updated Seats Sent!");

}
i.close();
s.close();
ss.close();
}

catch(Exception e)
{
System.out.println(e);
}
}
}
OUTPUT:

CLIENT:

import java.util.Scanner;
import java.net.*;
import java.io.*;
//client

public class socketlab12 {


public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
try{
Socket s= new Socket("localhost",6666);
DataOutputStream o= new DataOutputStream(s.getOutputStream());
DataInputStream i= new DataInputStream(s.getInputStream());
while(true)
{
System.out.println("\nSelect a Seat or type \"Bye\" to exit: ");
String selectedseat= new String();
selectedseat= sc.nextLine();
o.writeUTF(selectedseat);
o.flush();
if(selectedseat.equalsIgnoreCase("bye"))
break;
System.out.println("Seat Chosen!");
int updatedsize= i.readInt();
String[] updatedseats= new String[updatedsize];
System.out.println("Available Seats");
for(int k=0;k<updatedsize;k++)
{
updatedseats[k]=i.readUTF();
System.out.print(updatedseats[k]+" | ");
}

}
s.close();
o.close();
i.close();

}
catch(Exception e)
{
System.out.println(e);
}
}
}
OUTPUT:
LABWORK 2/5/2023

Write a client server program to implement the following in TCP/UDP:


1. A calculator, where the client will be shown a menu by the server after an exchange of
Hi-Hello message.
2. The client will pick any random operation from the menu and send to server.
3. The server will print the result in the server window as well as the client window.

SERVER:

import java.util.Scanner;
import java.net.*;
import java.io.*;

public class socketlab13b{

public static void main(String args[]){


try{
ServerSocket ss=new ServerSocket(8888);
Socket s= ss.accept();
DataInputStream i= new DataInputStream(s.getInputStream());
DataOutputStream o=new DataOutputStream(s.getOutputStream());

String message= new String();


message=i.readUTF();
if(message.equalsIgnoreCase("Hi"))
{
o.writeUTF("Hello");
o.flush();
}
else {o.writeUTF("SERVER: UNRECOGNIZED STRING");
o.flush();
s.close();
ss.close();
System.exit(0);
}

// message="\n********\nCHOOSE AN
OPERATION:\n1.Addition\t2.Subtracton\t3.Multiplication\n4.Division\t5.Modulo\t6.Exit";
// o.writeUTF(message);
// o.flush();
while(true){

int a=i.readInt();
if(a==6)
{System.out.println("Exiting");
break;}
int num1=i.readInt();
System.out.println("Received:"+num1);
int num2=i.readInt();
System.out.println("Received:"+num2);
int result=0;

switch(a)
{
case 1: System.out.println("Operation: Addition");
result=num1+num2;
break;
case 2: System.out.println("Operation: Subtraction");
result=num1-num2;
break;
case 3: System.out.println("Operation: Multiplication");
result=num1*num2;
break;
case 4: System.out.println("Operation: Division");
result=num1/num2;
break;
case 5: System.out.println("Operation: Modulo");
result=num1%num2;
break;
case 6: System.out.println("Exiting...");
s.close();
ss.close();
o.close();
i.close();
System.exit(0);
default: System.exit(0);
}
System.out.println("Sending result:"+result);
o.writeInt(result);
o.flush();
}
s.close();
ss.close();
o.close();
i.close();
}

catch(Exception e){
System.out.println(e);
}
}
}

OUTPUT:

CLIENT:

import java.util.Scanner;
import java.io.*;
import java.net.*;

public class socketlab13{


public static void main(String args[])
{
try{
Scanner sc = new Scanner(System.in);
Socket s= new Socket("localhost",8888);
DataInputStream i=new DataInputStream(s.getInputStream());
DataOutputStream o=new DataOutputStream(s.getOutputStream());
System.out.println("Enter a message:");
String message= new String();
message= sc.nextLine();
o.writeUTF(message);
//write some code if client refuses to cut

o.flush();

message=i.readUTF();
System.out.println("SERVER:"+message);
if(!message.equalsIgnoreCase("hello")){
s.close();
o.close();
i.close();
System.exit(0);
}
while(true)
{
message="\n********\nCHOOSE AN
OPERATION:\n1.Addition\t2.Subtracton\t3.Multiplication\n4.Division\t5.Modulo\t6.Exit";

System.out.println(message);

int a;
a=sc.nextInt();
o.writeInt(a);
o.flush();
if(a==6)
break;
System.out.println("Enter 2 numbers");
int num1=sc.nextInt();
o.writeInt(num1);
o.flush();

int num2=sc.nextInt();
o.writeInt(num2);
o.flush();

int result=i.readInt();
System.out.println("RESULT OF OPERATION:"+result);

}
s.close();
i.close();
o.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
OUTPUT:
LABWORK 24/5/2023
DVR implementation

import java.util.*;

public class DVR{


static int adjMat[][];
static int rt[][];
static int via[][];
static int V;
static int E;

public static void CreateRoutingTable()


{
for(int i=0;i<V;i++)
{
System.out.println("Routing Table for: "+i+"\nDestination\tDistance\tNext_hop");
for(int j=0;j<E;j++)
{
if(adjMat[i][j]==9999)
{
via[i][j]=-1;
rt[i][j]=9999;
}
else {
via[i][j]=j;
rt[i][j]=adjMat[i][j];
}

System.out.println(j+"\t\t"+adjMat[i][j]+"\t\t"+via[i][j]);
}

}
}

public static void print_table()


{
for(int i=0;i<V;i++)
{
System.out.println("Routing table for
"+i+"\nDestination\tDistance\tNext_hop");
for(int j=0;j<E;j++)
{
System.out.println(j+"\t\t"+rt[i][j]+"\t\t"+via[i][j]);
}
}

public static void DVRalgo()


{
for(int i=0;i<V;i++)
{
for(int j=0;j<V;j++)
{
if(adjMat[i][j]!=9999)
{
int dist= adjMat[i][j];
for(int k=0;k<V;k++)
{
int inter_dist=rt[j][k];
if(via[j][k]==i)
inter_dist=9999;
if(dist+inter_dist<rt[i][k])
{
rt[i][k]=dist+inter_dist;
via[i][k]=j;
}
}
}
}
}
}

public static void main(String args[]){


Scanner sc=new Scanner(System.in);
int weight;
System.out.println("Enter number of vertices: ");
V=sc.nextInt();
System.out.println("Enter number of edges: ");
E=sc.nextInt();
adjMat= new int[V][V];
rt=new int[V][V];
via= new int[V][V];

for(int i=0;i<V;i++)
{
for(int j=0;j<V;j++)
{
if(i==j)
adjMat[i][j]=0;
else
adjMat[i][j]=9999;
}
}

int u,v;
for(int i=0;i<E;i++)
{
System.out.println("\n\nEnter a vertex number");
u=sc.nextInt();
System.out.print("Enter vertex number to connect to: ");
v=sc.nextInt();
System.out.println("Enter weight");
weight= sc.nextInt();
adjMat[u][v]= weight;
adjMat[v][u]=weight;
}

System.out.println("\nADJACENCY MATRIX\n");
for(int i=0;i<V;i++)
{
for(int j=0;j<E;j++)
{
System.out.print(adjMat[i][j]+"\t");
}
System.out.println("\n");
}
CreateRoutingTable();
int flag=1;
int pass=0;
while(flag==1)
{
pass++;
System.out.println("\n\n\nPASS:"+pass);
flag=0;
DVRalgo();
print_table();
System.out.println("Type 1 to continue)");
int a=sc.nextInt();
if(a==1)
{
flag=1;

}
pass=0;
}
}
OUTPUT:

PACKET TRACER

Switch Configuration

Switch connects multiple devices together. When a device (PC0) wants to communicate with
another device (PC2), it sends a packet to the switch, containing the IP address of the
destination. The switch then checks the IP address and sends the packet to the corresponding
device bearing the desired IP address. The configuration consists of 3 PCs connected via a
switch. The three PCs are given IP addresses from 192.168.60.1-192.168.60.3.

In order to test if the connection works, we can use a simple PDU (Protocol Data Unit) and
send an ICMP packet. Or we can ping a destination device from a source device.
Hub Configuration

A hub also connects multiple devices together. Unlike a switch, it broadcasts the packet to all
the connected devices and the device that matches the destination IP address accepts the
packet. All other devices will drop/ discard the packet. The configuration consists of 4 PCs
connected to a hub. All PCs are given an IP address under the network id 192.168.60.x.

The connection can be tested by using a simple PDU to send an ICMP packet or by pinging a
destination device from a source.
Router Configuration

Router is used to connect multiple networking devices together. It can be used to connect two
or more different networks together. It forwards packets from one network to another. In the
configuration, the two PCs on the left are set to network id 192.168.60.x and the right PCs are
set to 192.168.61.x. These are then connected using a switch and the switches are connected
using a router so the two networks can be connected. A gateway has to be set for each
network so that packets can be forwarded properly.
Connection test is carried out by either pinging a destination IP from a source or using a
simple PDU to send an ICMP packet from one device to another.
Hybrid topology configuration

Hybrid topology consists of a combination of two or more different network topologies. In


the configuration, a ring, star and bus topology is connected together. The entire hybrid
topology lies under one network id, i.e., 192.168.60.x. Each topology is made using switches
to connect the devices together and the topologies are connected together by connecting the
switches.
The connection test is done by using a simple PDU to send an ICMP packet between two
devices in different topologies or pinging a device in one topology from another device in a
different topology.

You might also like