1

I am having a issue with a socket listener. It will work for a few mins thenjust stops listening. I have tried to enable and disable Compile in .Net option as suggested in other forums with no luck. Any ideas why it would just stop? I need it to be listening at all times for input from another client.

private async void DataListener_OpenListenPorts()
    {
        StreamSocketListener listener = new StreamSocketListener();
        listener.ConnectionReceived += SocketListener_ConnectionReceived;
        listener.Control.KeepAlive = true;

        try
        {               
            string ServerName = _rxPort.ToString();
            await listener.BindServiceNameAsync(ServerName);
        }
        catch (Exception exception)
        {
            if (Windows.Networking.Sockets.SocketError.GetStatus(exception.HResult) == SocketErrorStatus.Unknown)
            {
                throw;
            }
          //  Debug.WriteLine("Start listening failed with error: " + exception.Message);
        }

    }




    private async void SocketListener_ConnectionReceived(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args)
    {

            Stream inStream = args.Socket.InputStream.AsStreamForRead();
            StreamReader reader = new StreamReader(inStream);
            string request = await reader.ReadLineAsync();

            if (request != null)
            {
                incoming = request;
                await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    SynthPlayback(incoming);
                });
            }


    }

In debug mode the socket stays open. This problem only seems to happen in deployment

1

I also stucked with this issue and I've found a possible solution. We should set Control.QualityOfService property to LowLatency after we create the StreamSocketListener instance and before BindServiceNameAsync method is called. You may declare the listener outside of the method. This is how I start this listener:

StreamSocketListener listener = new StreamSocketListener();
var currentSetting = listener.Control.QualityOfService;
listener.Control.QualityOfService = SocketQualityOfService.LowLatency;
listener.ConnectionReceived += HandleRequest;
await listener.BindServiceNameAsync("8081");

You can find the documentation here. Please check Remark section for details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.