Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

My program receives data from UDP (a packet of 414 byte data at a time, 400 times/sec)

and writes the received data (414 bytes) into a text file using fprintf()

and writes part of the data into image files using fprintf().

In a threaded function, the UDP communication and file write look like:

C++
while(1)
    {
        // Block until receive message from a client
        int recvMsgSize = udpServer.ReceiveFrom(buffer, UDP_DATA_MAX, senderIP, senderPort);

        if (recvMsgSize < 0)
        {
            AfxMessageBox("Receive failed");
        }

        buffer[recvMsgSize]='\0';

        if (dlg->m_log_running)
        {
            dlg_setting->WriteSerial(buffer, recvMsgSize);

            Decode_Image(buffer);
            Decode_NavData(buffer);
        }

        stringToHexa_WiresharkForm(buffer, recvMsgSize, buffer_hex);

        if (dlg->m_dataPrintType == 0) // print
            dlg->m_udpReceive.SetWindowTextA(buffer_hex);
        else // hide
            ;

        if (dlg->m_log_running)
            fprintf(ptr_logFile, "%s\n\n", buffer_hex);
    }


For small data, it works well.

But repeatedly there is data loss after receiving about 8 MB of data.

Both of the image files (also using fprintf() ) and the text file have lost data.

While, the data captured by WireShark has no lost data..


The problem would be in UDP receive or fprintf().

Is it a memory-related problem? I'd like to understand how it occurred.

Thank you for any advises..!
Posted
Updated 29-Jun-15 17:03pm
v5
Comments
[no name] 29-Jun-15 23:06pm    
This is expected. It doesn't work because of the design. You need to move away from UDP to TCP if you want reliable data transfer. https://en.wikipedia.org/wiki/User_Datagram_Protocol. "It has no handshaking dialogues, and thus exposes any unreliability of the underlying network protocol to the user's program. There is no guarantee of delivery, ordering, or duplicate protection."
Member 11499804 29-Jun-15 23:22pm    
Thank you. I understand that point. But the data captured concurrently by WireShark has no loss. Is something different in WireShark and C++ program?
[no name] 29-Jun-15 23:34pm    
This is not c++ related. Your client can be written in anything to receive the UDP packet. The UDP packet does not care. Your problem is network related. Wireshark uses pcap a low level protocol. One possible reason packets are being lost is that you are slowing the system with your fprinf() or other activities. This could be reduced by capturing data on a separate thread and placing in buffer.
[Edit] I just looked at your code. Without comments I can't be bothered to work out what it does but it does not look good as a time critical loop.
Don't forget Wireshark does not have to cope with these constraints.
Member 11499804 30-Jun-15 0:03am    
Do you mean that slowing the system with fprintf() can be reduced by capturing data on a thread and using fprintf() in a separate thread?
I'm very interested in the time critical loop because some laptop with low performance didn't write data with this program.
Could you please advise me about any improvement to make time critical loop?
[no name] 30-Jun-15 0:20am    
Roughly speaking have one thread dedicated to capturing data and placing in a buffer. Have other thread(s) reading from buffer and processing data. Experiment with buffer size and thread priorities. This is the way to achieve best throughput. If data is sent fast enough it will always overrun your client at some point. Considering your code what happens to data which arrives while processing is occurring. In fact I will reconsider my initial comment and say this is likely your problem.
As a test you could eliminate some of your code or try this http://www.codeproject.com/Articles/132623/Basic-UDP-Receiver

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