ns-3源代码

时间:2013-04-11 13:49:47

标签: networking simulator ns-3

ns-3用户! 我是ns-3的新手 请帮我理解一下: http://code.nsnam.org/ns-3-dev/file/tip/examples/wireless/wifi-simple-adhoc-grid.cc

我在这里无法理解(从209到217的行):

  $ TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
  Ptr<Socket> recvSink = Socket::CreateSocket (c.Get (sinkNode), tid); //sinkNode=0
  InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny (), 80);
  recvSink->Bind (local);
  recvSink->SetRecvCallback (MakeCallback (&ReceivePacket));
  Ptr<Socket> source = Socket::CreateSocket (c.Get (sourceNode), tid);
  InetSocketAddress remote = InetSocketAddress (i.GetAddress (sinkNode, 0), 80);
  source->Connect (remote);$

这里发生了什么? 仅供参考:附加代码。

请帮助我! 谢谢你们! :)

1 个答案:

答案 0 :(得分:5)

以下评论的源代码。

获取可以创建udp套接字的工厂类的唯一ID

TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");

实际上在节点“sinkNode”(无论是什么)上创建一个udp套接字

Ptr<Socket> recvSink = Socket::CreateSocket (c.Get (sinkNode), tid);

将我们刚创建的套接字绑定到'any'ipv4地址

InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny (), 80);
recvSink->Bind (local);

使套接字将收到的数据包转发到“ReceivePacket”函数

recvSink->SetRecvCallback (MakeCallback (&ReceivePacket));

在发送节点上创建一个udp套接字

Ptr<Socket> source = Socket::CreateSocket (c.Get (sourceNode), tid);

将udp套接字连接到在sink节点上创建的udp套接字的ip地址和端口号。这只是为通过此套接字发送的数据包设置默认的“to”ip地址

InetSocketAddress remote = InetSocketAddress (i.GetAddress (sinkNode, 0), 80);
source->Connect (remote);