Lab Report Cn
Lab Report Cn
SERVER:
import java.util.Scanner;
import java.net.*;
import java.io.*;
//Server
public class socketlab1b {
try{
ServerSocket ss= new ServerSocket(6666);
Socket s= ss.accept();
DataInputStream i= new DataInputStream(s.getInputStream());
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 {
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 {
try{
ServerSocket ss= new ServerSocket(6666);
Socket s= ss.accept();
DataInputStream i= new DataInputStream(s.getInputStream());
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 {
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 {
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 {
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 {
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 {
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 {
try{
ServerSocket ss= new ServerSocket(6666);
Socket s= ss.accept();
DataInputStream i= new DataInputStream(s.getInputStream());
int a;
a= i.readInt();
OUTPUT:
CLIENT:
import java.util.Scanner;
import java.net.*;
import java.io.*;
//client
public class socketlab5 {
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 {
try{
ServerSocket ss= new ServerSocket(6666);
Socket s= ss.accept();
DataInputStream i= new DataInputStream(s.getInputStream());
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 {
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 {
try{
ServerSocket ss= new ServerSocket(6666);
Socket s= ss.accept();
DataInputStream i= new DataInputStream(s.getInputStream());
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 {
try{
ServerSocket ss= new ServerSocket(6666);
Socket s= ss.accept();
DataInputStream i= new DataInputStream(s.getInputStream());
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 {
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 {
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();
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 {
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 {
try{
ServerSocket ss= new ServerSocket(6666);
Socket s= ss.accept();
DataInputStream i= new DataInputStream(s.getInputStream());
int a;
a= i.readInt();
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 {
o.writeInt(a);
o.flush();
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());
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;
}
}
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());
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{
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
}
s.close();
o.close();
i.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
OUTPUT:
LABWORK 2/5/2023
SERVER:
import java.util.Scanner;
import java.net.*;
import java.io.*;
// 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.*;
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.*;
System.out.println(j+"\t\t"+adjMat[i][j]+"\t\t"+via[i][j]);
}
}
}
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