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);
if (udpServer.Create(PORT_NUM_DEFAULT, SOCK_DGRAM, NULL) == 0)
{
AfxMessageBox("Socket creation failed");
}
CString senderIP;
UINT senderPort;
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)
{
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:
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!