Request help on ns 3.38, getting error "Failed to bind socket"

176 views
Skip to first unread message

Ali M

unread,
May 28, 2023, 8:40:18 PM5/28/23
to ns-3-users
Hi,

I am on a ubuntu VM, and I have tried running the program with elevated privileges as well, and checked if any port is being used by running command "$ netstat -tuln | grep <portnumber>
$ netstat -tuln | grep <portnumber>
$ sudo lsof -i :<portnumber>"

This is my program's output,
"./ns3 run scratch/sample1.cc
step1
step2
step3
install tcp/ip stack
step4
step5
msg="Failed to bind socket", +1.000000000s 0 file=/home/ashraf/project/ns-allinone-3.38/ns-3.38/src/applications/model/onoff-application.cc, line=213
NS_FATAL, terminating
terminate called without an active exception
Command 'build/scratch/ns3.38-sample1-default' died with <Signals.SIGABRT: 6>.

"
Program:-
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"

using namespace ns3;

int main(int argc, char *argv[]) {
  // Create nodes
  NodeContainer nodes;
  nodes.Create(2);

  // Create point-to-point link
  PointToPointHelper p2p;
  p2p.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
  p2p.SetChannelAttribute("Delay", StringValue("2ms"));

  NetDeviceContainer devices;
  devices = p2p.Install(nodes);
  printf("step1 \n");
  // Install TCP/IP stack
  InternetStackHelper stack;
  stack.Install(nodes);
  printf("step2\n");
  // Assign IP addresses
  Ipv4AddressHelper address;
  address.SetBase("10.1.1.0", "255.255.255.0");
  Ipv4InterfaceContainer interfaces = address.Assign(devices);
  printf("step3\n");
  // Create a simple traffic generator
  OnOffHelper onoff("ns3::TcpSocketFactory", Address());
  onoff.SetAttribute("OnTime", StringValue("ns3::ConstantRandomVariable[Constant=1]"));
  onoff.SetAttribute("OffTime", StringValue("ns3::ConstantRandomVariable[Constant=0]"));
  onoff.SetAttribute("DataRate", StringValue("1Mbps"));
  onoff.SetAttribute("PacketSize", UintegerValue(1000));
  printf("install tcp/ip stack \n");
  // Install the traffic generator on the sender node
  ApplicationContainer sourceApps;
  sourceApps = onoff.Install(nodes.Get(0));
  sourceApps.Start(Seconds(1.0));
  sourceApps.Stop(Seconds(10.0));
  printf("step4\n");
  // Create a packet sink application on the receiver node
PacketSinkHelper sinkHelper("ns3::TcpSocketFactory", InetSocketAddress(interfaces.GetAddress(0), 12345));

  ApplicationContainer sinkApp = sinkHelper.Install(nodes.Get(1));
    printf("step5\n");
  sinkApp.Start(Seconds(0.0));
  sinkApp.Stop(Seconds(10.0));

  Simulator::Run();
  Simulator::Destroy();

  return 0;
}

Please let me know.
Thanks,
Ali

Tommaso Pecorella

unread,
May 31, 2023, 10:08:37 PM5/31/23
to ns-3-users
The problem is in this line:
OnOffHelper onoff("ns3::TcpSocketFactory", Address());

You must specify the destination address as the 2nd argument as an InetSocketAddress(sinkAddress, port), where sinkAddress is the IP address of the sink application, and port is its port number.

You can also use onoff.SetAttribute("Remote", InetSocketAddress(sinkAddress, port)); later on. Choose.
Reply all
Reply to author
Forward
0 new messages