Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello,I have program that use receiving data in udp protocol and broadcast ,the program work good in debug mode ,but in release mode it loss data,
help me.
thank you
Posted
Updated 20-Dec-10 5:14am
v2
Comments
fjdiewornncalwe 20-Dec-10 11:11am    
I'd love to help you, but you need to give us a little more information. Could you be more specific about the errors/loss of data you are experiencing.

UDP is not the protocol you want to use if you want to ensure that you get ALL of the data. You actually want to use TCP/IP.

 
Share this answer
 
v2
C#
BOOL CUDPServerSocket::Connect(){   char            psz[1024];  addrinfo        local;  struct           addrinfo hints;    BOOL Result = FALSE;    try
    {       ////**************************************************************      WSAData data;       WORD wVersion = MAKEWORD(2,0);      int retcode = ::WSAStartup(wVersion,&data);     if (retcode != 0) {         LogError("Winsock 2 not supported on this machine\n");          return FALSE;       }       ////**************************************************************      memset(&local, 0, sizeof(local));       //Now we populate the sockaddr_in structure     local.ai_family=AF_INET; //Address family       local.ai_socktype=SOCK_DGRAM;       local.ai_protocol=IPPROTO_UDP;      //*****************************************************************     ZeroMemory(&hints, sizeof(hints));      hints.ai_family = AF_INET;      hints.ai_socktype = SOCK_DGRAM;     hints.ai_protocol = IPPROTO_UDP;        hints.ai_flags = AI_PASSIVE;        //the socket function creates our SOCKET        m_SrvSocketUDP=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);      char* BuffRecvLane;     BuffRecvLane= new char[1024*1024];      long LenBuffLane=1024*1024;     int iResultop =setsockopt(m_SrvSocketUDP,SOL_SOCKET,SO_RCVBUF,BuffRecvLane,LenBuffLane);        //**************************************************************        m_RecvAddr.sin_family = AF_INET;        m_RecvAddr.sin_port = htons(m_nPortNo);     m_RecvAddr.sin_addr.s_addr = htonl(INADDR_ANY);;        char pszPort[128];      sprintf(pszPort, "%d", m_nPortNo);      ///*************************************************************        int Resultbind  = bind(m_SrvSocketUDP, (SOCKADDR *) &m_RecvAddr, sizeof(m_RecvAddr));       if (Resultbind!=0)      {           int nErrCode = WSAGetLastError();           sprintf(psz, "An error occured bind socket: %s", CGlobalTools::ErrorString(nErrCode));          LogError(psz);      }       else{           ListenToPort();     }       // Creation and Initialization is OK        return TRUE;    }   catch(...)  {       LogError("CTCPServerSocket::CreateSocket Failed."); }   return FALSE;   }BOOL  CUDPServerSocket::ListenToPort(){    bool ResultAcceptp=false;   try
    {       m_bStopSocket       = FALSE;        sockaddr_in clientAddr;     clientAddr.sin_family=AF_INET;      int AddrLen = sizeof(clientAddr);       if(m_hReciveThread)     {           _ASSERT(0);     }               m_hReciveThread = CreateThread(NULL, 0, ThreadRecive, (LPVOID)this, 0/*CREATE_SUSPENDED*/, NULL);       if(m_hReciveThread)     {           SetThreadPriority(m_hReciveThread, THREAD_PRIORITY_HIGHEST);        }   }   catch(...)  {       LogError("CTCPServerSocket::ListenToPort Failed."); }   return  FALSE;}DWORD WINAPI CUDPServerSocket::ThreadRecive(LPVOID lpParam){ CUDPServerSocket* pThis = (CUDPServerSocket*)lpParam;   pThis->ReciveData();    return 0;}void CUDPServerSocket::ReciveData(){      int         nReciveSize     = 0;    int         nOffset         = 0;    BYTE        yPreCounter     = 0;    char        psz[1024];  int         nSOPFIndex      = 0; // Start Of Packet Flag Index  int Size_RecvAddr=sizeof(m_RecvAddr);        m_WriteFile=new CFile();    char spz[1024];     __int64 PosFile=0;  sprintf(spz,"C:\\dataBoard%d",m_nPortNo);   m_WriteFile->Open(spz,CFile::modeCreate|CFile::modeReadWrite); while(!m_bStopSocket)   {       if(SOCKET_ERROR == (nReciveSize =     recvfrom(m_SrvSocketUDP, (char*)m_BuffPacketRecv ,UDP_PACKET_RECIVE ,0, (SOCKADDR *)&m_RecvAddr, &Size_RecvAddr)))        {           int nErrCode = WSAGetLastError();           m_WriteFile->Write(m_BuffPacketRecv,nReciveSize);           sprintf(psz, "An error occured while receiving data: %s", CGlobalTools::ErrorString(nErrCode));         LogError(psz);          break;      }       //*************************************************             //m_WriteFile->LockRange(PosFile,nReciveSize);      //m_WriteFile->Write(m_BuffPacketRecv,nReciveSize);     //m_WriteFile->UnlockRange(PosFile,nReciveSize);        //PosFile+=nReciveSize;     //*************************************************
    }   delete[] m_BuffPacketRecv;}BOOL CUDPServerSocket::Send(BYTE *pData, int nLength){   int nSize = 0;  try
    {       if(!m_bIsConnected)         return FALSE;       nSize =send(m_SocketUDP, (char*)pData, nLength, 0); }   catch(...)  {       LogError("CTCPServerSocket::Send Failed."); }       return (nSize == nLength);}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900