问题描述
嗨
我有一个用于从套接字接收数据的简单应用程序.
我有一个基于 CAsyncSocket 的类,该类为其OnReceive函数重写了一个函数.当OnReceive事件引发时,我去获取数据.
我的硬件是一个产生计数器并通过网络发送数据的电路板.当我想接收数据时,数据会丢失. (我会在收到邮件后通过检查计数器找到此未结).董事会生成并发送数据,我应该同时接收.
但是当我使用GUI玩游戏时(例如,单击标题栏),就会开始丢失数据.
我试图在线程中进行接收以删除其对gui的依赖性,但是我在数据接收方面没有获得任何收益.
有什么建议吗?
-------------------------------------------------- ------------------------------------
我的项目是Dialog Base MFC.有三个成员变量:
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()
{
// 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:
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);
}
}
通过这种方法,数据接收更好!比CSocket使用.但是当我在Windows上玩游戏(单击,Alt + Tab,...)时,接收过程遇到问题.
我猜想Windows在几微秒内都没有收到数据,并且我连续不断的数据流坏了!!!
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!!!
推荐答案
这篇关于即时的!从网络接收的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!