Click here to Skip to main content
15,900,598 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
i have a simple application for receiving data from socket.
i have a class based on CAsyncSocket that i override a function for its OnReceive function. when the OnReceive Event raised i go and get the data.

my hardware is a board that produces counter and sends data by network. when i want to receive data, the data is missed. (i find this miss by checking counter after receive). the board generates and sends the data and i should receive it concurrently.

but when i play with the GUI, for example by clicking the title bar , the miss of data, begins.

i tried to do my receive in a thread in order to remove its dependency to gui, but i don't achieve any gain in data receiving.

is there any suggestion???

--------------------------------------------------------------------------------------

my project is Dialog Base MFC. there is three member variables:

C++
struct sockaddr_in m_DestinationAddress;
SOCKET m_ServerSock;
HANDLE hRecThread;


connecting to the server is done by this code. after connection, we create a thread for receiving.

C++
void CSendReceive_LANDlg::OnButton5() 
{
	// TODO: Add your control notification handler code here
	m_DestinationAddress.sin_family		= AF_INET;
	m_DestinationAddress.sin_port		= htons(20482);
	m_DestinationAddress.sin_addr.S_un.S_addr=inet_addr("192.168.1.111");
	
	if((m_ServerSock = ::socket(AF_INET,SOCK_STREAM,0)) == INVALID_SOCKET)
	{
		MessageBox(_T("Can't open socket!"), _T("Send Receive LAN"), MB_ICONERROR|MB_OK);
		return ;
	}
	if (connect (m_ServerSock, (PSOCKADDR) &m_DestinationAddress, sizeof (m_DestinationAddress)) == SOCKET_ERROR)
	{
		MessageBox(_T("Can't connect to Board!"), _T("Send Receive LAN"), MB_ICONERROR|MB_OK);
		closesocket (m_ServerSock);
		return ;
	}

	GetDlgItem(IDC_BUTTON2)->EnableWindow();
	GetDlgItem(IDC_BUTTON3)->EnableWindow();

	// run save thread
	hRecThread	= CreateThread(NULL, 0, RecThread, this, 0, NULL);
	SetThreadPriority(hRecThread, /*THREAD_PRIORITY_NORMAL*/THREAD_PRIORITY_TIME_CRITICAL);
	SetThreadAffinityMask(hRecThread,0x8);
}


thread function is:

C++
DWORD WINAPI RecThread(LPVOID lpParameter)
{
	((CSendReceive_LANDlg*)lpParameter)->RecLoop();
	return 0;
}

char rbuffer[5*1024*1024];
void CSendReceive_LANDlg::RecLoop()
{
	int len,rlen;

	len=5*1024*1024;

	while(TRUE)
	{
		rlen=recv(m_ServerSock, rbuffer, len, 0);

	}

}


by this method, receiving of data is better! than CSocket use. but when i play with windows (clicking, Alt+Tab, ...), receive process encounter a problem.
i guess that Windows didn't receive data for a few micro seconds and my continous stream of data is broken!!!
Posted
Updated 24-Jul-12 3:39am
v2
Comments
Sergey Alexandrovich Kryukov 11-Jul-12 1:43am    
Not enough information. There is more than one way to screw up thing. A short but complete code sample could possibly help...
--SA

1 solution

i updated this question by adding my source code, but my problem still remains.
 
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