Пример #1
0
        private void buttonStart_Click(object sender, EventArgs e)
        {
            // Read the port number
            int port;

            if (!int.TryParse(textBoxPort.Text, out port))
            {
                MessageBox.Show("Invalid port number: " + textBoxPort.Text);
                textBoxPort.Focus();
                return;
            }

            try
            {
                // Define the socket, bind to the port, and start accepting connections
                ListeningSocket = new SimpleServerTcpSocket();
                ListeningSocket.ConnectionArrived += ListeningSocket_ConnectionArrived;
                ListeningSocket.Listen(port);

                textBoxLog.AppendText("Listening on port " + port.ToString() + Environment.NewLine);
            }
            catch (Exception ex)
            {
                ResetListeningSocket();
                textBoxLog.AppendText("Error creating listening socket on port " + port.ToString() + ": [" + ex.GetType().Name + "] " + ex.Message + Environment.NewLine);
            }
            RefreshDisplay();
        }
Пример #2
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;
        }
Пример #3
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();
        }