UDP Client Server Program

You might also like

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

Module 2

Client Server Network topology using NS-3

Shweta Waghmare
TIMSCDR
Topics

M2 : Client Server Network topology using NS-3


● Program to simulate UDP server client
● Program to simulate DHCP server and n clients
● Program to simulate FTP using TCP protocol
● Program to Create simple topology
○ first.cc
● Programs to different types of topologies
○ Bus(second.cc)
○ Mesh(mesh.cc)
○ Star(star.cc)
○ Hybrid(third.cc) OR Wired-wireless n/w topology
○ Program for complex topologies
● Program for client server networks
○ Three-way handshake
○ DORA Cycle(discovery, offer,request, acknowledgement)
Steps to install ns-3

Steps for other OS : http://www.nsnam.org/wiki


Step 1: Install necessary packages (See the Updated Installation document)
Step 2: Download ns-allinone-3.32 zip file (http://www.nsnam.org)
Step 3: Extract ns-allinone-3.32 from zip file
Step 4: Go to ns-allinone-3.32 via terminal and give following command:
./build.py --enable-examples —-enable-tests
Step 5 (Optional): To test the installation, go to ns-3.32 and give
./test.py -c core
Structure of a Program in ns-3

❑ Include necessary header files

❑ Use ns-3 namespace

❑ Enable Log for your program

❑ Begin the main function

❑ Enable Log for applications, if required

❑ Create nodes

❑ Create a channel and set it’s attributes

❑ Create NIC: this is done while assigning channels between nodes.


Structure of a Program in ns-3

❑ Install network stack on nodes (TCP/IP stack is called “Internet” stack)

❑ Select “network address” and “subnet mask”

❑ Create interfaces and assign IP address to each interface.

❑ Install applications in nodes

❑ Set application start and stop time

❑ Run the simulation

❑ Release resources (e.g., dynamically allocated memory)


The fundamental objects

Node: the motherboard of a computer with RAM, CPU, and, IO interfaces

Application: a packet generator and consumer which can run on a Node and
talk to a set of network stacks

NetDevice: a network card which can be plugged in an IO interface of a


Node

Channel: a physical connector between a set of NetDevice objects


A First ns-3 Script

ns-3 in a directory called repos under your home directory.


UDP

UDP: the Caché User Datagram Protocol (UDP) binding.


Provides two-way message transfer between a server and a
large number of clients.
UDP is not connection-based; each data packet transmission is
an independent event.
Provides fast and lightweight data transmission for local
packet broadcasts and remote multicasting.
Working with UDP DatagramSockets in Java

● Java provides DatagramSocket to communicate over UDP instead of TCP.

● It is also built on top of IP.

● DatagramSockets can be used to both send and receive packets over the
Internet.
Examples

● UDP is preferred over TCP is the live coverage of TV channels. In this aspect, we want to
transmit as many frames to live audience as possible not worrying about the loss of one or
two frames. TCP being a reliable protocol add its own overhead while transmission.
● Another example where UDP is preferred is online multiplayer gaming. In games like
counter-strike or call of duty, it is not necessary to relay all the information but the most
important ones.
● It should also be noted that most of the applications in real life uses careful blend of both
UDP and TCP; transmitting the critical data over TCP and rest of the data via UDP.
Creation of DatagramSocket:-

First, a datagramSocket object is created to carry the packet to the destination and to receive it
whenever the server sends any data. To create a datagramSocket following constructors can be used:

protected DatagramSocket DatagramSocket():


Syntax: public DatagramSocket()throws SocketException
Creates a datagramSocket and binds it to any available port on local machine. If this constructor is used,
the OS would assign any port to this socket.

protected DatagramSocket DatagramSocket(int port):-


Syntax: public DatagramSocket(int port)throws SocketException
Parameters: port - port to which socket is to be bound
Throws:SocketException - If the socket cannot be bound to the specific local port. Creates a
DatagramSocket and binds to the specified port on the local machine.
protected DatagramSocket DatagramSocket(int port, InetAddress inetaddress):-
Syntax: public DatagramSocket(int port, InetAddress inetaddress) throws SocketException
Parameters:
port - port to which socket is to be bound.
inetaddress - local address to which socket is to be bound.
Throws:
SocketException - If the socket cannot be bound to the specific local port. It creates a DatagramSocket
and binds it to specified port and ip-address.
Creation of DatagramPacket:
In this step, the packet for sending/receiving data via a datagramSocket is created.
Constructor to send data:
DatagramPacket(byte buf[], int length, InetAddress inetaddress, int port):-
Syntax: public DatagramPacket(byte[] buf, int offset, int length, SocketAddress address)
Parameters:
buf - the packet data.
offset - the packet data offset.
length - the packet data length.
address - the destination socket address.
Constructs a DatagramPacket for sending data at specified address and specified port.
Constructor to receive the data:
DatagramPacket(byte buf[], int length):-
Syntax: public DatagramPacket(byte buf[],int length)
Parameters:
buf - the packet data.
length - the packet data length.
Constructs a DatagramPacket for receiving the data of length length in the byte array buf.
Invoke a send() or receive() call on socket object
Syntax: void send(DatagramPacket packet) throws SocketException
Parameters:
packet - Datagrampacket to send.
Throws:
SocketException - If there is an error in binding.
IllegalArgumentException - if address is not supported by the socket.
Syntax: void receive(DatagramPacket packet)throws SocketException
Parameters:
packet - Datagrampacket to receive from this socket.
Throws:
SocketException - If there is an error in binding.
IllegalArgumentException - if address is not supported by the socket.
● Program to simulate UDP server client
Ns3 Code

https://www.nsnam.org/docs/release/3.19/doxygen/udp-client-server_8cc_source.html

Folder path ⇒ Workspace ⇒ ns3allinone ⇒ ns3.32 ⇒ src ⇒ internet-apps


⇒ examples ⇒ udp-client-server ⇒ udp-client-server.cc

You might also like