質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Ubuntu

Ubuntuは、Debian GNU/Linuxを基盤としたフリーのオペレーティングシステムです。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Q&A

1回答

835閲覧

ns3で”ns3-hello-world.cc”を実行することができない

hatana25

総合スコア13

Ubuntu

Ubuntuは、Debian GNU/Linuxを基盤としたフリーのオペレーティングシステムです。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

0グッド

0クリップ

投稿2017/09/04 05:58

ns3で"ns3-hello-world.cc"というプログラムを実行したところ,
コンパイルはできるのですが,エラーが出てしまいます.

ソースコードは

/* -- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -- /
/
payload-test.cc: send a greeting message between two nodes

  • F.Qian, Oct. 2013
  • src(source) -----net(10Mbps,5ms)----- dst(destination)
  • 192.168.1.0/24

*/

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

#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"
#include "ns3/tcp-header.h"
#include "ns3/udp-header.h"

#define NET_MASK "255.255.255.0"
#define NET_ADDR "192.168.1.0"
#define FIRST_NO "0.0.0.1"
#define PORT 50000

using namespace ns3;

NS_LOG_COMPONENT_DEFINE ("PayloadTest");

void
ReceivePacket (Ptr<Socket> socket)
{
Ptr<Packet> packet;
Address from;

uint8_t buf[1024]; memset(buf, 0, sizeof(buf)); while ((packet = socket->RecvFrom (from))) { if (packet->GetSize () > 0) { packet->CopyData(buf, 1024); InetSocketAddress iaddr = InetSocketAddress::ConvertFrom (from); char t[10]; sprintf(t,"%8.5f",Simulator::Now ().GetSeconds ()); std::cout << std::setw(10) << std::setprecision(4) << t << " received "<< packet->GetSize () << " bytes from: (" << iaddr.GetIpv4 () << ", " << iaddr.GetPort () << ")" << " --- " << buf << std::endl; } }

}

InetSocketAddress
setSocketAddress(Ptr<Node> node, uint32_t port)
{
Ipv4InterfaceAddress adr = node->GetObject <Ipv4> ()->GetAddress(1, 0);
return InetSocketAddress (Ipv4Address(adr.GetLocal()), port);
}

int main (int argc, char *argv[], char *envp[])
{
CommandLine cmd;
uint16_t times=1;
std::string greeting_msg = "hello ns3 world !!!";

//timesには送信する回数、greetingには送信するメッセージを指定する cmd.AddValue("times", "The number of times to send.", times); cmd.AddValue("greeting", "The greeting message from source to destination.", greeting_msg); cmd.Parse(argc, argv); //ノードを生成する NS_LOG_DEBUG("Creating Topology"); Ptr<Node> src = CreateObject<Node> (); Ptr<Node> dst = CreateObject<Node> (); NodeContainer net_nodes; net_nodes.Add (src); net_nodes.Add (dst); //データリンク層の設定を行う PointToPointHelper p2p; p2p.SetDeviceAttribute ("DataRate", StringValue ("5Mbps")); p2p.SetChannelAttribute ("Delay", StringValue ("2ms")); p2p.SetQueue ("ns3::DropTailQueue"); NetDeviceContainer devices; devices = p2p.Install (net_nodes); //ネットワーク層の設定を行う InternetStackHelper stack; stack.InstallAll (); Ipv4AddressHelper address; address.SetBase (NET_ADDR, NET_MASK, FIRST_NO); Ipv4InterfaceContainer ifs = address.Assign (devices); NS_LOG_INFO ("Network : " << ifs.GetAddress(0, 0) << " - " << ifs.GetAddress(1, 0)); NS_LOG_INFO ("Initialize Global Routing."); Ipv4GlobalRoutingHelper::PopulateRoutingTables (); // あて先ノードのソケットを生成する TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory"); NS_LOG_INFO ("set destination's socket."); Ptr<Socket> sink_socket = Socket::CreateSocket (dst, tid); InetSocketAddress dstSocketAddr = setSocketAddress (dst, PORT); sink_socket->Bind (dstSocketAddr); sink_socket->SetRecvCallback (MakeCallback (ReceivePacket)); // ソースノードのソケットを生成する NS_LOG_INFO ("set source's socket."); Ptr<Socket> src_socket = Socket::CreateSocket (src, tid); InetSocketAddress srcSocketAddr = setSocketAddress (src, PORT); src_socket->Bind (srcSocketAddr); src_socket->SetAllowBroadcast (true); //あて先のピアアドレスに接続する src_socket->Connect (dstSocketAddr); // 送信するパケットを組み立てる uint8_t buf[1024]; uint16_t bufsize; bufsize = greeting_msg.size(); memcpy(buf, greeting_msg.c_str(),bufsize); Ptr<Packet> packet = Create<Packet> (buf, bufsize); //パケットを送信する for(uint16_t i=0;i<times;i++) src_socket->Send (packet); Simulator::Stop (Seconds(10.0)); Simulator::Run (); Simulator::Destroy (); return 0;

}

です.

コマンドは
./waf --run "ns3-hello-world"

エラーは
Waf: Entering directory /home/hatanaka/ns-allinone-3.26/ns-3.26/build' Waf: Leaving directory /home/hatanaka/ns-allinone-3.26/ns-3.26/build'
Build commands will be stored in build/compile_commands.json
'build' finished successfully (0.562s)
Command ['/home/hatanaka/ns-allinone-3.26/ns-3.26/build/scratch/ns3-hello-world'] terminated with signal SIGSEGV. Run it under a debugger to get more information (./waf --run <program> --command-template="gdb --args %s <args>").

です.

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

keicha_hrs

2017/09/04 14:16 編集

(長すぎたので修正)まずはteratailのヘルプを読んでMarkdown記法を理解し、質問の体裁を何とかしてください。
guest

回答1

0

#include <iostream> #include <fstream> #include <string> #include <iomanip> #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" #include "ns3/tcp-header.h" #include "ns3/udp-header.h" #define NET_MASK "255.255.255.0" #define NET_ADDR "192.168.1.0" #define FIRST_NO "0.0.0.1" #define PORT 50000 using namespace ns3; NS_LOG_COMPONENT_DEFINE ("PayloadTest"); void ReceivePacket (Ptr<Socket> socket) { Ptr<Packet> packet; Address from; uint8_t buf[1024]; memset(buf, 0, sizeof(buf)); while ((packet = socket->RecvFrom (from))) { if (packet->GetSize () > 0) { packet->CopyData(buf, 1024); InetSocketAddress iaddr = InetSocketAddress::ConvertFrom (from); char t[10]; sprintf(t,"%8.5f",Simulator::Now ().GetSeconds ()); std::cout << std::setw(10) << std::setprecision(4) << t << " received "<< packet->GetSize () << " bytes from: (" << iaddr.GetIpv4 () << ", " << iaddr.GetPort () << ")" << " --- " << buf << std::endl; } } } InetSocketAddress setSocketAddress(Ptr<Node> node, uint32_t port) { Ipv4InterfaceAddress adr = node->GetObject <Ipv4> ()->GetAddress(1, 0); return InetSocketAddress (Ipv4Address(adr.GetLocal()), port); } void SendPacket (Ptr<Socket> socket, Ptr<Packet> packet) { socket->Send (packet); } int main (int argc, char *argv[], char *envp[]) { CommandLine cmd; uint16_t times=1; std::string greeting_msg = "hello ns3 world !!!"; //timesには送信する回数、greetingには送信するメッセージを指定する cmd.AddValue("times", "The number of times to send.", times); cmd.AddValue("greeting", "The greeting message from source to destination.", greeting_msg); cmd.Parse(argc, argv); //ノードを生成する NS_LOG_DEBUG("Creating Topology"); Ptr<Node> src = CreateObject<Node> (); Ptr<Node> dst = CreateObject<Node> (); NodeContainer net_nodes; net_nodes.Add (src); net_nodes.Add (dst); //データリンク層の設定を行う PointToPointHelper p2p; p2p.SetDeviceAttribute ("DataRate", StringValue ("5Mbps")); p2p.SetChannelAttribute ("Delay", StringValue ("2ms")); // p2p.SetQueue ("ns3::DropTailQueue"); NetDeviceContainer devices; devices = p2p.Install (net_nodes); //ネットワーク層の設定を行う InternetStackHelper stack; stack.InstallAll (); Ipv4AddressHelper address; address.SetBase (NET_ADDR, NET_MASK, FIRST_NO); Ipv4InterfaceContainer ifs = address.Assign (devices); NS_LOG_INFO ("Network : " << ifs.GetAddress(0, 0) << " - " << ifs.GetAddress(1, 0)); NS_LOG_INFO ("Initialize Global Routing."); Ipv4GlobalRoutingHelper::PopulateRoutingTables (); // あて先ノードのソケットを生成する TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory"); NS_LOG_INFO ("set destination's socket."); Ptr<Socket> sink_socket = Socket::CreateSocket (dst, tid); InetSocketAddress dstSocketAddr = setSocketAddress (dst, PORT); sink_socket->Bind (dstSocketAddr); sink_socket->SetRecvCallback (MakeCallback (ReceivePacket)); // ソースノードのソケットを生成する NS_LOG_INFO ("set source's socket."); Ptr<Socket> src_socket = Socket::CreateSocket (src, tid); InetSocketAddress srcSocketAddr = setSocketAddress (src, PORT); src_socket->Bind (srcSocketAddr); src_socket->SetAllowBroadcast (true); //あて先のピアアドレスに接続する src_socket->Connect (dstSocketAddr); // 送信するパケットを組み立てる uint8_t buf[1024]; uint16_t bufsize; bufsize = greeting_msg.size(); memcpy(buf, greeting_msg.c_str(),bufsize); Ptr<Packet> packet = Create<Packet> (buf, bufsize); //パケットを送信する for(uint16_t i=0;i<times;i++) { // src_socket->Send (packet); Simulator::Schedule(ns3::Seconds(i), &SendPacket, src_socket, packet); } Simulator::Stop (Seconds(10.0)); Simulator::Run (); Simulator::Destroy (); return 0; }

変更点としては
・SendPacket関数の定義、Simulator::ScheduleからSendPacket関数を呼び出してSend処理を行うように変更
・p2p.SetQueue ("ns3::DropTailQueue")のコメントアウト
です。

Socket->Send()の処理はSimulator::Scheduleで登録してあげないと上手く動きません(以前自分も同じところでエラーになりました)
DropTailQueueについては仕様が変わってると思うので、どうしても必要な場合は自分で探してみてください。

投稿2019/03/22 04:55

wawawanet

総合スコア12

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問