Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
34 views

36-2022 Shreya Jain - LAB 7 - Program For UPD-client-server Using NS3 (Ubuntu) and Java (In Eclipse)

The document describes a lab assignment to simulate a UDP client-server using NS3 and Java. It includes the source code for a UDP client-server model in NS3 that creates nodes, channels, IP addresses, and client and server applications. It also includes the source code for UDP client and server programs in Java that send and receive datagram packets over port 3500. Running the simulation in NS3 generates a network animation output. The conclusion states the UDP client-server was successfully simulated using both NS3 and Java.

Uploaded by

PUBG Noob
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

36-2022 Shreya Jain - LAB 7 - Program For UPD-client-server Using NS3 (Ubuntu) and Java (In Eclipse)

The document describes a lab assignment to simulate a UDP client-server using NS3 and Java. It includes the source code for a UDP client-server model in NS3 that creates nodes, channels, IP addresses, and client and server applications. It also includes the source code for UDP client and server programs in Java that send and receive datagram packets over port 3500. Running the simulation in NS3 generates a network animation output. The conclusion states the UDP client-server was successfully simulated using both NS3 and Java.

Uploaded by

PUBG Noob
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Shreya Jain

36

Lab Assignment 7: Program for UDP-client-server

Aim: Program for UDP-client-server using NS3(ubuntu) and Java(in Eclipse)

Objective: Simulate UDP-client-server using NS3(ubuntu) and Java(in Eclipse)

Source Code:

● UDP-client-server using NS3:

#include<fstream>
#include "ns3/core-module.h"
#include "ns3/core-module.h”
#include"ns3/csma-module.h"

#include"ns3/applications-module
.h" #include
"ns3/internet-module.h"

using namespace ns3;

NS_LOG_COMPONENT_DEFINE ("UdpClientServerExample");

int
main (int argc, char *argv[])
{

LogComponentEnable ("UdpClient", LOG_LEVEL_INFO);


LogComponentEnable ("UdpServer", LOG_LEVEL_INFO);
Shreya Jain
36
bool useV6 =
false; Address
serverAddress;

CommandLine cmd;

cmd.AddValue ("useIpv6", "Use Ipv6",


useV6); cmd.Parse (argc, argv);
NS_LOG_INFO ("Create
nodes."); NodeContainer n;

n.Create (2);

InternetStackHelper
internet; internet.Install
(n);

NS_LOG_INFO ("Create channels.");

CsmaHelper csma;
csma.SetChannelAttribute ("DataRate", DataRateValue
(DataRate (5000000))); csma.SetChannelAttribute ("Delay",
TimeValue (MilliSeconds (2))); csma.SetDeviceAttribute ("Mtu",
UintegerValue (1400));

NetDeviceContainer d = csma.Install (n);

NS_LOG_INFO ("Assign IP
Addresses."); if (useV6 == false)

Ipv4AddressHelper ipv4;
ipv4.SetBase ("10.1.1.0", "255.255.255.0");
Shreya Jain
36
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."); uint16_t port =

4000;

UdpServerHelper server (port);


ApplicationContainer apps = server.Install
(n.Get (1)); apps.Start (Seconds (1.0));

apps.Stop (Seconds (10.0));

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));
Shreya Jain
36
apps.Start (Seconds (2.0));

apps.Stop (Seconds (10.0));

NS_LOG_INFO ("Run
Simulation."); Simulator::Run ();

NS_LOG_INFO ("Done.");
Simulator::Destroy ();
}

● UDP-client-server using Java:

Udp_Server.java:

package ppt;

import java.io.IOException; import


java.net.DatagramPacket; import
java.net.DatagramSocket;

public class Udp_Server {

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

DatagramSocket server = new DatagramSocket(3500); byte[] buf =


new byte[256];

DatagramPacket packet = new DatagramPacket(buf, buf.length);

server.receive(packet);

String response = new String(packet.getData());


System.out.println("Response Data= "+ response); server.close();

} }
Shreya Jain
36

➔ Udp_Client.java:

package ppt;

import

java.io.IOException;

import

java.net.DatagramPacket

; import

java.net.DatagramSocket

; import

java.net.InetAddress;

public class Udp_Client {

public static void main(String[] args) throws

IOException { DatagramSocket client =

new DatagramSocket(); InetAddress

add=

InetAddress.getByName("localhost");

String str= "Hello World";

byte[] buf= str.getBytes() ;


Shreya Jain
36
DatagramPacket pkt = new

DatagramPacket(buf,buf.length, add, 3500);

client.send(pkt);

Output:

● UDP-client-server using NS3:

● NetAnimation:
Shreya Jain
36

● UDP-client-server using Java:

Conclusion: Successfully Simulated UDP-client-server using NS3 and Java.


Shreya Jain
36

You might also like