Ejemplo n.º 1
0
        public void StopServer()
        {
        
            if (_connectedSockets != null)
            {
                    
                try
                {
                    ClientConnected.Set();
                    lock (_connectedSocketsSyncHandle)
                    {
                        foreach (var req in _connectedSockets)
                        {
                            req.TcpClient.Client.Close();
                        }
                    }
                }
                catch (SocketException ex)
                {
                    //During one socket disconnected we can faced exception
                    Logger.LogExceptionToFile(ex, "Server");
                }
            }

            if (_myListener?.Server != null)
            {
                try
                {
                    _myListener.Server.Close(0);
                }
                catch (Exception ex)
                {
                    Logger.LogExceptionToFile(ex, "Server");
                }
                finally
                {
                    _myListener.Stop();
                }
                _myListener = null;
            }
            WebSocketServer?.Close();
            WebSocketServer = null;
        }
Ejemplo n.º 2
0
        public string StartServer()
        {
            bool ssl = MainForm.Conf.SSLEnabled;
            if (ssl && !string.IsNullOrEmpty(MainForm.Conf.SSLCertificate))
                X509.LoadCertificate(MainForm.Conf.SSLCertificate);

            string message = "";
            try
            {
                _myListener = new TcpListener(ListenerAddress, MainForm.Conf.LANPort) { ExclusiveAddressUse = false };
                if (MainForm.Conf.IPMode=="IPv6")
                {
                     _myListener.AllowNatTraversal(true);
                }
                _myListener.Start(200);

                WebSocketServer?.Close();

                WebSocketServer = new WebSocketServer(this);
                ServerStartupFailed = false;
            }
            catch (Exception e)
            {
                Logger.LogExceptionToFile(e,"Server");
                StopServer();
                message = "Could not start local iSpy server - please select a different LAN port in settings. The port specified is in use. See the log file for more information.";
                ServerStartupFailed = true;
            }
            if (message != "")
            {
                Logger.LogMessageToFile(message, "Server");
                return message;
            }
            try 
            {
                //start the thread which calls the method 'StartListen'
                if (Running)
                {
                    while (_th.ThreadState == ThreadState.AbortRequested)
                    {
                        Application.DoEvents();
                    }
                }
                
            }
            catch (Exception e)
            {
                message = e.Message;
                Logger.LogExceptionToFile(e, "Server");
            }

            lock (_threadLock)
            {
                _th = new Thread(StartListen) {IsBackground = true};
                _th.Start();
            }
            return message;
        }