0

I'm writing simple socket server with C#. The idea is to do it as simple as possible since the communication won't be heavy. I've used some of the TAP/APM patterns on socket async calls so the code now looks like this:

    public async Task StartListening()
    {
        try
        {
            var endpoint = new IPEndPoint(IPAddress.Loopback, Port);

            using (Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
            {
                Socket.Bind(endpoint);
                Socket.Listen(Limit);
                while (!Shutdown)
                {
                    // await till we get the connection - let the main thread/caller continue (I expect this loop to continue in separate thread after await)
                    var socket = await Socket.AcceptAsync().ConfigureAwait(false);

                    var state= new ClientStateObject(socket, id);
                    // do not await for receive - continue to listen for connections
                    Receive(socket, state);
                }
            }
        }
        catch (Exception ex)
        {
            // handle errors via callbacks
        }
    }

    private async void Receive(Socket socket, ClientStateObject state)
    {
        try
        {
            while (!Shutdown)
            {
                var bytes = await socket.ReceiveAsync(state).ConfigureAwait(false);

                var readResult = state.Read(bytes);
                if (readResult == CloseConn)
                {
                    // close connection

                    return;
                }

                if (readResult == Completed)
                {
                    // process the message

                    state.Reset();
                }
            }
        }
        catch (Exception ex)
        {
            // handle errors via callbacks
        }
    }

This code seems to run fine in development builds, but sometimes behaves strange in release mode. I assume this might be related to race conditions or something that is related to threading and async/await. Does anyone see what might be the problem with the above code?

Just to be clear AcceptAsync/ReceiveAsync are wrappers around socket methods to return tasks.

void
  • 333
  • 4
  • 12
  • 1
    So what exactly is the issue that is occurring? Error message? Hangs? You've said "it behaves strange", describe how exactly. – Equalsk Oct 21 '16 at 15:24
  • Initially I had an issue that I've described here: http://stackoverflow.com/questions/39588842/invalid-program-exception-in-release-build-net-4-6-1. Now it sometimes hangs at call to Receive - I'm not able to catch an exception or anything, can't confirm what line exactly fails (even though I have intensive logging enabled all around). So just want to check if the approach I have doesn't have fundamental flaw. – void Oct 21 '16 at 20:16
  • `AcceptAsync/ReceiveAsync are wrappers` around blocking code? What framework do you use? (console, asp.net, etc.) – shay__ Oct 21 '16 at 22:31
  • AcceptAsync/ReceiveAsync are wrappers around async socket methods BeginAccept/EndAccept and BeginReceive/EndReceive with Task.Factory.FromAsync (APM pattern). It's windows forms context. – void Oct 22 '16 at 06:56

0 Answers0