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

I want to ask some advice regarding my program.

It is supposed to receive data packets, each with a size of 400 bytes. Transmit speed is 414 packets per second.

But the stored data in ptr_logFile_hex (text file) omits some of data as

1, 3, 5, 8, 10, ... (when packet number 1...10 was sent)

while Wireshark program can monitor all data sent.


Is there any suggestion to resolve the problem?

Actually, fprintf(ptr_logFile, "%s\n\n", buffer); is not necessary because the ultimate aim is to store data converted to hex.


The receive function is:

C++
UINT ReceiveData(LPVOID pParam)
{
	CPS_GCSDlg *dlg=(CPS_GCSDlg*)pParam;
	CSocket udpServer;
	int errorCode = 0;

	CString s;
	s.Format(_T(" (Port: %d) "), PORT_NUM_DEFAULT);
	dlg->m_portNum.SetWindowText(s);

	// Create socket for sending/receiving datagrams
	if (udpServer.Create(PORT_NUM_DEFAULT, SOCK_DGRAM, NULL) == 0) 
	{
		AfxMessageBox("Socket creation failed");
	}

	CString senderIP;
	UINT senderPort;
	// Buffer for data string
	char buffer[UDP_DATA_MAX];
	char buffer_hex[UDP_DATA_MAX*4];
	
	struct ip_mreq mreq;
	mreq.imr_multiaddr.s_addr = inet_addr("224.10.10.10");
	mreq.imr_interface.s_addr = htonl(INADDR_ANY);
	if (setsockopt(udpServer, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&mreq, sizeof(mreq)) < 0)
	{
		AfxMessageBox("Socket option set failed");
	}

	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';

		dlg->stringToHexa_WiresharkForm(buffer, recvMsgSize, buffer_hex);

		if (dlg->m_dataPrintType == 0)
			dlg->m_udpReceive.SetWindowTextA(buffer_hex);
		else if (dlg->m_dataPrintType == 1)
			dlg->m_udpReceive.SetWindowTextA(buffer);

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

	return 0;
}  


UDP_DATA_MAX is defined as 2048.

The function stringToHexa_WiresharkForm is:

C++
void CPS_GCSDlg::stringToHexa_WiresharkForm(char * str, UINT len, char * hex)
{
    char tmp[10];

    int k = 0;
    for (int i = 0; i < len; i++)
    {
        if (i % 16 == 0 && i != 0)
            hex[k++] = '\n';
        else if (i % 8 == 0 && i != 0)
            hex[k++] = ' ';

        sprintf(tmp, "%02X", (unsigned char)str[i]);
        hex[k++] = tmp[0];
        hex[k++] = tmp[1];

        hex[k++] = ' ';
    }

    hex[k] = '\0';
}


Thank you!
Posted
Comments
Jochen Arndt 1-Apr-15 10:33am    
It looks like your receive function is running inside a worker thread.

If so, you should not access MFC GUI controls owned by another thread. Otherwise your application will fail sooner or later.

I'm not sure if that is related to your problem but you may comment the SetWindowText calls and check if you then got more packages.
Member 11499804 2-Apr-15 9:10am    
Thank you!

It seems resolved after I comment the SetWindowText calls.
jeron1 1-Apr-15 11:03am    
Are you receiving all the packets? Could you remove most or all data processing and logging and put some test code in to see if you get all packets all the time. Maybe write a small amount of test code to see if you indeed receive 1,2,3,...10 in correct order?
Member 11499804 2-Apr-15 9:10am    
Thank you!

I checked my code with test codes and it seems resolved now.

1 solution

You should not use UDP for such an application, unless you include your own message checking. UDP[^] is not reliable, as messages can appear in any order and are not guaranteed to be delivered. If the data is important then you should be using TCP.
 
Share this answer
 
Comments
Member 11499804 1-Apr-15 10:34am    
Thank you!
Unfortunately, I should use UDP. Isn't it a problem of application speed?
Is there any possible modification of UDP usage to get all the data (order is not important) ?
Richard MacCutchan 1-Apr-15 10:45am    
Your choice, but you will still have the same problem.

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