Client Socket stuck in 'connecting' state and not receiving data

Feb 11, 2015 at 12:22 PM
Hi,
I have a problem with the websocket client. I am using SuperWebSocket framework for the implementation of the server.
Here is the code of the main method of the server process:
static void Main(string[] args)
        {
            var server = new PushServer();
            ServerConfig rc = new ServerConfig();
            rc.Mode = SocketMode.Tcp;
            rc.Ip = "3.249.53.38";
            rc.Port = 810;
            rc.TextEncoding = "utf-8";            
            server.Setup(rc);
            var result = server.Start();
            
            Console.WriteLine("Start result: {0}!", result);

            if (!result)
            {
                Console.WriteLine("Failed to start!");
                Console.ReadKey();
                return;
            }

            Console.WriteLine("Press key 'q' to stop it!");

            while (Console.ReadKey().KeyChar != 'q')
            {
                Console.WriteLine();
            }

            Console.WriteLine();
            server.Stop();
            Console.WriteLine("The server was stopped!");
            Console.ReadKey();
        }
Here is the server
public class PushServer : AppServer
    {
        private Timer _pushTimer;

        private const int Interval = 5*1000;

        public override bool Start()
        {
            _pushTimer = new Timer(OnPushTimerCallback);
            _pushTimer.Change(Interval, Interval);
            return base.Start();
        }

        private void OnPushTimerCallback(object state)
        {
            try
            {
                _pushTimer.Change(Timeout.Infinite, Timeout.Infinite);
                PushToAllClients();
            }
            catch (Exception e)
            {
                Logger.Error(e);
            }
            finally
            {
                _pushTimer.Change(Interval, Interval);
            }
        }

        private void PushToAllClients()
        {
            var now = DateTime.Now.ToString();

            foreach (var session in this.GetAllSessions())
            {
                session.Send(now);
            }
        }

        protected override void OnStopped()
        {
            if (_pushTimer != null)
            {
                _pushTimer.Change(Timeout.Infinite, Timeout.Infinite);
                _pushTimer.Dispose();
                _pushTimer = null;
            }

            base.OnStopped();
        }

        protected override void OnNewSessionConnected(AppSession session)
        {
            var thisSession =
                session as AppSession<AppSession, SuperSocket.SocketBase.Protocol.StringRequestInfo>;
            Console.WriteLine("Client connected {0}:{1}", thisSession.LocalEndPoint.Address, thisSession.LocalEndPoint.Port);
            base.OnNewSessionConnected(session);
        }
    }
Here is the client
class Program
    {
        private static WebSocket websocket;
        private static bool connected;
        static void Main(string[] args)
        {
            var task = Task.Factory.StartNew(StartWebSocket, TaskCreationOptions.LongRunning);
            Task.WaitAll(new[] {task});
            while (!connected)
            {
                Console.WriteLine(websocket.State);
                Thread.Sleep(5000);
            }
        }

        private static void StartWebSocket()
        {
            websocket = new WebSocket("ws://3.249.53.38:810/");
            websocket.AllowUnstrustedCertificate = true;
            websocket.EnableAutoSendPing = false;
            websocket.Opened += websocket_Opened;
            websocket.Error += websocket_Error;
            websocket.Closed += websocket_Closed;
            websocket.MessageReceived += websocket_MessageReceived;
            websocket.DataReceived += websocket_MessageReceived;
            websocket.Open();
        }

        private static void websocket_MessageReceived(object sender, EventArgs e)
        {
            Console.WriteLine("Received");
        }

        private static void websocket_Error(object sender, ErrorEventArgs e)
        {
            Console.WriteLine("Error " + e.Exception.Message);
        }

        private static void websocket_Closed(object sender, EventArgs e)
        {
            Console.WriteLine("Closed");
        }

        private static void websocket_Opened(object sender, EventArgs e)
        {
            Console.WriteLine("Opened");
            connected = true;
        }
    }
I can see the connection being correctly established at server side but at client side the state remains "Connecting".

The server tries also to send messages to the client but the message gets never delivered. The funny thing is that if I stop the server, the event websocket_Closed is immediately raised at the client side.

Some more info: if I connect to the server via a telnet client the message from the server is correctly received.

Any help would be appreciated.

Thanks