how to convert ipv4 address from const char to integer

575 views
Skip to first unread message

azize tunc

unread,
Nov 16, 2016, 8:19:24 PM11/16/16
to ns-3-users
I have below code. I am trying to iterate through my hosts and create UDP client and server application. My topology has a csma bridge that has many hosts. I am adding udp client and server application ever 60 seconds.

I know what the ip addresses of each host in const char type. but when going through each hosts in a loop, the ip address will also change. i want to assign that number to integer and pass it to ipv4Address class.

Can somebody help me with how i can convert the string value of ipv4 address to integer?



// We've got the "hardware" in place.  Now we need to add IP addresses.
  //
  printf ("Assign IP Addresses.\n");
  Ipv4AddressHelper ipv4;
  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
  ipv4.Assign (terminalDevices);
....
....
......
for(int i = 0; i < 3600; i += 60)
  {
      randomClientNode = getRandomNumber(0,49);
      printf("Random Client Node %d\n",randomClientNode);
      
      randomServerNode = getRandomNumber(0,49);
      printf("Random Server Node %d\n",randomServerNode);
      
     if(randomClientNode != randomServerNode)
     {

         if( previousClientNode != randomClientNode)
         {
             // Traffic Generater
             int trafficGenIpNumber = randomServerNode + 1;
             OnOffHelper onoff("ns3::UdpSocketFactory", Address (InetSocketAddress (Ipv4Address ("10.1.1.randomServerNode"), port)));
             onoff.SetAttribute("PacketSize", UintegerValue(1024));
             app.Add(onoff.Install (terminals.Get (randomClientNode)));
             // Start the application
             app.Start (Seconds (i));
             app.Stop (Seconds (3600 - i));
             //Traffic receiver
             // Create an optional packet sink to receive these packets
             PacketSinkHelper sink("ns3::UdpSocketFactory",Address (InetSocketAddress (Ipv4Address::GetAny (), port)));
             sinkApps.Add(sink.Install (terminals.Get (randomServerNode)));
             sinkApps.Start (Seconds (0.0));
         }
     }
     previousClientNode = randomClientNode;
  }   

Nazmul Alam

unread,
Nov 16, 2016, 8:51:36 PM11/16/16
to ns-3-users
Hi,
Just a suggestion, how about directly using Ipv4Address::m_address?

azize tunc

unread,
Nov 16, 2016, 8:56:56 PM11/16/16
to ns-3-...@googlegroups.com
thank you. 

I used GetAddress from Ipv4InterfaceContainer 

 Ipv4InterfaceContainer interfaceContainer = ipv4.Assign (terminalDevices);
OnOffHelper onoff("ns3::UdpSocketFactory", Address (InetSocketAddress (interfaceContainer.GetAddress(randomServerNode), port)));



--
Posting to this group should follow these guidelines https://www.nsnam.org/wiki/Ns-3-users-guidelines-for-posting
---
You received this message because you are subscribed to a topic in the Google Groups "ns-3-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ns-3-users/LlW89cXI7Q0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ns-3-users+unsubscribe@googlegroups.com.
To post to this group, send email to ns-3-...@googlegroups.com.
Visit this group at https://groups.google.com/group/ns-3-users.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages