本文介绍了如何计算到达目的地( NS2 )的跳数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 NS2 ..in TCL 文件中,如何计算一条路径中到达无线网络目的地的跳数?

In NS2 ..in TCL file, how can I calculate number of Hops in one path to reach the destination in wireless Network?

我需要你的帮助.

推荐答案

你必须在协议的代码中这样做...
首先我不知道你要使用哪种协议,还好ns2中的协议非常相似...所以我会指导你如何去做.

You must do that in the protocol's codes...
First of all i don't know which protocol you want to use, but fortunately the protocols in ns2 are very similar to each other... So i will c guide you to how to do it.

我将使用 AODV 协议作为示例...
理论上:

I will use AODV protocol as a sample...
In theory :

我们会说当一个 RREQ 数据包到达时,增加当前路由的数据包跳数...

We'll say when a RREQ packet has arrived, increment the packet hop count for current rout...

这是最简单的方法:

This is the simplest way :

现在打开 aodv.cc 例如 /ns-allinone-2.35/ns-2.35/aodv/aodv.cc 和在说:

Now open aodv.cc For example /ns-allinone-2.35/ns-2.35/aodv/aodv.cc and in the lines that says :

// First check if I am the destination ..

if (rq->rq_dst == index) {

if (rq->rq_dst == index) { 后点击 Enter 并写入:

Click Enter after if (rq->rq_dst == index) { and write :

printf("Hop_Count_ %d | Packet with uid %d | from %d to %d ",rq->rq->rq_hop_count,ch->uid,ih->saddr(),ih->daddr());

此代码将打印到达目的地的每个数据包的跳数.

This Code will print the hop count for each packet that arrived to the destination.

在终端 cd 中进入您的 ns2 目录并键入 make 并单击 Enter.例如:

In terminal cd into your ns2 directory and type make and click Enter.For example :

$ cd /ns-allinone-2.35/ns-2.35/
$ make

这是最难的方式:
在代码中:



This is the hardest way :
In code :

从你的 ns2 目录打开 aodv_packet.h... 例如 /ns-allinone-2.35/ns-2.35/aodv/aodv_packet.h

Open aodv_packet.h from your ns2 directory... For example /ns-allinone-2.35/ns-2.35/aodv/aodv_packet.h

找到 struct hdr_aodv_request { 并在其中创建一个称为 hop_count_...

Find struct hdr_aodv_request { and create an interget variable in it called hop_count_...

喜欢:struct hdr_aodv_request {int hop_count_;

现在打开/ns-allinone-2.35/ns-2.35/aodv/aodv.cc中的aodv.cc,找到sendRequest(Packet*p) 函数可能看起来像 void AODV::sendRequest(nsaddr_t dst) {...

Now open aodv.cc from /ns-allinone-2.35/ns-2.35/aodv/aodv.cc and find the definition of sendRequest(Packet *p) function that might look like void AODV::sendRequest(nsaddr_t dst) {...

现在在这个函数中你必须看到一些代码,比如:

Now in this function you must see some codes like :

// Fill up some more fields.
rq->rq_type = AODVTYPE_RREQ;
rq->rq_hop_count = 1;
rq->rq_bcast_id = bid++;
rq->rq_dst = dst;
rq->rq_dst_seqno = (rt ? rt->rt_seqno : 0);
rq->rq_src = index;
seqno += 2;
assert((seqno % 2) == 0);
rq->rq_src_seqno = seqno;
rq->rq_timestamp = CURRENT_TIME;

它说的地方 rq->rq_timestamp = CURRENT_TIME; 只需单击 Enter 并编写打击代码:

And where it says rq->rq_timestamp = CURRENT_TIME; just click Enter and write the blow code :

rq->hop_count_ = 0;

现在你需要找到 recvRequest(Packet *p) 的定义,它可能看起来像 void AODV::recvRequest(Packet *p) {...

Now you need to find the definition of recvRequest(Packet *p) that might look like void AODV::recvRequest(Packet *p) {...

在这个函数中找到说:

In this function find the lines that says :

/*
 * Can't reply. So forward the  Route Request
 */
else {

else { 之后点击 Enter 并写入:

After else { click Enter and write :

rq->hop_count_++;

在终端 cd 中进入您的 ns2 目录并键入 make 并单击 Enter.例如:

In terminal cd into your ns2 directory and type make and click Enter.For example :

$ cd /ns-allinone-2.35/ns-2.35/
$ make

现在进行打印,您可以使用我上面写的第一个解决方案.

Now for printing you can use the first solution that i wrote above.

这篇关于如何计算到达目的地( NS2 )的跳数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-20 13:24