更快的数据接收和保存在UDP通信中 [英] Faster data receive and save in UDP communication

查看:110
本文介绍了更快的数据接收和保存在UDP通信中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好



我想问一些关于我的计划的建议。



它应该是接收数据包,每个数据包的大小为400字节。传输速度是每秒414个数据包。



但是ptr_logFile_hex(文本文件)中存储的数据省略了一些数据,因为



1,3,5,8,10 ......(当发送包号1 ... 10时)



而Wireshark程序可以监控所有发送的数据。





有什么建议可以解决这个问题吗?



实际上,fprintf(ptr_logFile,%s\\\
\ n,缓冲区);没有必要,因为最终的目的是存储转换为十六进制的数据。





接收功能是:



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:

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定义为2048.



函数stringToHexa_WiresharkForm是:





UDP_DATA_MAX is defined as 2048.

The function stringToHexa_WiresharkForm is:

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!

推荐答案

你应该除非您包含自己的消息检查,否则不要将UDP用于此类应用程序。 UDP [ ^ ]不可靠,因为消息可以按任何顺序出现,并且不保证可以传递。如果数据很重要,那么您应该使用TCP。
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.


这篇关于更快的数据接收和保存在UDP通信中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆