udp指向windows多宿主服务器上的通信

时间:2015-01-08 17:50:42

标签: c++ windows sockets udp boost-asio

在我的多播接收器应用程序中,我加入一个组并成功接收多播数据。 此外,还有填补空白的api。这使用udp。当检测到间隙时,发送重传请求。有一个专门用于接收和处理udp数据报的线程,这些数据报响应请求而返回。 这一切都在带有一个接口卡的Windows机器上运行良好。现在我们需要让它在具有2个NIC卡的多宿主机器上运行。 要使多播工作,我们必须添加路由,以便应用程序将连接发送到正确的NIC。同样,这部分工作正常。

然而,点对点udp receive_from方法在进入时立即引发错误。我的理解是sender_endpoint是用方法本身初始化的。我是否需要为此udp套接字做一些特殊操作才能在输入时丢失错误?我是否需要以某种方式绑定套接字或在主机级别设置特殊路由? 任何帮助将不胜感激。

这是返回的错误:  错误:reRequestMoldUdp64 :: waitForResponseNT出错:提供了无效参数

boost::asio::ip::address    m_targetIP;     
short                       m_port;
udp::endpoint           m_receiver_endpoint;
m_receiver_endpoint.address(m_targetIP);
m_receiver_endpoint.port(m_port);   

if (!m_socket.is_open()) {  
        openUdpSocket();
}

while (1)
{
        m_last_ReceiveLen = 0;
        udp::endpoint sender_endpoint;

        try {
            //m_last_ReceiveLen = m_socket.receive_from( (boost::asio::buffer(m_Buffer, max_length)), sender_endpoint);
            m_last_ReceiveLen = m_socket.receive_from( (boost::asio::buffer(m_Buffer, max_length)), sender_endpoint, 0, ec);
            if (ec) {
                _snprintf(logBuf, sizeof(logBuf), "%s got error:%s", __FUNCTION__, ec.message().c_str());
                MyLog(LOG_ERROR, logBuf);
                myExit(__FILE__, __LINE__, 1);  
            }
        }

        catch (std::exception& e) {                 
            _snprintf(logBuf, sizeof(logBuf), "INFO:%s sender_endpoint.address[%s] error:[%s]",
                            __FUNCTION__, 
                            sender_endpoint.address().to_string().c_str(),
                            e.what());
            MyLogAlways(logBuf);
            if (!m_socket.is_open()) {  
                openUdpSocket();
            }           
            if (++m_waitForResponseNTRetryCnt > 100) {
                myExit(__FILE__, __LINE__, 1);          
            }
            else {
                Sleep(100);     // 100 ms
                continue;           
            }
        }
        std::string dummyStr;
        const UINT64 dummySeqno(0);
        udpResponseAPI(BuildMap,dummySeqno, dummyStr);
    }
}

void reRequestMoldUdp64::openUdpSocket()
{
    char logBuf[512];

    if (!m_socket.is_open()) {      
        m_socket.open(udp::v4(), m_Error);
    }
    else {
        return;
    }   

    if (!m_socket.is_open()) {      
        _snprintf(logBuf, sizeof(logBuf), "%s udp socket didn't reopen error:[%s]", __FUNCTION__, m_Error.message().c_str());
        MyLog(LOG_ERROR, logBuf);       
    }
    else {          
        _snprintf(logBuf, sizeof(logBuf), "%s udp socket successfully opened", __FUNCTION__);
        MyLogAlways(logBuf);
    }
}
//----------------------------------------------------------------------------------------------------
void reRequestMoldUdp64::sendRequest(std::string request)
{   
    MyLog("INFO:Begin sendRequest");

    m_lastRequestSent = request;

    char myBuff[128];
    int len(request.length());
    memcpy(myBuff, request.c_str(), len);   
#if 0
    _snprintf(logBuf, sizeof(logBuf), "INFO:Before sendto() len:%d ip:%s port:%hu",
                len,
                m_receiver_endpoint.address().to_string().c_str(),
                m_receiver_endpoint.port()
                );

    MyLogAlways(logBuf);
#endif
    _snprintf(logBuf, sizeof(logBuf),"%s sending udp rerequest to:%s", __FUNCTION__, 
                                                m_receiver_endpoint.address().to_string().c_str());
    MyLogAlways(logBuf);

    if (m_socket.is_open()) {
        m_socket.send_to(boost::asio::buffer(myBuff, len), m_receiver_endpoint);    
    }
    else {
        m_socket.open(udp::v4(), m_Error);
        m_socket.send_to(boost::asio::buffer(myBuff, len), m_receiver_endpoint);    
        MyLogAlways("ERR:reRequest socket is not open");
    }

}

0 个答案:

没有答案