繁体   English   中英

NS-3套接字以连接到外部程序

[英]NS-3 Socket to connect to external program

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

有没有人遇到过类似的事情或有任何建议?

非常感激你的帮助!

我正在使用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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM