2016-08-01 49 views
0

单播(一对一)UDP通信,每次收到的数据包不相同;如果我在500ms的间隔内发送1000个数据包,我会丢失9个数据包。我正在开发Windows平台VCC 6.0;使用sendto系统调用发送以太网数据包。在主机端,我错过了校验和错误或报头错误的数据包。单播UDP数据包未命中

如果您需要更多详细信息,请让我知道。我的议程是我不应该错过目标端的任何数据包。 有关这个问题的任何帮助将不胜感激。

{ 
    //Initialize local variables 
    MAINAPP(pAppPtr); 
    int iResult = 0; 
    int sRetVal = 0; 
    static char cTransmitBuffer[1024]; 
    unsigned long ulTxPacketLength =0; 
    int in_usTimeOut = 0; 
    unsigned short usTimeout = 0; 

    S_QJB_POWER_CNTRL S_Out_QJB_Power_Cntrl = {0}; 

    pAppPtr->S_Tcp_Handle.Tcp_Tx_Msg.m_ucHeader[0] = QJB_TCP_HEADER_BYTE1; 

    pAppPtr->S_Tcp_Handle.Tcp_Tx_Msg.m_ucHeader[1] = QJB_TCP_HEADER_BYTE2; 

    pAppPtr->S_Tcp_Handle.Tcp_Tx_Msg.m_usCmdID = QJB_ETH_POWER_ON; 

    pAppPtr->S_Tcp_Handle.Tcp_Tx_Msg.m_usCmdResults = 0; 

    pAppPtr->S_Tcp_Handle.Tcp_Tx_Msg.m_usDataSize = sizeof(S_QJB_POWER_CNTRL); 

    //Fill the controls & delay 
    sRetVal = PowerCntrlStructFill(&pAppPtr->S_Tcp_Handle.Tcp_Tx_Msg.U_Tcp_Msg.S_QJB_PowerCntrl,&usTimeout); 

    if(sRetVal) 
    { 
     return sRetVal; 
    } 

    pAppPtr->S_Tcp_Handle.Tcp_Tx_Msg.m_usReserved = 0; 

    pAppPtr->S_Tcp_Handle.Tcp_Tx_Msg.m_usChecksum = 0; 
    //Perform Endian Swap 
    pAppPtr->objEndianConv.EndianSwap(&pAppPtr->S_Tcp_Handle.Tcp_Tx_Msg.U_Tcp_Msg.S_QJB_PowerCntrl, &S_Out_QJB_Power_Cntrl); 

    //Frame the transmission packet 
    QJB_Frame_TXBuffer(cTransmitBuffer, &(pAppPtr->S_Tcp_Handle.Tcp_Tx_Msg), &ulTxPacketLength,(void *)&S_Out_QJB_Power_Cntrl); 

    //Send the data to the target 
    iResult = sendto(pAppPtr->sktConnectSocket,cTransmitBuffer,ulTxPacketLength,0,(struct sockaddr *)&pAppPtr->g_dest_sin, sizeof(pAppPtr->g_dest_sin)); 
    if(iResult == SOCKET_ERROR) 
    { 
     return QJB_TARGET_DISCONNECTED; 
    } 

    memset(&pAppPtr->S_Tcp_Handle.Tcp_Rx_Msg,0,sizeof(S_QJB_ETHERNET_PKT));// 1336 
    //Send the Command and obtain the response 
    sRetVal = QJB_ETHResRev(pAppPtr->sktConnectSocket,&pAppPtr->S_Tcp_Handle.Tcp_Rx_Msg,3); 
    return sRetVal; 
} 

Sathishkumar。

+1

UDP不提供保证,所以有些数据包可能会丢失。如果您需要传送所有数据包,请使用TCP。并请提供一些示例代码。 –

回答

0

不幸的是,由于UDP对传送没有任何保证,网络堆栈可以随时因任何原因丢弃发送的数据包。值得注意的是,这些数据包也不会受到订单保护。

如果订购和交付是您的应用程序的主要应用程序,我认为它应该考虑切换到TCP。

+0

我正在一个项目中工作,我不应该使用TCP;如果我们可以实现如何实现,可以在msdn中实现UDPlite? – satiz01bravo

+0

UDP-lite仍不能保证数据包传输,而只是允许应用程序处理“损坏”的数据。但是如果你仍然希望继续它,Linux已经支持添加内核> 2.6.20。对于窗户有一个WULL图书馆,我不认为它是免费的。你可以在这里了解它的实现:https://www.irisa.fr/temics/softwares/WULLUserGuide.pdf –