2013-07-03 27 views
1

我想寫一個套接字類來連接我的NS-3模擬到外部程序的方法。所以我想要做的是在NS-3中創建數據包,並通過此套接字將它們發送到外部工具,對該工具中的數據包執行一些簡單的操作,然後將其發送回NS-3。我不認爲內置的NS-3插座可以用於此目的。NS-3插座連接到外部程序

有沒有人遇到過這樣的事情或有任何建議?

非常感謝您的幫助!

回答

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