How can I calculate number of hops to reach destin

2019-09-02 23:29发布

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

I need your help.

标签: tcl wireless ns2
1条回答
手持菜刀,她持情操
2楼-- · 2019-09-03 00:18

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.

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

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


This is the simplest way :

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) {

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.

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 :

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

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

Like : struct hdr_aodv_request { int hop_count_;

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;

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

rq->hop_count_ = 0;

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 {

After else { click Enter and write :

rq->hop_count_++;

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.


查看更多
登录 后发表回答