You are on page 1of 27

CN SEM

Week -4:

TOPOLOGIES:

BUS:

MESH:
RING:

STAR:
Week -5a

BITSTUFFING:

public class BitStuffing {


public static void main(String[] args) {
String originalData = "0111111011001111"; // Example data
String stuffedData = bitStuff(originalData);
System.out.println("Original Data: " + originalData);
System.out.println("Stuffed Data: " + stuffedData);
String destuffedData = bitDestuff(stuffedData);
System.out.println("Destuffed Data: " + destuffedData);
}

public static String bitStuff(String data) {


String stuffedData = "01111110"; // Initial flag sequence
int count = 0; // Counter for consecutive 1s

for (char bit : data.toCharArray()) {


if (bit == '1') {
count++;
} else {
count = 0;
}

stuffedData += bit;

if (count == 5) {
stuffedData += '0'; // Insert a stuffed bit
count = 0;
}
}

stuffedData += "01111110"; // Final flag sequence


return stuffedData;
}

public static String bitDestuff(String stuffedData) {


String destuffedData = "";
int count = 0; // Counter for consecutive 1s

for (int i = 8; i < stuffedData.length() - 8; i++) { // Skip initial


and final flags
char bit = stuffedData.charAt(i);
destuffedData += bit;

if (bit == '1') {
count++;
} else {
count = 0;
}

if (count == 5 && i < stuffedData.length() - 8) {


i++; // Skip the next stuffed bit
count = 0;
}
}

return destuffedData;
}
}

OUTPUT:

BYTESTUFFING:

public class ByteStuffing {


public static void main(String[] args) {
String data = "DLE ETX This is a sample DLE ETX data frame DLE ETX";
String startDelimiter = "DLE";
String endDelimiter = "ETX";
String controlEscape = "STX";

// Perform byte stuffing


String stuffedData = byteStuff(data, startDelimiter, endDelimiter,
controlEscape);

// Display the stuffed data


System.out.println("Original Data: " + data);
System.out.println("Stuffed Data: " + stuffedData);

// Perform byte unstuffing


String unstuffedData = byteUnstuff(stuffedData, startDelimiter,
endDelimiter, controlEscape);

// Display the unstuffed data


System.out.println("Unstuffed Data: " + unstuffedData);
}

public static String byteStuff(String data, String startDelimiter, String


endDelimiter, String controlEscape) {
// Perform byte stuffing
String stuffedData = data.replace(startDelimiter, controlEscape +
startDelimiter);
stuffedData = stuffedData.replace(endDelimiter, controlEscape +
endDelimiter);
return stuffedData;
}

public static String byteUnstuff(String stuffedData, String


startDelimiter, String endDelimiter, String controlEscape) {
// Perform byte unstuffing
String unstuffedData = stuffedData.replace(controlEscape +
startDelimiter, startDelimiter);
unstuffedData = unstuffedData.replace(controlEscape + endDelimiter,
endDelimiter);
return unstuffedData;
}
}

OUTPUT:

Week -5 b

STOP AND WAIT PROTOCOL:

import java.util.Random;

class StopAndWait {
static final int FRAME_SIZE = 5;
static final int TIMEOUT = 1000; // 1 second

public static void main(String[] args) {


Sender sender = new Sender();
Receiver receiver = new Receiver(sender);

sender.start();
receiver.start();
}
}

class Sender extends Thread {


private Receiver receiver;

public Sender() {
}

public void setReceiver(Receiver receiver) {


this.receiver = receiver;
}

public void run() {


for (int i = 1; i <= StopAndWait.FRAME_SIZE; i++) {
System.out.println("Sender: Sending Frame " + i);
receiver.receiveFrame(i);
try {
Thread.sleep(StopAndWait.TIMEOUT);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

class Receiver extends Thread {


private Sender sender;
private Random rand = new Random();

public Receiver(Sender sender) {


this.sender = sender;
this.sender.setReceiver(this);
}

public void receiveFrame(int frameNumber) {


if (rand.nextBoolean()) {
System.out.println("Receiver: Received Frame " + frameNumber);
System.out.println("Receiver: ACK for Frame " + frameNumber + "
sent.");
} else {
System.out.println("Receiver: Frame " + frameNumber + "
dropped.");
}
}

public void run() {


// Receiver doesn't actively send anything in stop-and-wait
}
}
OUTPUT:

SLIDING WINDOW PROTOCOL:

import java.util.LinkedList;
import java.util.Queue;
import java.util.Random;

class SlidingWindow {
static final int WINDOW_SIZE = 4;
static final int FRAME_SIZE = 10;

public static void main(String[] args) {


Sender sender = new Sender();
Receiver receiver = new Receiver(sender);

sender.start();
receiver.start();
}
}

class Sender extends Thread {


private Receiver receiver;
private Queue<Integer> window = new LinkedList<>();
private Random rand = new Random();

public Sender() {
}

public void setReceiver(Receiver receiver) {


this.receiver = receiver;
}
public void receiveAck(int ackNumber) {
// Simulate receiving an ACK
System.out.println("Sender: Received ACK for Frame " + ackNumber);
}

public void run() {


int frameNumber = 1;
while (frameNumber <= SlidingWindow.FRAME_SIZE || !window.isEmpty()) {
if (window.size() < SlidingWindow.WINDOW_SIZE && frameNumber <=
SlidingWindow.FRAME_SIZE) {
System.out.println("Sender: Sending Frame " + frameNumber);
window.add(frameNumber);
receiver.receiveFrame(frameNumber);
frameNumber++;
}

if (rand.nextBoolean() && !window.isEmpty()) {


int ack = window.poll();
receiveAck(ack); // Receive ACK locally in the Sender
}
}
}
}

class Receiver extends Thread {


private Sender sender;

public Receiver(Sender sender) {


this.sender = sender;
this.sender.setReceiver(this);
}

public void receiveFrame(int frameNumber) {


// Simulate receiving a frame
System.out.println("Receiver: Received Frame " + frameNumber);
if (frameNumber % 3 != 0) {
sender.receiveAck(frameNumber);
} else {
System.out.println("Receiver: Frame " + frameNumber + " is
corrupted.");
}
}

public void run() {


// Receiver doesn't actively send anything in sliding window
}
}
OUTPUT:
Week – 6:

CRC:

public class CRC{


public static void main(String[] args) {
String data = "1101101010110"; // Replace with your data set
String polynomial = "1101"; // Replace with your CRC polynomial

String crcChecksum = calculateCRC(data, polynomial);

System.out.println("Data: " + data);


System.out.println("CRC Polynomial: " + polynomial);
System.out.println("CRC Checksum: " + crcChecksum);

boolean isValid = verifyCRC(data, crcChecksum, polynomial);

if (isValid) {
System.out.println("CRC Check: Valid");
} else {
System.out.println("CRC Check: Invalid");
}
}

public static String calculateCRC(String data, String polynomial) {


int dataLength = data.length();
int polynomialLength = polynomial.length();

data = data + "0".repeat(polynomialLength - 1);


char[] dataChars = data.toCharArray();
char[] polynomialChars = polynomial.toCharArray();

for (int i = 0; i < dataLength - polynomialLength + 1; i++) {


if (dataChars[i] == '1') {
for (int j = 0; j < polynomialLength; j++) {
dataChars[i + j] = (char) ((dataChars[i + j] ==
polynomialChars[j]) ? '0' : '1');
}
}
}

String remainder = new String(dataChars, dataLength - polynomialLength


+ 1, polynomialLength - 1);
return data.substring(0, dataLength - polynomialLength + 1) +
remainder;
}

public static boolean verifyCRC(String data, String crcChecksum, String


polynomial) {
String recalculatedCRC = calculateCRC(data, polynomial);
return recalculatedCRC.equals(crcChecksum);
}
}

OUTPUT:

CRC:

import java.io.*;

class cr
{
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Generator:");
String gen = br.readLine();
System.out.println("Enter Data:");
String data = br.readLine();
String code = data;
while(code.length() < (data.length() + gen.length() - 1))
code = code + "0";
code = data + div(code,gen);
System.out.println("The transmitted Code Word is: " + code);
System.out.println("Please enter the received Code Word: ");
String rec = br.readLine();
if(Integer.parseInt(div(rec,gen)) == 0)
System.out.println("The received code word contains no errors.");
else
System.out.println("The received code word contains errors.");
}

static String div(String num1,String num2)


{
int pointer = num2.length();
String result = num1.substring(0, pointer);
String remainder = "";
for(int i = 0; i < num2.length(); i++)
{
if(result.charAt(i) == num2.charAt(i))
remainder += "0";
else
remainder += "1";
}
while(pointer < num1.length())
{
if(remainder.charAt(0) == '0')
{
remainder = remainder.substring(1, remainder.length());
remainder = remainder + String.valueOf(num1.charAt(pointer));
pointer++;
}
result = remainder;
remainder = "";
for(int i = 0; i < num2.length(); i++)
{
if(result.charAt(i) == num2.charAt(i))
remainder += "0";
else
remainder += "1";
}
}
return remainder.substring(1,remainder.length());
}
}

OUTPUT:
WEEK -7

CSMA:

import java.util.Random;

class NetworkNode {
private String nodeName;
private boolean busy;

public NetworkNode(String nodeName) {


this.nodeName = nodeName;
this.busy = false;
}

public String getNodeName() {


return nodeName;
}

public boolean isBusy() {


return busy;
}

public void transmitData() {


System.out.println(nodeName + " is transmitting data.");
busy = true;
// Simulate transmission time
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
busy = false;
System.out.println(nodeName + " finished transmitting data.");
}
}

public class CSMA{


private static final int NUM_NODES = 5;

public static void main(String[] args) {


NetworkNode[] nodes = new NetworkNode[NUM_NODES];
for (int i = 0; i < NUM_NODES; i++) {
nodes[i] = new NetworkNode("Node" + i);
}

// Simulate CSMA/CD
System.out.println("Simulating CSMA/CD:");
for (NetworkNode node : nodes) {
if (!node.isBusy()) {
// Node is not busy, can transmit
node.transmitData();
} else {
System.out.println(node.getNodeName() + " detected a
collision.");
// Simulate collision
handleCollision();
}
}

// Simulate CSMA/CA
System.out.println("\nSimulating CSMA/CA:");
for (NetworkNode node : nodes) {
if (!node.isBusy() && !isChannelBusy()) {
// Node is not busy, and the channel is not busy, can transmit
node.transmitData();
} else {
System.out.println(node.getNodeName() + " sensed the channel
is busy or is busy.");
// Simulate collision avoidance (wait for a random backoff
time)
backoff();
}
}
}

private static void handleCollision() {


System.out.println("Collision detected. Implementing backoff and
retransmission logic...");
// Simulate exponential backoff and retransmission
backoff();
}

private static boolean isChannelBusy() {


// Simulate channel sensing logic
Random random = new Random();
return random.nextBoolean();
}

private static void backoff() {


// Simulate backoff time
Random random = new Random();
int backoffTime = random.nextInt(1000);
System.out.println("Waiting for backoff time: " + backoffTime + "
milliseconds.");
try {
Thread.sleep(backoffTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

OUTPUT:
WEEK-8:

DISTANCE VECTOR:

STEPS:

step 1: selection

Take 3 computers{PC-PT PC0,1,2},3 switches{Switch-PT Switch 0,1,2},3 routers{Router - PT Router


0,1,2}

step 2: connections

connect PC0 -> Switch 0 -> Router 0

connect PC1 -> Switch 1 -> Router 1

connect PC2 -> Switch 2 -> Router 2

connect Router0 -> Router1 -> Router 2

step 3: ip configuration for PC0,1,2

select PC0 -> desktop -> Ip Configuration -> IP Address:192.168.10.2 -> enter -> Default
Gateway:192.168.10.1

select PC1 -> desktop -> Ip Configuration -> IP Address:192.168.20.2 -> enter -> Default
Gateway:192.168.20.1

select PC2 -> desktop -> Ip Configuration -> IP Address:192.168.30.2 -> enter -> Default
Gateway:192.168.30.1
step 4: router configuration

select Router0 -> Config -> Fast ethernet 0/0 -> IP Address:192.168.10.1(default gateway)

-> enter & select ON i.e Tick it

select Router1 -> Config -> Fast ethernet 0/0 -> IP Address:192.168.20.1(default gateway)

-> enter & select ON i.e Tick it

select Router2 -> Config -> Fast ethernet 0/0 -> IP Address:192.168.30.1(default gateway)

-> enter & select ON i.e Tick it

Result: u can see the red arrow changing to green arrow from switches to routers

Step 5: activating cables b/w routers

u can see the near(right) router0 clock Serial2/0 so click on router0 -> config -> Serial2/0 -> clock rate
: 64000 & IP Address:10.0.0.2 & select ON i.e Tick it

u can see the near(left) router1 Serial2/0 so click on router1 -> config -> Serial2/0 ->

clock rate : notset & IP Address:10.0.0.3 & select ON i.e Tick it

u can see the near(right) router1 clock Serial3/0 so click on router0 -> config -> Serial3/0 -> clock rate
: 64000 & IP Address:20.0.0.2 & select ON i.e Tick it

u can see the near(left) router2 Serial2/0 so click on router2 -> config -> Serial2/0 ->

clock rate : not-set & IP Address:20.0.0.3 & select ON i.e Tick it

Result: u can see the red arrow changing to green arrow between the routers

Step 6: RIP

select Router0 -> Config -> RIP -> Network: 192.168.10.1 and click on add ->Network: 10.0.0.2 and
click on add

select Router1 -> Config -> RIP -> Network: 192.168.20.1 and click on add ->Network: 10.0.0.3 and
click on add & Network: 20.0.0.2 and click on add

select Router2 -> Config -> RIP -> Network: 192.168.30.1 and click on add ->Network: 20.0.0.3 and
click on add

step 7: sending messages

select the message and send it to pc0 to pc1 & pc1 to pc2

step 8: note down the output from simulation


WEEK-9

CLIENT:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class client {


public static void main(String[] args) {
try {
Socket socket = new Socket("localhost", 12345);

BufferedReader serverInput = new BufferedReader(new


InputStreamReader(socket.getInputStream()));
PrintWriter clientOutput = new
PrintWriter(socket.getOutputStream(), true);
BufferedReader clientInput = new BufferedReader(new
InputStreamReader(System.in));

new Thread(() -> {


try {
String serverMessage;
while ((serverMessage = serverInput.readLine()) != null) {
System.out.println("Server: " + serverMessage);
}
} catch (IOException e) {
e.printStackTrace();
}
}).start();

String clientMessage;
while (true) {
clientMessage = clientInput.readLine();
clientOutput.println(clientMessage);
}

} catch (IOException e) {
e.printStackTrace();
}
}
}
SERVER:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class server {


public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(12345);
System.out.println("Server is running and waiting for
clients...");

Socket clientSocket = serverSocket.accept();


System.out.println("Client connected.");

BufferedReader clientInput = new BufferedReader(new


InputStreamReader(clientSocket.getInputStream()));
PrintWriter serverOutput = new
PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader serverInput = new BufferedReader(new
InputStreamReader(System.in));

new Thread(() -> {


try {
String clientMessage;
while ((clientMessage = clientInput.readLine()) != null) {
System.out.println("Client: " + clientMessage);
}
} catch (IOException e) {
e.printStackTrace();
}
}).start();

String serverMessage;
while (true) {
serverMessage = serverInput.readLine();
serverOutput.println(serverMessage);
}

} catch (IOException e) {
e.printStackTrace();
}
}
}
OUTPUT:

STEPS:

1. Save the codes in notepad with java extension i.e client.java & server.java.
2. Compile the codes i.e javac client.java & javac server.java.
3. Run server side first i.e java server.java.
4. Open new cmd and run java client.
WEEK 10:

FTP:

STEPS:

step 1 : Selections

Select 1 server{Server-PT Server 1},1 Router{1841 Router0},1 Switch {2950-24 Switch0}, 2 PC's{PC-PT
PC0,PC1}

Step 2: connections

Server -> Router -> Switch -> PC1 & PC2

Step 3: ASSIGN IP Adresses to all devices

click on router0 -> config -> Fastethernet0/0 ->activate by click on -> IP Address:10.10.10.1
Fastethernet0/1 ->activate by click on -> IP Address:192.168.0.1

click on PC0 -> Desktop -> IP Configuration -> IP Address:192.168.0.2 ->Default Gateway:192.168.0.1

click on PC0 -> Desktop -> IP Configuration -> IP Address:192.168.0.3 ->Default Gateway:192.168.0.1
click on Server0 -> Desktop -> IP Configuration -> IP Address:10.10.10.2 ->Default
Gateway:10.10.10.1

Step 4: change server name

click on Server0 -> config -> Display Name: FTP SERVER

Step 5: changing server to ftp server

click on Server0 -> Services -> FTP -> username: cisco1 & password:test & click on RWDRL and click
on add

Step 6: checking FTP server working or not.

click on PC0 -> Desktop -> Text editor -> write hello word,testing file for ftp server and save
it(hello.txt) now go to -> cmd : write some commands

commands:

->PC> ping 10.10.10.2

->PC> ftp 10.10.10.2

username:cisco1

password:test

230 - Logged in

->ftp>put hello.txt

transfer complete

->ftp>dir

u can see hello.txt

step 7: how to download the file {hello.txt}

go to another pc{PC1}

click on PC1 -> Desktop -> cmd -> write some commands

commands:

->PC> ftp 10.10.10.2

username:cisco1

password:test

230 - Logged in

->ftp>get hello.txt

transfer complete
WEEK 11:

TELNET:

STEPS:

step 1 : Selections
Select 1 Switch {2950T-24 Switch0}, 5 PC's{PC-PT PC0,PC1,PC2,PC3,PC4}

Step 2: connections
PC0,PC1,PC2,PC3(client) -> Switch0 -> PC5(network adminstrator)

Step 3: ASSIGN IP Adresses to all devices.


click on switch0 -> CLI (click enter) ->

Switch>
Switch>enable
Switch#configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
Switch(config)#interface vlan 1
Switch(config-if)#ip address 192.168.10.100 255.255.255.0
Switch(config-if)#no shutdown
Switch(config-if)#
LINK-5-CHANGED: Interface Vlanl, changed state to up
LINEPROTO-5-UPDOWN: Line protocol on Interface Vlanl, changed state to up
Switch(config-if)#exit
Switch(config)#line vty 0 15
Switch (config-line)#password telnet@1234
Switch(config-line)#login
Switch(config-line)#exit
Switch(config)#exit
Switch#
%SYS-5-CONFIG I: Configured from console by console
Switch#copy running-config startup-config
Destination filename [startup-config]?
Building configuration...
[OK]

Switch#configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
Switch(config) #enable password admin@1234
Switch(config)#exit
Switch#
%SYS-5-CONFIG I: Configured from console by console
Switch#copy running-config startup-config
Destination filename (startup-config)?
Building configuration...
[OK]
step 4:
click on PC4 -> Desktop -> IP Configuration -> IP Address:192.168.10.2
click on PC4 -> Desktop -> cmd: write some commands
commands:
Cisco Packet Tracer PC Command Line 1.0
C:\>telnet 192.168.10.100
Trying 192.168.10.100... Open
User Access Verification
Password: telnet@1234
Switch>enable
Password: admin@1234
Switch#configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
Switch(config)#interface fastethernet 0/3
Switch(config-if)#shutdown
Switch(config-if)#no shutdown
Switch(config-if)#

Step 5: Result
u can see the fastethernet 0/3 cable turning red when commanding shutdown and turning green
when commanding no shutdown.
WEEK 12:

RSA:

// program for RSA


// Java Program to Implement the RSA Algorithm
import java.math.*;
import java.util.*;

class RSA {
public static void main(String args[]) {
int p, q, n, z, d = 0, e, i;

// The number to be encrypted and decrypted


int msg = 12;
double c;
BigInteger msgback;

// 1st prime number p


p = 3;

// 2nd prime number q


q = 11;
n = p * q;
z = (p - 1) * (q - 1);
System.out.println("the value of z = " + z);

for (e = 2; e < z; e++) {

// e is for public key exponent


if (gcd(e, z) == 1) {
break;
}
}
System.out.println("the value of e = " + e);
for (i = 0; i <= 9; i++) {
int x = 1 + (i * z);

// d is for private key exponent


if (x % e == 0) {
d = x / e;
break;
}
}
System.out.println("the value of d = " + d);
c = (Math.pow(msg, e)) % n;
System.out.println("Encrypted message is : " + c);
// converting int value of n to BigInteger
BigInteger N = BigInteger.valueOf(n);

// converting float value of c to BigInteger


BigInteger C = BigDecimal.valueOf(c).toBigInteger();
msgback = (C.pow(d)).mod(N);
System.out.println("Decrypted message is : "
+ msgback);
}

static int gcd(int e, int z) {


if (e == 0)
return z;
else
return gcd(z % e, e);
}
}

OUTPUT:

You might also like