socketprogramminginc(编辑修改稿)内容摘要:

er the data is received so that client can read it rather than client continuously polling for data. Well that is possible with a little effort. If you read the first part of this article, you already know that the Socket class in the namespace has several methods like Receive and Send which are blocking calls. Besides there are also functions like BeginReceive , BeginSend etc. These are meant for asynchronous IO . For example , there are at least two problems with the blocking Receive: When you call Receive function the call blocks if no data is present, the call blocks till some data arrives. Even if there is data when you made the receive call , you don39。 t know when to call next time. You need to do polling which is not an efficient way. You can argue that one can overe these shortings by multithreading meaning that one can spawn a new thread and let that thread do the polling which then notifies the main thread of the data. This concept could work well, but even if you create a new thread it would require your main thread to share the CPU time with this new thread. Windows operating system (Windows NT /2020 /XP) provide what is called Completion Port IO model for doing overlapped ( asynchronous) IO. The details of IO Completion port are beyond the scope of the current discussion, but to make it simple you can think of IO Completion Ports as the most efficient mechanism for doing asynchronous IO in Windows that is provided by the Operating system. Completion Port model can be applied to any kind of IO including the file read /write and serial munication. The .NET asynchronous socket programming helper class39。 s Socket provides the similar model. BeginReceive .NET framework39。 s Socket class provides BeginReceive method to receive data asynchronously ., in an nonblocking manner The BeginReceive method has following signature: public IAsyncResult BeginReceive( byte[] buffer, int offset, int size, SocketFlags socketFlags, AsyncCallback callback, object state )。 The way BeginReceive function works is that you pass the function a buffer , a callback function (delegate) which will be called whenever data arrives. The last parameter, object, to the BeginReceive can be any class derived from object ( even null ) . When the callback function is called it means that the BeginReceive function pleted which means that the data has arrived. The callback function needs to have the following signature: void AsyncCallback( IAsyncResult ar)。 As you can see the callback returns void and is passed in one parameter , IAsyncResult interface , which contains the status of the asynchronous receive operation. The IAsyncResult interface has several properties. The first parameter AsyncState is an object which is same as the last parameter that you passed to BeginReceive(). The second property is AsyncWaitHandle which we will discuss in a moment. The third property indicates whether the receive was really asynchronous or it finished synchronously. The important thing to follow here is that it not necessary for an asynchronous function to always finish asynchronously it can plete immediately if the data is already present. Next parameter is IsComplete which indicates whether the operation has pleted or not. If you look at the signature of the BeginReceive again you will note that the function also returns IAsyncResult. This is interesting. Just now I said that I will talk about the second peoperty of the IAsyncResult in a moment. Now is that moment. The second parameter is called AsyncWaitHandle. The AsyncWaitHandle is of type WaitHandle, a class defined in the namespace. WaitHandle class encapsulates a Handle (which is a pointer to int or handle ) and provides a way to wait for that handle to bee signaled. The class has several static methods like WaitOne ( which is similar to WaitForSingleObject ) WaitAll ( similar to WaitForMultipleObjects with waitAll true ) , WaitAny etc. Also there are overloads of these functions available with timeouts. Coming back to our discussion of IAsyncResult interface, the handle in AsyncWaitHandle (WaitHandle) is signalled when the receive operation pletes. So if we wait on that handle infinitely we will be able to know when the receive pleted. This means if we pass that WaitHandle to a different thread, the different thread can wait on that handle and can notify us of the fact that the data has arrived and so that we can read the data. So you must be wondering if we use this mechanism why would we us。
阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。 用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。