读书人

OnConnect遇到FD_Read程序是自动执行

发布时间: 2013-10-22 16:16:51 作者: rapoo

OnConnect碰到FD_Read,程序是自动执行OnSend(表示可以发数据),还是根据FD_Read调用OnReceive()函数内容。
请教各位大侠,
客户端在第一次调用
void socket::onconnect()
{ ...
AsyncSelelct(FD_Read);
...
}
程序是自动执行OnSend,表示可以发数据了,还是根据FD_Read调用OnReceive()函数内容。 socket onconnect onreceive
[解决办法]
没看懂你要问什么。流程就是,你先尝试调用发送,看send返回值,如果发送成功,你可以继续调用发送。如果发送失败,但错误表示异步等待(ewouldblock),则需等onsend,才能再次send
[解决办法]

引用:
MFC中客户端socket在建立完成后经过connect()函数连接上服务端,会自动触发Onconnect()函数表示连接完成。然后缓冲区为空应该自动触发OnSend()函数,表示可以发送数据了。可是,我在Onconnect函数里加上手动事件申请AsyncSelelct(FD_Read),应该会触发OnReceive()函数。。。要问的是,这个程序段中,当Onconnect表明连接完成后,程序接下来的工作是默认的促发OnSend()函数,还是根据程序触发OnReceive()函数。谢谢

CAsyncSocket::OnReceive ()

Called by the framework to notify this socket that there is data in the buffer that can be retrieved by calling the Receive member function.
[解决办法]
引用:
没人知道吗。求大神

异步都是请求应答式,你不调用相关动作就不会有相关事件通知。你调用了如果没有事件到来也不会有通知。
[解决办法]
引用:
MFC中客户端socket在建立完成后经过connect()函数连接上服务端,会自动触发Onconnect()函数表示连接完成。然后缓冲区为空应该自动触发OnSend()函数,表示可以发送数据了。可是,我在Onconnect函数里加上手动事件申请AsyncSelelct(FD_Read),应该会触发OnReceive()函数。。。要问的是,这个程序段中,当Onconnect表明连接完成后,程序接下来的工作是默认的促发OnSend()函数,还是根据程序触发OnReceive()函数。谢谢

应该是在CSocket.OnSend()中调用CSocket.Send()
跟Recieve的过程是一样的。
// CMyAsyncSocket is derived from CAsyncSocket and defines the 
// following variables:
// CString m_sendBuffer; //for async send
// int m_nBytesSent;
// int m_nBytesBufferSize;

void CMyAsyncSocket ::OnSend(int nErrorCode)
{
while (m_nBytesSent < m_nBytesBufferSize)
{
int dwBytes;

if ((dwBytes = Send((LPCTSTR)m_sendBuffer + m_nBytesSent,
m_nBytesBufferSize - m_nBytesSent)) == SOCKET_ERROR)
{
if (GetLastError() == WSAEWOULDBLOCK) break;
else
{
TCHAR szError[256];
wsprintf(szError, "Server Socket failed to send: %d",
GetLastError());
Close();
AfxMessageBox (szError);
}
}
else
{
m_nBytesSent += dwBytes;
}
}
if (m_nBytesSent == m_nBytesBufferSize)
{
m_nBytesSent = m_nBytesBufferSize = 0;
m_sendBuffer = "";
}
CAsyncSocket::OnSend(nErrorCode);
}

读书人网 >VC/MFC

热点推荐