OnConnect() 在什么时候被调用?

TianGo123 2002-12-06 02:30:31
1


CAsyncSocket::OnConnect()
virtual void OnConnect( int nErrorCode );

Remarks

Called by the framework to notify this connecting socket that its connection attempt is completed, whether successfully or in error


函数被framework调用,表明正在连接的socket的连接意图已经完成,而不论连接
是不是成功

上面的翻译理解是不是对的呢?
我的理解是这样的:当调用了CAsyncSocket::Connect()函数后,在未来未知的时间里,framework将自动调用CAsyncSocket::OnConnect()

这样的理解对吗?
我测试了一下,设很多的断点,发现程序的流程不是很一致。









2


CAsyncSocket::OnReceive
virtual void OnReceive( int nErrorCode );


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. For more information, see the articleWindows Sockets: Socket Notifications in Visual C++ Programmer's Guide.

被framework调用,表明socket有data在buffer中,buffer可以通过调用Receive member function来得到。

理解:如果buffer中有data 的话,那么framework会调用CAsyncSocket::OnReceive。

这样的理解是不是对的呢?
...全文
788 19 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
TianGo123 2002-12-11
  • 打赏
  • 举报
回复
给分了,给的不公的地方请大家原谅
TianGo123 2002-12-11
  • 打赏
  • 举报
回复
to waitingforyou(中国男人) ( ) 信誉:100

Client端发出连接请求,并且Server同意Accept后,
Client端触发了OnConnect事件


我测试过的,即使没有server,只运行 client ,在connect()后,

ONCONNECT()马上就被调用了。
yanhuahui 2002-12-10
  • 打赏
  • 举报
回复
呵呵,但愿如此
TianGo123 2002-12-10
  • 打赏
  • 举报
回复
我自己总结一下,然后结帐:


在Connect()后,系统会自动调用OnConnect()函数

Connect()的返回值在我看来确实没有什么意义。

要判断是不是连接成功,在OnConnect()里面判断;

CMyAsyncSocket:;OnConnect(int nErrorCode)
{
if(nErrorCode == 0) //连接成功
{
}

else //连接不成功
{
}
}

就是这样的了

huche 2002-12-07
  • 打赏
  • 举报
回复
http://expert.csdn.net/Expert/topic/1235/1235060.xml?temp=9.058779E-02
Sander 2002-12-07
  • 打赏
  • 举报
回复
同意楼上的
qrlvls 2002-12-07
  • 打赏
  • 举报
回复
连接请求被接受或超时
Chiun 2002-12-07
  • 打赏
  • 举报
回复
我对waitingforyou(中国男人)得回贴有质疑,根据MSDN,CONNECT操作结束后就CALLBACK ONCONNECT, 操作结果作为参数传递。CAsyncSocket::OnConnect()又返回值吗?找了半天也没见到你得BOOL Connect( LPCTSTR lpszHostAddress, UINT nHostPort );
TianGo123 2002-12-06
  • 打赏
  • 举报
回复
BOOL Connect( LPCTSTR lpszHostAddress, UINT nHostPort );

BOOL Connect( const SOCKADDR* lpSockAddr, int nSockAddrLen );

Return Value


那,Connect的返回值有什么意义呢?

不懂!
qing_li73 2002-12-06
  • 打赏
  • 举报
回复
Have a try, debug whether it can be called when the connection attempt is completed in error.

I just posted the remarks of MSDN


yanhuahui 2002-12-06
  • 打赏
  • 举报
回复
TO : qing_li73(bluemoon) (:
不会吧,但是我感觉一般都是成功才调用的,你能否举个例子你见到的连不上仍调用?
TianGo123 2002-12-06
  • 打赏
  • 举报
回复
waitingforyou(中国男人) ( ) 信誉:100

Client端发出连接请求,并且Server同意Accept后



你的意思是说:“连接上了就调用OnConnect”吗?
waitingforyou 2002-12-06
  • 打赏
  • 举报
回复
Client端发出连接请求,并且Server同意Accept后,
Client端触发了OnConnect事件
qing_li73 2002-12-06
  • 打赏
  • 举报
回复
Remarks

Called by the framework to notify this connecting socket that its connection attempt is completed, whether successfully or in error.

Important In CSocket, the OnConnect notification function is never called.

For connections, you simply call Connect, which will return when the connection is completed (either successfully or in error). How connection notifications are handled is an MFC implementation detail.


TianGo123 2002-12-06
  • 打赏
  • 举报
回复
我觉得不是的
TianGo123 2002-12-06
  • 打赏
  • 举报
回复
连接不成功就不调用吗???????
yanhuahui 2002-12-06
  • 打赏
  • 举报
回复
同上
sdb72 2002-12-06
  • 打赏
  • 举报
回复
连接成功后
TianGo123 2002-12-06
  • 打赏
  • 举报
回复
谢谢你的回答

18,363

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧