简体   繁体   English

使用异步套接字的回调

[英]Callbacks using asynchronous sockets

My asking is quite simple and is about asynchronous sockets, working with TCP protocol. 我的问题很简单,它涉及使用TCP协议的异步套接字。

When I send some data with the "BeginSend" method, when will the callback be called? 当我使用“ BeginSend”方法发送一些数据时,何时调用回调?

Will it be called when the data is just sent out to the network, or when we are ensured that the data as reached its destination (like it should be regarding to TCP specification) ? 当数据刚刚发送到网络时,或者当我们确保数据到达其目的地时(如它应该与TCP规范有关),会调用它吗?

Thanks for your answers. 感谢您的回答。

KiTe. 风筝。

ps : I'm sorry if my english is a bit bad ^^. ps:对不起,我的英语不好一点^^。

From MSDN: 从MSDN:

"When your application calls BeginSend, the system will use a separate thread to execute the specified callback method, and will block on EndSend until the Socket sends the number of bytes requested or throws an exception." “当您的应用程序调用BeginSend时,系统将使用一个单独的线程来执行指定的回调方法,并将在EndSend上阻塞,直到Socket发送请求的字节数或引发异常为止。”

"The successful completion of a send does not indicate that the data was successfully delivered. If no buffer space is available within the transport system to hold the data to be transmitted, send will block unless the socket has been placed in nonblocking mode." “成功完成发送并不表示数据已成功传递。如果传输系统中没有可用的缓冲区空间来保存要发送的数据,则除非套接字已置于非阻塞模式,否则发送将阻塞。”

http://msdn.microsoft.com/en-us/library/38dxf7kt.aspx http://msdn.microsoft.com/en-us/library/38dxf7kt.aspx

When the callback is called you can be sure that the data has been cleared from the output buffer (the asynchronous operation uses a separate thread to ensure that your calling thread is not blocked in case there is no room in the transmit buffer and it has to wait to send the date) and that it will reach it's destination - but not that it has reached it yet. 调用回调时,可以确保已从输出缓冲区中清除了数据(异步操作使用单独的线程来确保在传输缓冲区中没有空间的情况下不会阻塞您的调用线程,并且必须等待发送的日期),它会到达它的目的地-但并不表明它已经达到了它。

Because of the TCP protocol's nature however, you can be sure (well, I guess almost sure) that it will get to the destination, eventually. 但是,由于TCP协议的性质,您可以确定(嗯,我想几乎可以肯定)它最终会到达目的地。

However, for timing purposes you should not consider the time of the callback as being the same as the time the data reaches the other party. 但是,出于计时目的,您不应将回调时间与数据到达另一方的时间相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM