Practical 6

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

ATHARVA KALE PRACTICAL NO.

6 ROLL NO: 27

AIM: Program to simulate UDP Server Client.

 THEORY:
1. In computer networking, the User Datagram Protocol (UDP) is one of the core
members of the Internet protocol suite.
2. With UDP, computer applications can send messages, in this case referred to as
datagrams, to other hosts on an Internet Protocol (IP) network. Prior
communications are not required in order to set up communication channels or
data paths.
3. UDP uses a simple connectionless communication model with a minimum of
protocol mechanisms.
4. UDP provides checksums for data integrity, and port numbers for addressing
different functions at the source and destination of the datagram.
5. UDP is suitable for purposes where error checking and correction are either not
necessary or are performed in the application.
6. Working:
a. UDP sends packets (units of data transmission) directly to a target
computer, without establishing a connection first, indicating the order of
said packets, or checking whether they arrived as intended.
7. Classes Used In Code:
a. Following different classes are used in code:
i. Ipv4AddressHelper Class:
1. A helper class to make life easier while doing simple IPv4
address assignment in scripts.
2. This class is a very simple IPv4 address generator. You can
think of it as a simple local number incrementer. It has no
notion that IP addresses are part of a global address space.
3. If you have a complicated address assignment situation
you may want to look at the Ipv4AddressGenerator which
ATHARVA KALE PRACTICAL NO.6 ROLL NO: 27

does recognize that IP address and network number


generation is part of a global problem. Ipv4AddressHelper
is a simple class to make simple problems easy to handle.
ii. UdpClientHelper Class:
1. It create a client application which sends UDP packets
carrying a 32bit sequence number and a 64 bit time stamp.
2. It sends UDP packet carrying sequence number and time
stamp in their payloads.
iii. NetDevice Class:
1. Net device abstraction covers both the software driver and
the simulated hardware. The net device abstraction is
represented in C++ by the class NetDevice.
2. The NetDevice class provides methods for managing
connections to Node and Channel objects; and may be
specialized by developers in the object-oriented
programming sense.
iv. Application Class:
1. In ns-3 the basic abstraction for a user program that
generates some activity to be simulated is the application.
This abstraction is represented in C++ by the class
Application.
2. The Application class provides methods for managing the
representations of version of user-level applications in
simulations. Developers are expected to specialize the
Application class in the object-oriented programming
sense to create new applications.
ATHARVA KALE PRACTICAL NO.6 ROLL NO: 27

A) Write a program to simulate UDP server client.

 CODE:
// Network topology
//
// n0 n1
// | |
// =======
// LAN
//
// - UDP flows from n0 to n1

#include <fstream>
#include "ns3/core-module.h"
#include "ns3/csma-module.h"
#include "ns3/applications-module.h"
#include "ns3/internet-module.h"
#include "ns3/netanim-module.h"
using namespace ns3;

NS_LOG_COMPONENT_DEFINE ("UdpClientServerExample");

int
main (int argc, char *argv[])
{
//
// Enable logging for UdpClient and
//
LogComponentEnable ("UdpClient", LOG_LEVEL_INFO);
ATHARVA KALE PRACTICAL NO.6 ROLL NO: 27

LogComponentEnable ("UdpServer", LOG_LEVEL_INFO);

bool useV6 = false;


Address serverAddress;

CommandLine cmd (__FILE__);


cmd.AddValue ("useIpv6", "Use Ipv6", useV6);
cmd.Parse (argc, argv);

//
// Explicitly create the nodes required by the topology (shown above).
//
NS_LOG_INFO ("Create nodes.");
NodeContainer n;
n.Create (2);

InternetStackHelper internet;
internet.Install (n);

NS_LOG_INFO ("Create channels.");


//
// Explicitly create the channels required by the topology (shown above).
//
CsmaHelper csma;
csma.SetChannelAttribute ("DataRate", DataRateValue (DataRate (5000000)));
csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
csma.SetDeviceAttribute ("Mtu", UintegerValue (1400));
NetDeviceContainer d = csma.Install (n);
//
ATHARVA KALE PRACTICAL NO.6 ROLL NO: 27

// We've got the "hardware" in place. Now we need to add IP addresses.


//
NS_LOG_INFO ("Assign IP Addresses.");
if (useV6 == false)
{
Ipv4AddressHelper ipv4;
ipv4.SetBase ("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer i = ipv4.Assign (d);
serverAddress = Address (i.GetAddress (1));
}
else
{
Ipv6AddressHelper ipv6;
ipv6.SetBase ("2001:0000:f00d:cafe::", Ipv6Prefix (64));
Ipv6InterfaceContainer i6 = ipv6.Assign (d);
serverAddress = Address(i6.GetAddress (1,1));
}
NS_LOG_INFO ("Create Applications.");
//
// Create one udpServer applications on node one.
//
uint16_t port = 4000;
UdpServerHelper server (port);
ApplicationContainer apps = server.Install (n.Get (1));
apps.Start (Seconds (1.0));
apps.Stop (Seconds (10.0));

//
// Create one UdpClient application to send UDP datagrams from node zero to
ATHARVA KALE PRACTICAL NO.6 ROLL NO: 27

// node one.
//
uint32_t MaxPacketSize = 1024;
Time interPacketInterval = Seconds (0.05);
uint32_t maxPacketCount = 320;
UdpClientHelper client (serverAddress, port);
client.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount));
client.SetAttribute ("Interval", TimeValue (interPacketInterval));
client.SetAttribute ("PacketSize", UintegerValue (MaxPacketSize));
apps = client.Install (n.Get (0));
apps.Start (Seconds (2.0));
apps.Stop (Seconds (10.0));
AnimationInterface anim("udp.xml");
//
// Now, do the actual simulation.
//
NS_LOG_INFO ("Run Simulation.");
Simulator::Run ();
Simulator::Destroy ();
NS_LOG_INFO ("Done.");
}
ATHARVA KALE PRACTICAL NO.6 ROLL NO: 27

 OUTPUT:
o ./waf output:

o Python Visualizer:
ATHARVA KALE PRACTICAL NO.6 ROLL NO: 27

o NetAnim Output:

 CONCLUSION:
Hence we successfully simulated UDP Server Client.

You might also like