kerryjiang / WebSocket4Net

A popular .NET WebSocket Client
752 stars 272 forks source link

An unhandled exception of type 'System.Net.Sockets.SocketException' #118

Open NKumarPraveen opened 6 years ago

NKumarPraveen commented 6 years ago

I have retry connection running in separate thread when connection lost. following exception is thrown and application is crashed.

System.Net.Sockets.SocketException was unhandled Message: An unhandled exception of type 'System.Net.Sockets.SocketException' occurred in System.dll Additional information: Only one usage of each socket address (protocol/network address/port) is normally permitted

`

        Task.Factory.StartNew(() =>
        {
            try
            {
                while (!token.IsCancellationRequested)
                {
                    if (_socket != null && _socket.State == WebSocketState.Closed)
                    {
                        _socket.Open();
                        _isWait = 1;
                        this.Wait();
                    }
                    token.WaitHandle.WaitOne(10000);
                }
            }
            catch (Exception ex)
            {
                Log.Write(LogType.Exception, MethodBase.GetCurrentMethod(), ex);
            }
        }, cancellationTokenSource.Token);

` Operating system : windows 7 .Net farmework: 4.5 WebSocket4Net.dll version: 0.15.0.0 SuperSocket.ClientEngine.dll version: 0.8.0.8

Thank you

kerryjiang commented 6 years ago

Could you show me all the code?

And I suggest you to use async/await pattern which can handle asynchronous easily:

await client.OpenAsync();