コード例 #1
0
        /// <summary>
        /// Closes and clears the listening socket and all connected sockets, without causing exceptions.
        /// </summary>
        private void ResetListeningSocket()
        {
            // Close all child sockets
            foreach (KeyValuePair<SimpleServerChildTcpSocket, ChildSocketState> socket in Server.ChildSockets)
                socket.Key.ShutdownAsync();

            Server.ChildSockets.Clear();

            // Close the listening socket
            ListeningSocket.Close();
            ListeningSocket = null;
        }
コード例 #2
0
        public void Start(int port)
        {
            try
            {
                // Define the socket, bind to the port, and start accepting connections
                ListeningSocket = new SimpleServerTcpSocket();
                ListeningSocket.ConnectionArrived += ListeningSocket_ConnectionArrived;
                ListeningSocket.Listen(port);

                ConsoleService.Write("Listening on port " + port);
            }
            catch (Exception ex)
            {
                ResetListeningSocket();
                ConsoleService.Write("Error creating listening socket on port " + port + ": [" + ex.GetType().Name + "] " + ex.Message);
            }

            RefreshDisplay();
        }