关于NS -2仿真的两个问题

最近在自学NS -2,遇到了一些问题,对照《NS与网络模拟》一书中的第一个例子自己操作了一下,但是一直在报错。各位大神能否帮忙解答一下,万分感谢!!!!

代码如下

set ns [new Simulator]
set tracefd [open example1.tr w]
$ns trace-all $tracefd
set namtracefd [open example1.nam w]
$ns namtrace-all $namtracefd
proc finish {} {
   global ns tracefd namtracefd
   $ns flush-trace
   close $tracefd
   close $namtracefd
   exec nam example1.nam &
   exit 0
}
set n0 [$ns node]
set n1 [$ns node]
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_500
$cbr0 set interval_0.005
$cbr0 attach-agent $udp0
set null0 [new Agent/Null]
$ns attach-agent $n1 $null0
$ns connect $udp0 $null0
$ns at 0.5 "$cbr0 start"
$ns at 4.5 "$cbr0 stop"
$ns at 5.0 "finish"
$ns run

报错结果如下

这个packet定义书上就是这样写的,我不知道到底错在了那里T-T

还有一个问题就是nam我已经安装好了,也能打开out.nam文件,可是打不开自己编写并且编译通过tcl脚本生成的nam

————————————————————————————————————————————————————————

—————————————————————————————————————问题解决了!!!!!!!!

第一个问题应该在packetSize_后面加空格,语法规范和C太不一样了,书上印刷看不太出来,tcl脚本的空格起得作用太大了

$cbr0 set packetSize_ 500

第二个问题输入指令nam 文件名.nam就可以进去了,不知道为什么从nam软件的file进不去,等弄明白了再写出来供大家参考

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基于NS-3的WIFI性能仿真代码示例: ``` #include "ns3/core-module.h" #include "ns3/wifi-module.h" #include "ns3/mobility-module.h" #include "ns3/internet-module.h" #include "ns3/applications-module.h" using namespace ns3; NS_LOG_COMPONENT_DEFINE ("WifiPerformance"); int main (int argc, char *argv[]) { // 设置日志级别 LogComponentEnable ("WifiPerformance", LOG_LEVEL_INFO); // 创建节点 NodeContainer wifiNodes; wifiNodes.Create (2); // 配置物理层参数 WifiHelper wifi; wifi.SetStandard (WIFI_PHY_STANDARD_80211n_2_4GHZ); wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode", StringValue ("HtMcs0"), "ControlMode", StringValue ("HtMcs0")); // 创建wifi设备 NetDeviceContainer devices = wifi.Install (WifiPhyHelper::Default (), WifiMacHelper::Default (), wifiNodes); // 设置移动模型 MobilityHelper mobility; mobility.SetPositionAllocator ("ns3::GridPositionAllocator", "MinX", DoubleValue (0.0), "MinY", DoubleValue (0.0), "DeltaX", DoubleValue (5.0), "DeltaY", DoubleValue (10.0), "GridWidth", UintegerValue (3), "LayoutType", StringValue ("RowFirst")); mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel", "Bounds", RectangleValue (Rectangle (-50, 50, -50, 50))); mobility.Install (wifiNodes); // 安装网络协议栈 InternetStackHelper stack; stack.Install (wifiNodes); // 分配IP地址 Ipv4AddressHelper address; address.SetBase ("10.1.1.0", "255.255.255.0"); Ipv4InterfaceContainer interfaces = address.Assign (devices); // 创建流量发生器和接收器 uint16_t port = 9; OnOffHelper onOffHelper ("ns3::UdpSocketFactory", Address (InetSocketAddress (interfaces.GetAddress (1), port))); onOffHelper.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]")); onOffHelper.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]")); onOffHelper.SetAttribute ("PacketSize", UintegerValue (1024)); onOffHelper.SetAttribute ("DataRate", StringValue ("50Mbps")); ApplicationContainer onOffApps = onOffHelper.Install (wifiNodes.Get (0)); onOffApps.Start (Seconds (1.0)); onOffApps.Stop (Seconds (10.0)); PacketSinkHelper sinkHelper ("ns3::UdpSocketFactory", Address (InetSocketAddress (Ipv4Address::GetAny (), port))); ApplicationContainer sinkApps = sinkHelper.Install (wifiNodes.Get (1)); sinkApps.Start (Seconds (1.0)); sinkApps.Stop (Seconds (10.0)); // 启动仿真器 Simulator::Stop (Seconds (10.0)); Simulator::Run (); Simulator::Destroy (); return 0; } ``` 代码中创建了两个节点,配置了物理层参数,设置了移动模型,安装了网络协议栈并分配了IP地址,创建了流量发生器和接收器,最后启动了仿真器。 要运行该代码,需要安装NS-3并编译该代码。运行命令如下: ``` ./waf --run wifi-performance ``` 其中,`wifi-performance`是代码文件名。在运行过程中,程序会输出日志信息和仿真结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值