Web based solutions, tutorials, examples and help. 

  • Easy-to-use async socket? Not likely...

 #213  by SilverX
 Sun Aug 28, 2011 2:28 pm
So I got asked a question the other day about having a single universal handler for async sockets, I got lost in a brainstorm for a while and this is what I came up with.. figured I would share project...

[UPDATE] Uploaded a more recent version of the project, providing better error handling and a few bug fixes

A preview of the AsyncTest console app...

Code: Select allnamespace AsyncTests
{
    class Program
    {
        static AsyncTcpSocket TestListener;
        static AsyncTcpSocket TestClient;

        static AsyncUdpSocket TestSender;
        static AsyncUdpSocket TestReceiver;

        static void Main(string[] args) {

            Console.WriteLine("Testing AsyncSocket...");

            Random r = new Random();

            TestListener = new AsyncTcpSocket(new AsyncHandler());
            TestListener.Listen(r.Next(1024, 60000));

            TestClient = new AsyncTcpSocket(new AsyncHandler());
            TestClient.Connect("127.0.0.1", TestListener.LocalEndPoint.Port);

            //during this test the sender is only sending 64 bytes
            //so each datagram received will only be 64 bytes and not 1024
            TestReceiver = new AsyncUdpSocket(new AsyncHandler(), new IPEndPoint(IPAddress.Any, TestListener.LocalEndPoint.Port));
            TestReceiver.Receive(1024);

            TestSender = new AsyncUdpSocket(new AsyncHandler(), new IPEndPoint(IPAddress.Any, 0));

            for (int i = 0; i < 25; i++)
                TestSender.Send(new byte[64], new IPEndPoint(IPAddress.Parse("127.0.0.1"), TestListener.LocalEndPoint.Port));

            Console.WriteLine("Waiting, press any key to quit...");
            Console.ReadKey();
        }


        private class AsyncHandler : IAsyncSocketEventListener
        {
            public void OnAsyncTcpEvent(AsyncTcpSocketEventArgs e) {
                switch (e.Operation) {
                    case AsyncOperation.Accept:
                        if (e.Exception == null) {
                            Console.WriteLine("TCP - Accepted");
                            e.Accept.Listener = new AsyncHandler();

                            for (int i = 0; i < 2; i++)
                                e.Accept.Send(new byte[8192]);

                            e.Accept.Dispose();
                        }
                        break;
                    case AsyncOperation.Connect:
                        if (e.Exception == null) {

                            Console.WriteLine("TCP - Connected");
                            e.Socket.Receive(1024);
                        }
                        else Console.WriteLine("TCP - Connect failed");

                        break;
                    case AsyncOperation.Receive:
                        Console.WriteLine("TCP - Receive: {0}", e.Buffer.Length);
                        e.Socket.Receive(1024);
                        break;
                    case AsyncOperation.Disconnect:
                        Console.WriteLine("TCP - Disconnected");
                        break;
                }
            }

            public void OnAsyncUdpEvent(AsyncUdpSocketEventArgs e) {
                switch (e.Operation) {
                    case AsyncOperation.Receive:
                        Console.WriteLine("UDP - Receive: {0}", e.Buffer.Length);
                        e.Socket.Receive(64);
                        break;
                }
            }
        }
    }
}


And the output...
Image
Attachments
AsyncSocketLib Source
(26.28 KiB) Downloaded 247 times
 #757  by Meenk
 Thu May 24, 2012 6:42 pm
A powerful share, I just given this onto a colleague who was doing just a little analysis on this. And he the truth is purchased me breakfast as a result of I discovered it for him.. smile. So let me reword that: Thnx for the treat! But yeah Thnkx for spending the time to discuss this, I really feel strongly about it and love reading more on this topic. If attainable, as you turn into experience, would you thoughts updating your weblog with extra particulars? It is extremely helpful for me. Large thumb up for this blog put up!