NS-3 Socket连接外部程序

时间:2013-07-03 18:27:50

标签: c++ sockets ns-3

我正在尝试编写一个套接字类来将我的NS-3模拟连接到外部程序。所以我想要做的是在NS-3中创建数据包并通过这个套接字将它们发送到外部工具,对该工具中的数据包进行一些简单的操作,然后将其发送回NS-3。我认为内置的NS-3插座不能用于此目的。

有没有人之前遇到过这样的事情或有任何建议?

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

我正在使用TCP套接字使用NS-3连接外部Python TCP套接字,这里是代码:

/*
 * Create a NS-3 Application that opens a TCP Socket and 
 * waits for incoming connections
 *
 */
#include "ns3/icmpv4.h"
#include "ns3/assert.h"
#include "ns3/log.h"
#include "ns3/ipv4-address.h"
#include "ns3/socket.h"
#include "ns3/integer.h"
#include "ns3/boolean.h"
#include "ns3/inet-socket-address.h"
#include "ns3/packet.h"
#include "ns3/trace-source-accessor.h"
#include "ns3/config.h"
#include "ns3/tos-device.h"
#include "ns3/names.h"
#include "ns3/string.h"
#include "ns3/object.h"


namespace ns3 {

IOProxyServer::IOProxyServer ()
{
    m_socket = 0;
}

TypeId IOProxyServer::GetTypeId (void)
{
  static TypeId tid = TypeId ("ns3::IoProxyServer")
    .SetParent<Application> ()
    .AddConstructor<IOProxyServer> ()
    .AddAttribute("RemotePortNumber",
                        "Remote port listening for connections",
                        IntegerValue(9999),
                        MakeIntegerAccessor(&IOProxyServer::m_remotePortNumber),
                        MakeIntegerChecker<int64_t>())
    .AddAttribute("RemoteIp",
                        "Remote IP listening for connections",
                        StringValue("127.0.0.1"),
                        MakeStringAccessor(&IOProxyServer::m_remoteIp),
                        MakeStringChecker())
    .AddAttribute("LocalPortNumber",
                        "Local port for incoming connections",
                        IntegerValue(3333),
                        MakeIntegerAccessor(&IOProxyServer::m_localPortNumber),
                        MakeIntegerChecker<int64_t>())
    .AddAttribute("LocalIp",
                        "Local IP for incoming connections",
                        StringValue("127.0.0.1"),
                        MakeStringAccessor(&IOProxyServer::m_localIp),
                        MakeStringChecker());
  return tid;
}


void IOProxyServer::StartApplication (void)
{
  NS_LOG_FUNCTION (this);

  m_socket = Socket::CreateSocket (GetNode (), TypeId::LookupByName ("ns3::TcpSocketFactory"));
  NS_ASSERT_MSG (m_socket != 0, "An error has happened when trying to create the socket");


  InetSocketAddress src = InetSocketAddress (Ipv4Address::GetAny(), m_localPortNumber );
  InetSocketAddress dest = InetSocketAddress(Ipv4Address(m_remoteIp.c_str()), m_remotePortNumber);

  int status;
  status = m_socket->Bind (src);
  NS_ASSERT_MSG (status != -1, "An error has happened when trying to bind to local end point");

  status = m_socket->Connect(dest);
  NS_ASSERT_MSG (status != -1, "An error has happened when trying to connect to remote end point");

  // Configures the callbacks for the different events related with the connection

  //m_socket->SetConnectCallback


  m_socket->SetAcceptCallback (
    MakeNullCallback<bool, Ptr<Socket>, const Address &> (),
    MakeCallback (&IOProxyServer::HandleAccept, this));

  m_socket->SetRecvCallback (
    MakeCallback (&IOProxyServer::HandleRead, this));

  m_socket->SetDataSentCallback (
    MakeCallback (&IOProxyServer::HandleSend,this));

  //m_socket->SetSendCallback

  m_socket->SetCloseCallbacks (
    MakeCallback (&IOProxyServer::HandlePeerClose, this),
    MakeCallback (&IOProxyServer::HandlePeerError, this));

  // If we need to configure a reception only socket or a sending only socket
  // we need to call one of the following methods:
  // m_socket->ShutdownSend();
  // m_socket->ShutdownRecv();
}

void IOProxyServer::StopApplication (void)
{
  NS_LOG_FUNCTION (this);
  m_socket->Close();
}

void IOProxyServer::HandlePeerClose (Ptr<Socket> socket)
{
  NS_LOG_FUNCTION (this << socket);
}

void IOProxyServer::HandlePeerError (Ptr<Socket> socket)
{
  NS_LOG_FUNCTION (this << socket);
}

void IOProxyServer::HandleSend (Ptr<Socket> socket, uint32_t dataSent)
{
  NS_LOG_FUNCTION (this << socket);
}

void IOProxyServer::HandleAccept (Ptr<Socket> s, const Address& from)
{
  NS_LOG_FUNCTION (this << s << from);
  s->SetRecvCallback (MakeCallback (&IOProxyServer::HandleRead, this));
}

void IOProxyServer::HandleRead (Ptr<Socket> socket)
{
  NS_LOG_FUNCTION (this << socket);
  Ptr<Packet> packet;

  while ((packet = socket->RecvFrom (from)))
    {
      if (packet->GetSize () == 0)
        { //EOF
          break;
        }

      if (InetSocketAddress::IsMatchingType (from))
        {
           //Do whatever you need with the incoming info 
        }

    }
}

void IOProxyServer::SendData()
{
    //Do whatever you need for creating your packet and send it using the socket

    //Ptr<Packet> packet = Create<Packet>(pointer, sizeof(pointer));
    //m_socket->Send(packet, 0, from);
}

IOProxyServer::~IOProxyServer ()
{

}

void IOProxyServer::DoDispose (void)
{
  NS_LOG_FUNCTION (this);
  m_socket = 0;
  Application::DoDispose ();
}

} // namespace ns3