Represents a two way connection from the server to a single client
示例#1
0
文件: Server.cs 项目: jonocodes/mybox
        /// <summary>
        /// Constructor. Maintains a loop for listening for incoming clients.
        /// </summary>
        /// <param name="configFile"></param>
        public Server(String configFile)
        {
            Console.WriteLine("Starting server");
            Console.WriteLine("Loading config file " + configFile);

            serverDB = LoadConfig(configFile);

            TcpListener tcpListener = new TcpListener(IPAddress.Any, Port);

            try {
                tcpListener.Start();
            }
            catch (SocketException e) {
                Console.WriteLine("Unable to start listener on port " + Port + e.Message);
                throw;
            }

            while (true)
            {
                Console.WriteLine(" waiting for client to connect...");
                Socket listenerSocket = tcpListener.AcceptSocket();

                Console.WriteLine("Client connected with handle " + listenerSocket.Handle);
                ServerClientConnection client = new ServerClientConnection(this, listenerSocket);
                clients.Add(listenerSocket.Handle, client);
            }
        }
示例#2
0
文件: Server.cs 项目: jonocodes/mybox
        /// <summary>
        /// Disconnect a ServerClientConnection from the server
        /// </summary>
        /// <param name="handle"></param>
        public void RemoveConnection(IntPtr handle)
        {
            if (clients.ContainsKey(handle))
            {
                ServerClientConnection toTerminate = clients[handle];

                if (toTerminate.User != null)
                {
                    Console.WriteLine("Removing client " + handle + " " + toTerminate.User);
                    RemoveFromMultiMap(toTerminate.User.id, handle);
                }
                else
                {
                    Console.WriteLine("Removing accountless client " + handle);
                }

                /*
                 * try {
                 * toTerminate.StopListener();
                 * }
                 * catch (IOException ioe) {
                 * Console.WriteLine("Error closing thread: " + ioe);
                 * }
                 */
                // remove from list
                clients.Remove(handle);
            }
        }