See the MSDN example here . This example shows how to receive data asynchronously. You can also use the Socket BeginSend / EndSend methods to send data asynchronously.
It should be noted that the callback delegate is executed in the context of the ThreadPool thread. This is important if the data received inside the callback must be shared by another thread, for example, the main thread of the user interface, which displays the data in the form of Windows. If so, you will need to synchronize data access using the lock keyword, for example.
As you noticed, with non-blocking sockets and a while loop, the processor is tied to 100%. The asynchronous model will only call the callback delegate when there is data to send or receive.
Matt davis
source share