0% found this document useful (0 votes)
31 views13 pages

NL-Assignment No 2 - AY2023-2024

The document discusses building TCP socket programs for an arithmetic calculator and selecting the appropriate system call that results in sending SYN packets. It also provides a Tcl script that forms a network topology and sends UDP packets between nodes, and applies the fundamentals of connection-oriented and connectionless traffic using NS2.

Uploaded by

imranahmedkhan42
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)
31 views13 pages

NL-Assignment No 2 - AY2023-2024

The document discusses building TCP socket programs for an arithmetic calculator and selecting the appropriate system call that results in sending SYN packets. It also provides a Tcl script that forms a network topology and sends UDP packets between nodes, and applies the fundamentals of connection-oriented and connectionless traffic using NS2.

Uploaded by

imranahmedkhan42
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/ 13

PARSHVANATH CHARITABLE TRUST'S

A. P. SHAH INSTITUTE OF TECHNOLOGY


(All Branches NBA Accredited)

Department of Information Technology

Academic Year: 2023-24 Semester: IV


Class / Branch: SE IT
Subject: NL

Assignment No 02
Lab Outcomes (LO):

LO1 Analyze network administration commands and examine their use in different network

scenarios using Netkit Emulator

LO2 Make use of network simulation concept to implement a network topology on the basis

of Installation and configuration of Network simulator.

LO3 Build different network scenarios using NS2 to measure their performance behaviour.

LO4 Develop client server architecture with the help of socket programming.

LO5 Analyze the traffic flow of various connectionless and connection-oriented protocols.

LO6 Design a network for the given scenario using cisco packet tracer.

Q.1 Build a TCP socket program for arithmetic calculator. (BL3-LO4)

Answer:

Client Program:

#include<sys/types.h>
#include<sys/socket.h>
#include<stdio.h>
#include<netinet/in.h>
#include <unistd.h>
#include<string.h>
#include<strings.h>
#include <arpa/inet.h>

//#define buffsize 150

Department of Information Technology | AP SIT


PARSHVANATH CHARITABLE TRUST'S

A. P. SHAH INSTITUTE OF TECHNOLOGY


(All Branches NBA Accredited)

void main()
{
int b,sockfd,sin_size,con,n,len;
//char buff[256];
char operator;
int op1,op2,result;
if((sockfd=socket(AF_INET,SOCK_STREAM,0))>0)
printf("socket created sucessfully\n");
//printf("%d\n", sockfd);
struct sockaddr_in servaddr;

servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=inet_addr("127.0.0.1");
servaddr.sin_port=6006;

sin_size = sizeof(struct sockaddr_in);


if((con=connect(sockfd,(struct sockaddr *) &servaddr, sin_size))==0);
//initiate a connection on a socket
printf("connect sucessful\n");
printf("Enter operation:\n +:Addition \n -: Subtraction \n /: Division
\n*:Multiplication \n");
scanf("%c",&operator);
printf("Enter operands:\n");
scanf("%d %d", &op1, &op2);

write(sockfd,&operator,10);
write(sockfd,&op1,sizeof(op1));
write(sockfd,&op2,sizeof(op2));
read(sockfd,&result,sizeof(result));
printf("Operation result from server=%d\n",result);
close(sockfd);
}

Server Program:

#include<sys/types.h>
#include<sys/socket.h>
#include<stdio.h>
#include<netinet/in.h>
#include <unistd.h>
#include<string.h>
#include <arpa/inet.h>

void main()
{
int b,sockfd,connfd,sin_size,l,n,len;
char operator;
int op1,op2,result;
if((sockfd=socket(AF_INET,SOCK_STREAM,0))>0)
printf("socket created sucessfully\n"); //socket creation

Department of Information Technology | AP SIT


PARSHVANATH CHARITABLE TRUST'S

A. P. SHAH INSTITUTE OF TECHNOLOGY


(All Branches NBA Accredited)

//printf("%d\n", sockfd); //on success 0 otherwise -1

struct sockaddr_in servaddr;


struct sockaddr_in clientaddr;

servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=inet_addr("127.0.0.1");
servaddr.sin_port=6006;

if((bind(sockfd, (struct sockaddr *)&servaddr,sizeof(servaddr)))==0)


printf("bind sucessful\n"); //bind() assigns the

//printf("%d\n",b);

if((listen(sockfd,5))==0) //listen for connections on a socket


printf("listen sucessful\n");
//printf("%d\n",l);

sin_size = sizeof(struct sockaddr_in);


if((connfd=accept(sockfd,(struct sockaddr *)&clientaddr,&sin_size))>0);
printf("accept sucessful\n");
//printf("%d\n",connfd);
read(connfd, &operator,10);
read(connfd,&op1,sizeof(op1));
read(connfd,&op2,sizeof(op2));
switch(operator) {
case '+': result=op1 + op2;
printf("Result is: %d + %d = %d\n",op1, op2, result);
break;
case '-':result=op1 - op2;
printf("Result is: %d - %d = %d\n",op1, op2, result);
break;
case '*':result=op1 * op2;
printf("Result is: %d * %d = %d\n",op1, op2, result);
break;
case '/':result=op1 / op2;
printf("Result is: %d / %d = %d\n",op1, op2, result);
break;
default:
printf("ERROR: Unsupported Operation");
}

write(connfd,&result,sizeof(result));
close(sockfd);
}

Department of Information Technology | AP SIT


PARSHVANATH CHARITABLE TRUST'S

A. P. SHAH INSTITUTE OF TECHNOLOGY


(All Branches NBA Accredited)

Q.2 Select appropriate system call which results in the sending of SYN packets? (BL3-LO4)

(A) socket
(B) bind
(C) listen
(D) connect
Answer (D)
socket () creates a new socket of a certain socket type, identified by an integer number, and allocates
system resources to it.
bind () is typically used on the server side, and associates a socket with a socket address structure, i.e. a
specified local port number and IP address.
listen () is used on the server side, and causes a bound TCP socket to enter listening state.
connect () is used on the client side, and assigns a free local port number to a socket. In case of a TCP
socket, it causes an attempt to establish a new TCP connection.
When connect () is called by client, following three-way handshake happens to establish the connection
in TCP.
1) The client requests a connection by sending a SYN (synchronize) message to the server.
2) The server acknowledges this request by sending SYN-ACK back to the client.
3) The client responds with an ACK, and the connection is established.

Q.3Apply the concept of routing to write a Tcl script that forms a network consisting of 7
nodes, numbered from 1 to 7, forming a ring topology. The links have a 512Kbps
bandwidth with 5ms delay. Set the routing protocol to DV (Distance vector). Send UDP
packets from node 1 to node 4 with the rate of 100 packets/sec (using default packet size).
Start transmission at 0.01. Bring down the link between node 2 and node 3 at 0.4. Finish
the transmission at 1.000. [BL3, LO3]

Answer: Routing is the process of selecting a path for traffic in a network or between or across
multiple networks. Broadly, routing is performed in many types of networks, including circuit-

Department of Information Technology | AP SIT


PARSHVANATH CHARITABLE TRUST'S

A. P. SHAH INSTITUTE OF TECHNOLOGY


(All Branches NBA Accredited)

switched networks, such as the public switched telephone network (PSTN), and computer
networks, such as the Internet.
#Simulation program to genrate coneection oriented and connectionless trafic over given
network which uses static and dynamic routing.

#Create a simulator object


set ns [new Simulator]
#Open the Trace file
set nr [open apsit.tr w]

$ns trace-all $nr


#Open the NAM trace file
set nf [open apsit.nam w]
$ns namtrace-all $nf
#Define a 'finish' procedure
proc finish { } {
global ns nr nf
$ns flush-trace
close $nf

close $nr
exec nam apsit.nam &
exit 0
}
#Create nodes by using looping constructs.
for { set i 1 } { $i < 8} { incr i 1 } {
set n($i) [$ns node]}
#Create links between the nodes

Department of Information Technology | AP SIT


PARSHVANATH CHARITABLE TRUST'S

A. P. SHAH INSTITUTE OF TECHNOLOGY


(All Branches NBA Accredited)

for {set i 1} {$i < 8} {incr i} {


$ns duplex-link $n($i) $n([expr $i+1]) 512Kb 5ms DropTail }

#Setup a udp connection


set udp0 [new Agent/UDP]
$ns attach-agent $n(1) $udp0
#Setup a CBR over UDP connection
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 100

$cbr0 set interval_ 0.005


$cbr0 attach-agent $udp0
set null0 [new Agent/Null]
$ns attach-agent $n(4) $null0
$ns connect $udp0 $null0

#Dynamic Routing by using Distance Vector Routing Protocol


$ns rtproto DV
#Schedule events for the CBR agents

$ns rtmodel-at 0.4 down $n(2) $n(3)

$ns at 0.01 "$cbr0 start"


#Call the finish procedure after 1second of simulation time
$ns at 1.0 "finish"
#Run the simulation
$ns run

Department of Information Technology | AP SIT


PARSHVANATH CHARITABLE TRUST'S

A. P. SHAH INSTITUTE OF TECHNOLOGY


(All Branches NBA Accredited)

Q.4 Apply the fundamentals of (TCP)Connection Oriented and Connectionless (UDP) traffic to
develop given network topology by using NS2. [BL3, LO3]

The network consists of five nodes n0 to n4. In this scenario, node n0 sends constant bit-rate (CBR) traffic
to node n3(connectionless), and node n1 transfers data to node n4 using a file transfer protocol
(FTP)(connection oriented). These two carried traffic sources are carried by transport layer protocols User
Datagram Protocol (UDP) and Transmission Control Protocol (TCP), respectively. In NS2, the transmitting
object of these two protocols are a UDP agent and a TCP agent, while the receivers are a Null agent and a
TCP sink agent, respectively.

Sender Receiver

TCP Sink (FTP)

UDP null (CBR)

Answer:

Tcl script:

Department of Information Technology | AP SIT


PARSHVANATH CHARITABLE TRUST'S

A. P. SHAH INSTITUTE OF TECHNOLOGY


(All Branches NBA Accredited)

#Create a simulator object

set ns [new Simulator]

#Define different colors for data flows (for NAM)

$ns color 1 Blue

$ns color 2 Red

#Open the Trace file

set nr [open apsit.tr w]

$ns trace-all $nr

#Open the NAM trace file

set nf [open out.nam w]

$ns namtrace-all $nf

#Define a 'finish' procedure

proc finish {} {

global ns nr nf

$ns flush-trace

#Close the NAM trace file

close $nr

close $nf

#Execute NAM on the trace file

exec nam out.nam &

exit 0

Department of Information Technology | AP SIT


PARSHVANATH CHARITABLE TRUST'S

A. P. SHAH INSTITUTE OF TECHNOLOGY


(All Branches NBA Accredited)

#Create four nodes

set n0 [$ns node]

set n1 [$ns node]

set n2 [$ns node]

set n3 [$ns node]

set n4 [$ns node]

#Create links between the nodes

$ns duplex-link $n0 $n2 1Mb 10ms DropTail

$ns duplex-link $n1 $n2 1Mb 10ms DropTail

$ns duplex-link $n2 $n3 1Mb 10ms DropTail

$ns duplex-link $n2 $n4 1Mb 10ms DropTail

#Give node position (for NAM)

$ns duplex-link-op $n0 $n2 orient right-up

$ns duplex-link-op $n1 $n2 orient right-down

$ns duplex-link-op $n2 $n3 orient right-down

$ns duplex-link-op $n2 $n4 orient right-up

#Setup a TCP connection

set tcp [new Agent/TCP]

$tcp set class_ 2

$ns attach-agent $n0 $tcp

set sink [new Agent/TCPSink]

$ns attach-agent $n3 $sink

Department of Information Technology | AP SIT


PARSHVANATH CHARITABLE TRUST'S

A. P. SHAH INSTITUTE OF TECHNOLOGY


(All Branches NBA Accredited)

$ns connect $tcp $sink

$tcp set fid_ 1

#Setup a FTP over TCP connection

set ftp [new Application/FTP]

$ftp attach-agent $tcp

$ftp set type_ FTP

#Setup a UDP connection

set udp [new Agent/UDP]

$ns attach-agent $n1 $udp

set null [new Agent/Null]

$ns attach-agent $n4 $null

$ns connect $udp $null

$udp set fid_ 2

#Setup a CBR over UDP connection

set cbr [new Application/Traffic/CBR]

$cbr attach-agent $udp

$cbr set type_ CBR

$cbr set packet_size_ 1000

$cbr set rate_ 1mb

$cbr set random_ false

Department of Information Technology | AP SIT


PARSHVANATH CHARITABLE TRUST'S

A. P. SHAH INSTITUTE OF TECHNOLOGY


(All Branches NBA Accredited)

#Schedule events for the CBR and FTP agents

$ns at 0.1 "$cbr start"

$ns at 0.1 "$ftp start"

$ns at 4.5 "$ftp stop"

$ns at 4.5 "$cbr stop"

#Call the finish procedure after 5 seconds of simulation time

$ns at 5.0 "finish"

#Print CBR packet size and interval

puts "CBR packet size = [$cbr set packet_size_]"

puts "CBR interval = [$cbr set interval_]"

#Run the simulation

$ns run

Department of Information Technology | AP SIT


PARSHVANATH CHARITABLE TRUST'S

A. P. SHAH INSTITUTE OF TECHNOLOGY


(All Branches NBA Accredited)

Q.5 (A) Identify phase/phases of Connection Oriented Protocol from following snippet NS2
Simulation Trace file? Justify your answer. [BL3, LO2]

Answer: Connection Oriented Protocol service involves three phases, Connection setup phase, Data
transfer phase and Connection release phase.

In given snippet of NS2 simulation trace file two phases are involved i.e. Connection setup phase and Data
transfer phase. Since, third record represents packet has been sent from node0 to node1 having packet size
40, sequence no and packet_id 0 as a SYN packet to establish connection. Sixth record represents
acknowledgment for SYN packet sent from node1 to node 0 having sequence no 0 and packet_id 1. Here
connection established successfully in between node0 and node1.

Hereafter data packets has been sent from node0 to node1 having packet size 1040 that means after
connection setup data gets transferred in between node0 and node1.

Department of Information Technology | AP SIT


PARSHVANATH CHARITABLE TRUST'S

A. P. SHAH INSTITUTE OF TECHNOLOGY


(All Branches NBA Accredited)

Q.5 (B) Make use of following snippet of NS2 Simulation Trace file to identify no. of segments
transferred from Source node 0 to Destination node 1 of Simulation. [BL3, LO2]

(A) 4

(B) 3

(C) 2

(D) 1

Answer: (B)

Explanation: A TCP segment consists of data bytes to be sent and a header that is added to the data by TCP.

Above trace file has three records having their packet size 1040bytes that means packet consist of data as
well as header. That means total three segments have transferred from Source node 0 to Destination node
1. Other packet transmitted in between Source node0 to Destination node1 is to establish connection.

Department of Information Technology | AP SIT

You might also like