Question

In Stefen Toub's code Awaiting Socket Operations : it doesn't mention about the usage of its async methods. I mean how the Server program will be calling the async methods using await? Ther isn't any main() also. Please elaborate over this. I tried creating a main() and calling the SocketExtensions.ReceiveAsync(sock,awaitable);

public static void Main(string[] str)
{
    Console.WriteLine("\n\n<<<<<<<<<<<  Server is starting...  >>>>>>>>>>>>>>>>>");

    IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 10100);
    Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    var args = new SocketAsyncEventArgs();
    args.SetBuffer(new byte[0x100000], 0, 0x100000);
    var awaitable = new SocketAwaitable(args);
    while(true)
        SocketExtensions.ReceiveAsync(sock,awaitable);
}

But it didn't work for me.

Was it helpful?

Solution

Stephen Toub's blog post is not a sockets tutorial; it's showing how to use custom awaitables to support extremely high-performance socket applications.

I recommend that you start with something like WebAPI or WCF and only use bare sockets if you have to. If you absolutely do need to use sockets, then I recommend that you first use TaskFactory.FromAsync to wrap the Begin/End methods; this is slightly less efficient but an easier programming model. If performance tests indicate that your server isn't fast enough at this point, then you should use the custom awaiter approach from Stephen Toub's blog post.

The reason your server isn't working is because you're not listening nor accepting. I recommend you use WebAPI or WCF instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top