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:
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.
void CSendReceive_LANDlg::OnButton5()
{
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();
hRecThread = CreateThread(NULL, 0, RecThread, this, 0, NULL);
SetThreadPriority(hRecThread, THREAD_PRIORITY_TIME_CRITICAL);
SetThreadAffinityMask(hRecThread,0x8);
}
thread function is:
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!!!