Q2NS dev
ns-3 module
Loading...
Searching...
No Matches
q2ns-3-classical-comms-example.cc
Go to the documentation of this file.
1/*-----------------------------------------------------------------------------
2 * Q2NS - Quantum Network Simulator
3 * Copyright (c) 2026 quantuminternet.it
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *---------------------------------------------------------------------------*/
9/**
10 * @file q2ns-3-classical-comms-example.cc
11 *
12 * @brief Minimal ns-3 UDP communication example intended for q2ns users who may not already be
13 *familiar with classical ns-3 networking.
14 *
15 * Demonstrates:
16 * - Creating nodes
17 * - Installing Internet stack
18 * - Creating a point-to-point link
19 * - Sending and receiving a UDP packet
20 *---------------------------------------------------------------------------*/
21
22#include "ns3/q2ns-netcontroller.h"
23#include "ns3/q2ns-qnode.h"
24
25#include "ns3/core-module.h"
26#include "ns3/internet-module.h"
27#include "ns3/network-module.h"
28#include "ns3/point-to-point-module.h"
29
30#include <iostream>
31
32using namespace ns3;
33using namespace q2ns;
34
35
36int main() {
37
38 std::cout << "[DEMO] Classical communication (A->B) starting\n";
39
40 ns3::RngSeedManager::SetSeed(1);
41 ns3::RngSeedManager::SetRun(1);
42
43 // QNode inherits from ns3::Node, so it can be used directly with ordinary
44 // ns-3 networking helpers (Internet stack, sockets, etc.).
45 NetController net;
46 auto A = net.CreateNode();
47 auto B = net.CreateNode();
48
49
50 // Classical networking setup
51 // We install the ns-3 Internet stack so the nodes can send IP/UDP packets.
52 InternetStackHelper internet;
53 internet.Install(A);
54 internet.Install(B);
55
56
57 // Create a simple point-to-point classical link between A and B.
58 // This gives us a direct network connection with fixed bandwidth and delay.
59 PointToPointHelper p2p;
60 p2p.SetDeviceAttribute("DataRate", StringValue("100Mbps"));
61 p2p.SetChannelAttribute("Delay", StringValue("1ms"));
62 NetDeviceContainer devices = p2p.Install(A, B);
63
64
65 // Assign IPv4 addresses to the two ends of the link.
66 // ns-3 requires this before we can send UDP packets.
67 Ipv4AddressHelper ip;
68 ip.SetBase("10.1.1.0", "255.255.255.0");
69 Ipv4InterfaceContainer interfaces = ip.Assign(devices);
70
71
72 // UDP socket setup
73 // We create a UDP receiver on B and a sender on A.
74 const uint16_t port = 9000;
75
76 // Create Bob's UDP socket, bind it to the chosen port, and attach the callback that will run
77 // whenever a packet arrives.
78 Ptr<Socket> bobSocket = Socket::CreateSocket(B, UdpSocketFactory::GetTypeId());
79 InetSocketAddress local = InetSocketAddress(Ipv4Address::GetAny(), port);
80 bobSocket->Bind(local);
81 bobSocket->SetRecvCallback([](Ptr<Socket> socket) {
82 while (Ptr<Packet> packet = socket->Recv()) {
83 std::cout << "[B][classical] Received UDP packet at " << Simulator::Now().GetSeconds()
84 << " s, size = " << packet->GetSize() << " bytes\n";
85 }
86 });
87
88 // Create Alice's UDP socket and connect it to Bob's address and port.
89 // interfaces.GetAddress(1) is Bob's IP address on the point-to-point link.
90 // Now, aliceSocket->Send(...) will transmit packets to Bob.
91 Ptr<Socket> aliceSocket = Socket::CreateSocket(A, UdpSocketFactory::GetTypeId());
92 InetSocketAddress remote = InetSocketAddress(interfaces.GetAddress(1), port);
93 aliceSocket->Connect(remote);
94
95
96
97 // Simulation scheduling
98 // Schedule a UDP packet to be sent from A to B after 1 second of simulation time.
99 Simulator::Schedule(Seconds(1.0), [aliceSocket]() {
100 Ptr<Packet> packet = Create<Packet>(4);
101 aliceSocket->Send(packet);
102 std::cout << "[A][classical] Sent UDP packet at " << Simulator::Now().GetSeconds() << " s\n";
103 });
104
105 Simulator::Stop(Seconds(10));
106 Simulator::Run();
107
108 std::cout << "[DONE] Classical communication (A->B) finished\n";
109
110 Simulator::Destroy();
111 return 0;
112}
Main user-facing facade for creating and configuring a quantum network.
ns3::Ptr< QNode > CreateNode(const std::string &label="")
Create a QNode with an optional human-readable label.