0% found this document useful (0 votes)
26 views31 pages

DCCNLAB

Dccn
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)
26 views31 pages

DCCNLAB

Dccn
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/ 31

DATA COMMUNICATIONS AND

COMPUTER NETWORKS LAB

MODULE-1:
1. Study of different types of network cables and practically implement the cross wired cable and
straight through cable using Clamping tool.
2. Connect the computers in Local Area Network.
3. Study of basic network commands and network configuration commands.
4. Configure a Network Topology using packet tracer software.

MODULE-2:
1. Implementation of Error Detection / Error Correction Techniques.
2. Implementation of Stop and Wait Protocol and sliding window.
3. Implementation and study of Goback-N and selective repeat protocols.
4. Implementation of High Level Data Link Control.
5. Study of Socket Programming and Client – Server model using java.
6. Write a socket Program for Echo/Ping/Talk commands using java.
7. Implementation of distance vector routing algorithm.
8. Implementation of Link state routing algorithm.
9. Study of Network simulator (NS) and simulation of Congestion Control Algorithms using NS.
MODULE-2

1. Implementation of Error Detection / Error Correction Techniques.

PROGRAM:
import java.util.*;
class Hamming {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the number of bits for the Hamming data: ");
int n = scan.nextInt();
int a[] = new int[n];
for (int i = 0; i < n; i++) {
System.out.println("Enter bit no" + (n - i - 1) + ":");
a[n - i - 1] = scan.nextInt();
}
System.out.println("You entered:");
for (int i = 0; i < n; i++) {
System.out.println(a[n - i - 1]);
}
System.out.println();
int b[] = generateCode(a);
System.out.println("Generated code is: ");
for (int i = 0; i < b.length; i++) {
System.out.print(b[b.length - i - 1]);
}
System.out.println();
System.out.println("Enter position of a bit to alter to check for error detection at the receiver end (0 for no
error): ");
int error = scan.nextInt();
if (error != 0) {
b[error - 1] = (b[error - 1] + 1) % 2;
}
System.out.println("Sent code is: ");
for (int i = 0; i < b.length; i++) {
System.out.println(b[b.length - i - 1]);
}
System.out.println();
receive(b, b.length - a.length);
}
static int[] generateCode(int a[]) {
int b[];
int i = 0, parity_count = 0, j = 0, k = 0;
while (i < a.length) {
if (Math.pow(2, parity_count) == i + parity_count + 1) {
parity_count++;
} else {
i++;
}
}
b = new int[a.length + parity_count];
for (i = 1; i <= b.length; i++) {
if (Math.pow(2, j) == i) {
b[i - 1] = 2;
j++;
} else {
b[k + j] = a[k++];
}
}
for (i = 0; i < parity_count; i++) {
b[((int) Math.pow(2, i)) - 1] = getParity(b, i);
}
return b;
}

static int getParity(int b[], int power) {


int parity = 0;
for (int i = 0; i < b.length; i++) {
if (b[i] != 2) {
int k = i + 1;
String s = Integer.toBinaryString(k);
int x = ((Integer.parseInt(s)) / ((int) Math.pow(10, power))) % 10;
if (x == 1) {
if (b[i] == 1) {
parity = (parity + 1) % 2;
}
}
}
}
return parity;
}
static void receive(int a[], int parity_count) {
int power;
int parity[] = new int[parity_count];
String syndrome = new String();
for (power = 0; power < parity_count; power++) {
for (int i = 0; i < a.length; i++) {
int k = i + 1;
String s = Integer.toBinaryString(k);
int bit = ((Integer.parseInt(s)) / ((int) Math.pow(10, power))) % 10;
if (bit == 1) {
if (a[i] == 1) {
parity[power] = (parity[power] + 1) % 2;
}
}
}
syndrome = parity[power] + syndrome;
}
int error_location = Integer.parseInt(syndrome, 2);
if (error_location != 0) {
System.out.println("Error is at location " + error_location + ".");
a[error_location - 1] = (a[error_location - 1] + 1) % 2;
System.out.println("Corrected code is : ");
for (int i = 0; i < a.length; i++) {
System.out.println(a[a.length - i - 1]);
}
System.out.println();
} else {
System.out.println("There is no error in the data.");
}
System.out.println("Original data sent was : ");
power = parity_count - 1;
for (int i = a.length; i > 0; i--) {
if (Math.pow(2, power) != 1) {
System.out.println(a[i - 1]);
} else {
power--;
}
}
System.out.println();
}
}
OUTPUT:
2. Implementation of Stop and Wait Protocol and sliding window

PROGRAM:
import java.io.*;
import java.net.*;

public class Sender {


Socket sender;
ObjectOutputStream out;
ObjectInputStream in;
String packet, ack, str, msg;
int n, i = 0, sequence = 0;

Sender() {
}

public void run() {


try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Waiting for connection...");
sender = new Socket("localhost", 2005);
sequence = 0;
out = new ObjectOutputStream(sender.getOutputStream());
out.flush();
in = new ObjectInputStream(sender.getInputStream());
str = (String) in.readObject();
System.out.println("receiver>" + str);
System.out.println("enter the data to send...");
packet = br.readLine();
n = packet.length();
do {
try {
if (i < n) {
msg = String.valueOf(sequence);
msg = msg.concat(packet.substring(i, i + 1));
} else if (i == n) {
msg = "end";
out.writeObject(msg);
break;
}
out.writeObject(msg);
sequence = (sequence == 0) ? 1 : 0;
out.flush();
System.out.println("data sent>" + msg);
ack = (String) in.readObject();
System.out.println("waiting for ack...\n\n");
if (ack.equals(String.valueOf(sequence))) {
i++;
System.out.println("receiver>" + "packet received\n\n");
} else {
System.out.println("time out resending data...\n\n");
sequence = (sequence == 0) ? 1 : 0;
}
} catch (Exception e) {
}
} while (i < n + 1);
System.out.println("all data sent exiting");
} catch (Exception e) {
} finally {
try {
in.close();
out.close();
sender.close();
} catch (Exception e) {
}
}
}

public static void main(String args[]) {


Sender s = new Sender();
s.run();
}
}
//STOP AND WAIT RECEIVER
import java.io.*;

import java.net.*;

public class Receiver {

ServerSocket receiver;

Socket connection = null;

ObjectOutputStream out;

ObjectInputStream in;

String packet, ack, data = " ";

int i = 0, sequence = 0;

Receiver() {

public void run() {


try {

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

receiver = new ServerSocket(2005, 10);

System.out.println("Waiting for connection....");

connection = receiver.accept();

sequence = 0;

System.out.println("Connection established: ");

out = new ObjectOutputStream(connection.getOutputStream());

out.writeObject("connected.");

do {

try {

in = new ObjectInputStream(connection.getInputStream());

packet = (String) in.readObject();

if (Integer.valueOf(packet.substring(0, 1)) == sequence) {

data += packet.substring(1);

sequence = (sequence == 0) ? 1 : 0;

System.out.println("\n\n receiver>" + packet);

} else {

System.out.println("\n\n receiver>" + packet + " duplicate data");

if (i < 3) {

out.writeObject(String.valueOf(sequence));

i++;

} else {

out.writeObject(String.valueOf((sequence + 1) % 2));

i = 0;

} catch (IOException | ClassNotFoundException e) {

// Handle exceptions here

} finally {

try {
in.close();

out.close();

connection.close();

} catch (IOException e) {

// Handle exceptions here

} while (true);

} catch (IOException e) {

// Handle exceptions here

public static void main(String args[]) {

Receiver s = new Receiver();

while (true) {

s.run();

//SLIDING WINDOW SENDER

import java.io.*;

import java.net.*;

public class SlideSender {

Socket sender;

ObjectOutputStream out;

ObjectInputStream in;

String pkt;

char data = 'a';

int SeqNum = 0, SWS = 5;

int LAR = -1, LFS = -1;


int NF;

SlideSender() {

public void SendFrames() {

if ((SeqNum <= 15) && (SWS >= (LFS - LAR))) {

try {

NF = SWS - (LFS - LAR);

for (int i = 0; i < NF; i++) {

pkt = String.valueOf(SeqNum);

pkt = pkt.concat(" ");

pkt = pkt.concat(String.valueOf(data));

LFS = SeqNum;

System.out.println("sent " + SeqNum + " " + data);

data++;

if (data == 'g')

data = 'a';

SeqNum++;

out.writeObject(pkt);

out.flush();

} catch (Exception e) {

public void run() throws IOException {

sender = new Socket("localhost", 1500);

out = new ObjectOutputStream(sender.getOutputStream());

in = new ObjectInputStream(sender.getInputStream());
while (LAR <= 15) {

try {

SendFrames();

String Ack = (String) in.readObject();

LAR = Integer.parseInt(Ack);

System.out.println("ack received: " + LAR);

} catch (Exception e) {

in.close();

out.close();

sender.close();

System.out.println("\n connection terminated : ");

public static void main(String args[]) throws IOException {

SlideSender s = new SlideSender();

s.run();

//sliding window Receiver

import java.io.*;

import java.net.*;

import java.util.*;

public class SlideReceiver {

ServerSocket receiver;

Socket conc = null;

ObjectOutputStream out;

ObjectInputStream in;
String ack, pkt, data = " ";

int SeqNum = 1, RWS = 5;

int LFR = 0;

int LAF = LFR + RWS;

Random rand = new Random();

SlideReceiver() {

public void run() throws IOException, InterruptedException {

receiver = new ServerSocket(1500, 10);

conc = receiver.accept();

if (conc != null)

System.out.println("Connection established: ");

out = new ObjectOutputStream(conc.getOutputStream());

in = new ObjectInputStream(conc.getInputStream());

while (LFR < 15) {

try {

pkt = (String) in.readObject();

String[] str = pkt.split("\\s");

ack = str[0];

data = str[1];

LFR = Integer.parseInt(ack);

System.out.println("\nmsg received: " + data);

if ((SeqNum <= LFR) && (SeqNum > LAF)) {

out.writeObject(ack);

out.flush();

System.out.println("resending ack" + LFR);

}
} catch (Exception e) {

in.close();

out.close();

receiver.close();

System.out.println("\nConnection terminated.");

public static void main(String args[]) throws IOException, InterruptedException {

SlideReceiver R = new SlideReceiver();

R.run();

OUTPUT:

3. Implementation and study of Goback-N and selective repeat protocols

PROGRAM:
import java.io.*;

public class GoBackN {


public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter the window size: ");
int window = Integer.parseInt(br.readLine());
int sent = 0;
int nextSequenceExpected = 0;

boolean loop = true;


while (loop) {
for (int i = 0; i < window; i++) {
if (sent == window) {
System.out.println("All frames have been transmitted.");
break;
}
System.out.println("Frame " + sent + " has been transmitted.");
sent++;
}

System.out.println("Please enter the last Acknowledgement received.");


int ack = Integer.parseInt(br.readLine());
if (ack == nextSequenceExpected) {
System.out.println("Acknowledgment " + ack + " received. Moving window...");
nextSequenceExpected += window;
} else {
System.out.println("Acknowledgment " + ack + " received. Resending frames from " +
nextSequenceExpected);
sent = nextSequenceExpected;
}

if (ack >= window) {


System.out.println("All frames have been acknowledged. Exiting.");
loop = false;
}
}
}
}

//selective repeat client


import java.lang.System;
import java.net.*;
import java.io.*;
import java.text.*;
import java.util.Random;
import java.util.*;

public class Client {


static Socket connection;

public static void main(String a[]) throws IOException {


try {
int v[] = new int[10];
int n = 0;
Random rands = new Random();
int rand = 0;
InetAddress addr = InetAddress.getByName("localhost");
System.out.println(addr);
connection = new Socket(addr, 8011);
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
DataInputStream in = new DataInputStream(connection.getInputStream());
int p = in.readInt(); // Read the number of frames
System.out.println("No of frames is: " + p);

for (int i = 0; i < p; i++) {


v[i] = in.readInt();
System.out.println(v[i]);
}

int g[] = new int[p]; // Array to store received frames

rand = rands.nextInt(p);
v[rand] = -1;
for (int i = 0; i < p; i++) {
if (v[i] == -1) {
System.out.println("request to retransmit from packet no" + (i + 1) + " again!!");
n = i;
out.writeInt(n);
out.flush();
}
}
System.out.println();
v[n] = in.readInt();
System.out.println("received frame is: " + v[n]);
System.out.println("quitting");
} catch (Exception e) {
System.out.println(e);
}
}
}
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;

public class Server {


static ServerSocket serversocket;
static DataInputStream dis;
static DataOutputStream dos;

public static void main(String[] args) throws SocketException {


try {
int a[] = { 30, 40, 50, 60, 70, 0, 90, 100 };
serversocket = new ServerSocket(8011);
System.out.println("waiting for connection");
Socket client = serversocket.accept();
dis = new DataInputStream(client.getInputStream());
dos = new DataOutputStream(client.getOutputStream());
System.out.println("the number of packets sents is: " + a.length);
int y = a.length;
dos.writeInt(y);
dos.flush();
for (int i = 0; i < a.length; i++) {
dos.writeInt(a[i]);
dos.flush();
}
int k = dis.readInt();
dos.writeInt(a[k]);
dos.flush();
} catch (IOException e) {
System.out.println(e);
} finally {
try {
dis.close();
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
OUTPUT:

4. Implementation of High Level Data Link Control.

PROGRAM:
import java.util.*;
public class Main {
static int[] crc_func(int[] crc) {
int l_d = 4, l_dd = 0;
char divisor[] = {'1', '1', '0', '0'};
char divident[] = new char[8];

for (int i = 0; i < 8; i++) {


divident[i] = (char) ('0' + crc[i + 8]);
}

int l = l_dd + l_d - 1;


char n_str[] = new char[1 + l];

for (int i = 0; i < l_dd; i++) {


n_str[i] = divident[i];
}

for (int i = l_dd; i < l; i++) {


n_str[i] = '0';
}

char div_part[] = new char[l_d];

for (int i = 0; i < l_d; i++) {


div_part[i] = n_str[i];
}

int in = 0;

for (int i = 0; i < l - l_d; i++) {


if (div_part[0] == '1') {
for (int com = 0; com < l_d; com++) {
div_part[com] = (char) (((div_part[com] - '0') ^ (divisor[com] - '0')) + '0');
}
}

for (int sp = 0; sp < l_d - 1; sp++) {


div_part[sp] = div_part[sp + 1];
}

div_part[l_d - 1] = n_str[i + l_d];


in++;
}

int fs[] = new int[16];

for (int i = 5; i < 13; i++) {


fs[i] = crc[i + 3];
}

in = 0;

for (int i = 13; i < 16; i++) {


fs[i] = (div_part[in] - '0');
in++;
}

return fs;
}

public static void main(String[] args) {


Scanner s = new Scanner(System.in);
int[] fd = new int[]{0, 1, 1, 1, 1, 1, 1, 0};
int[] crc = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0};
int payload;

int[] address = new int[]{0, 0, 1, 1, 0, 0, 1, 1};


int[] control = new int[]{0, 1, 1, 0, 1, 1, 0, 0};

System.out.println("Enter the length of payload");


payload = s.nextInt();
int payarr[] = new int[payload];
int hdleframe[] = new int[48 + payload];

for (int i = 0; i < 8; i++) {


hdleframe[i] = fd[i];
}

for (int i = 0; i < 8; i++) {


hdleframe[i + 8] = address[i];
}

for (int i = 0; i < 8; i++) {


hdleframe[i + 16] = control[i];
}

System.out.println("Enter the payload data");


for (int i = 0; i < payarr.length; i++) {
payarr[i] = s.nextInt();
}

crc = crc_func(crc);

for (int i = 0; i < payload; i++) {


hdleframe[i + 24] = payarr[i];
}
for (int i = 0; i < 16; i++) {
hdleframe[i + 24 + payload] = crc[i];
}

for (int i = 0; i < 8; i++) {


hdleframe[i + 40 + payload] = fd[i];
}

System.out.println("HDLC:");
for (int i = 0; i < 48 + payload; i++) {
System.out.print(hdleframe[i]);
}
}
}

OUTPUT:

5. Study of Socket Programming and Client – Server model using java.

PROGRAM:
import java.net.*;
import java.io.*;
public class Server {
private ServerSocket server = null;
private Socket socket = null;
private DataInputStream in = null;
public Server(int port) {
try {
server = new ServerSocket(port);
System.out.println("Server Started");
System.out.println("Waiting for a client...");
socket = server.accept();
System.out.println("Client accepted");
in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));

String line = "";


while (!line.equals("over")) {
try {
line = in.readUTF();
System.out.println(line);
} catch (IOException i) {
System.out.println(i);
}
}
System.out.println("Closing connection");

// Close the resources in reverse order of opening


in.close();
socket.close();
server.close();
} catch (IOException i) {
System.out.println(i);
}
}

public static void main(String args[]) {


Server server = new Server(5000);
}
}
//run server prog first, then client program
//client program
import java.net.*;
import java.io.*;

public class Client {


private Socket socket = null;
private BufferedReader input = null;
private DataOutputStream out = null;
public Client(String address, int port) {
try {
socket = new Socket(address, port);
System.out.println("Connected");
input = new BufferedReader(new InputStreamReader(System.in));
out = new DataOutputStream(socket.getOutputStream());
} catch (UnknownHostException u) {
System.out.println(u);
} catch (IOException i) {
System.out.println(i);
}
String line = "";
while (!line.equals("over")) {
try {
line = input.readLine();
out.writeUTF(line);
} catch (IOException i) {
System.out.println(i);
}
}
try {
input.close();
out.close();
socket.close();
} catch (IOException i) {
System.out.println(i);
}
}
public static void main(String args[]) {
Client client = new Client("127.0.0.1", 5000);
}
}

OUTPUT:
6. Write a socket Program for Echo/Ping/Talk commands using java.
PROGRAM:

NOTE: On older versions of java, use this code:


//Ping Command
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class pingEmd
{
public static void runSystemCommand(String command)
{
try
{
Process p=Runtime.getRuntime().exec(command);
BufferedReader inputStream= new BufferedReader(new InputStreamReader(p.getInputStream()));
String s=" ";
while((s=inputStream.readLine())!=null)
System.out.println(s);
}
catch(Exception e)
{}
}
public static void main(String[] args){
//String ip="www.google.co.in";
//String ip="127.0.0.1";
String ip="www.drranurekha.com";
runSystemCommand("ping"+ip);
}
}
IF YOU SEE THIS ON RUNNING THE PROGRAM:

Use this code:

import java.io.BufferedReader;
import java.io.InputStreamReader;
public class pingEmd {
public static void runSystemCommand(String command) {
try {
ProcessBuilder processBuilder = new ProcessBuilder(command.split(" "));
Process p = processBuilder.start();

BufferedReader inputStream = new BufferedReader(new InputStreamReader(p.getInputStream()));


String s;
while ((s = inputStream.readLine()) != null)
System.out.println(s);
} catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String[] args) {


String ip = "www.drranurekha.com";
runSystemCommand("ping " + ip);
// Note the space after "ping"
}
}

OUTPUT:
7. Implementation of distance vector routing algorithm.

PROGRAM:

import java.io.*;
public class DVR
{
static int graph[][];
static int via[][];
static int rt[][];
static int v;
static int e;

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


{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.println("Please enter the number of Vertices: ");


v = Integer.parseInt(br.readLine());

System.out.println("Please enter the number of Edges: ");


e = Integer.parseInt(br.readLine());

graph = new int[v][v];


via = new int[v][v];
rt = new int[v][v];
for(int i = 0; i < v; i++)
for(int j = 0; j < v; j++)
{
if(i == j)
graph[i][j] = 0;
else
graph[i][j] = 9999;
}

for(int i = 0; i < e; i++)


{
System.out.println("Please enter data for Edge " + (i + 1) + ":");
System.out.print("Source: ");
int s = Integer.parseInt(br.readLine());
s--;
System.out.print("Destination: ");
int d = Integer.parseInt(br.readLine());
d--;
System.out.print("Cost: ");
int c = Integer.parseInt(br.readLine());
graph[s][d] = c;
graph[d][s] = c;
}
dvr_calc_disp("The initial Routing Tables are: ");

System.out.print("Please enter the Source Node for the edge whose cost has changed: ");
int s = Integer.parseInt(br.readLine());
s--;
System.out.print("Please enter the Destination Node for the edge whose cost has changed: ");
int d = Integer.parseInt(br.readLine());
d--;
System.out.print("Please enter the new cost: ");
int c = Integer.parseInt(br.readLine());
graph[s][d] = c;
graph[d][s] = c;

dvr_calc_disp("The new Routing Tables are: ");


}

static void dvr_calc_disp(String message)


{
System.out.println();
init_tables();
update_tables();
System.out.println(message);
print_tables();
System.out.println();
}

static void update_table(int source)


{
for(int i = 0; i < v; i++)
{
if(graph[source][i] != 9999)
{
int dist = graph[source][i];
for(int j = 0; j < v; j++)
{
int inter_dist = rt[i][j];
if(via[i][j] == source)
inter_dist = 9999;
if(dist + inter_dist < rt[source][j])
{
rt[source][j] = dist + inter_dist;
via[source][j] = i;
}
}
}
}
}
static void update_tables()
{
int k = 0;
for(int i = 0; i < 4*v; i++)
{
update_table(k);
k++;
if(k == v)
k = 0;
}
}

static void init_tables()


{
for(int i = 0; i < v; i++)
{
for(int j = 0; j < v; j++)
{
if(i == j)
{
rt[i][j] = 0;
via[i][j] = i;
}
else
{
rt[i][j] = 9999;
via[i][j] = 100;
}
}
}
}

static void print_tables()


{
for(int i = 0; i < v; i++)
{
for(int j = 0; j < v; j++)
{
System.out.print("Dist: " + rt[i][j] + " ");
}
System.out.println();
}
}
}
OUTPUT:
8. Implementation of Link state routing algorithm.

PROGRAM:

import java.util.*;

public class LSR {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of nodes:");
int nodes = sc.nextInt();
int[] preD = new int[nodes];
int min = 999;
int nextNode = 0;
int[] distance = new int[nodes];
int[][] matrix = new int[nodes][nodes];
int[] visited = new int[nodes];
System.out.println("Enter the cost matrix");
for (int i = 0; i < nodes; i++) {
visited[i] = 0;
preD[i] = 0;
for (int j = 0; j < nodes; j++) {
matrix[i][j] = sc.nextInt();
if (matrix[i][j] == 0) {
matrix[i][j] = 999;
}
}
}
distance = matrix[0];
visited[0] = 1;
distance[0] = 0;
for (int counter = 0; counter < nodes; counter++) {
min = 999;
for (int i = 0; i < nodes; i++) {
if (min > distance[i] && visited[i] != 1) {
min = distance[i];
nextNode = i;
}
}
for (int i = 0; i < nodes; i++) {
if (visited[i] != 1) {
if (min + matrix[nextNode][i] < distance[i]) {
distance[i] = min + matrix[nextNode][i];
preD[i] = nextNode;
}
}
}
visited[nextNode] = 1;
}
int j;
for (int i = 0; i < nodes; i++) {
if (i != 0) {
System.out.print("Path=" + i);
j = i;
do {
j = preD[j];
System.out.print("<-" + j);
} while (j != 0);
System.out.println();
System.out.println("Cost=" + distance[i]);
}
}
}
}

OUTPUT:

You might also like