コード例 #1
0
ファイル: ServerGUI.cs プロジェクト: kr094/csharp
        public void serverMode()
        {
            ChatServer server = null;
            try
            {
                server = new ChatServer();
                Console.WriteLine("SyncChat Server started on " + NetworkInfo.IpString + ":" + NetworkInfo.portString);
                Console.WriteLine("Awaiting Connection...");
                if (server.startServer())
                    Console.WriteLine("Client Connected!");
                this.fancyOutput();

                server.sendMessage("Welcome to the server");

                while (true)
                {
                    if (Console.KeyAvailable)
                    {
                        if (Console.ReadKey(true).Key == ConsoleKey.I)
                        {
                            Console.Write(">> ");
                            this.data = Console.ReadLine();
                            server.sendMessage(this.data);
                            if (this.data.ToLower().Equals("quit"))
                            {
                                server.close();
                                Console.WriteLine("Server Closed!");
                                break;
                            }
                        }
                    }
                    else if (server.messageAvailable())
                    {
                        this.data = server.getData();
                        if (this.data.ToLower().Equals("quit"))
                        {
                            server.close();
                            Console.WriteLine("Server Closed!");
                            break;
                        }
                        else
                            Console.WriteLine("Client: {0}", this.data);
                    }
                }
            }
            catch (System.Net.Sockets.SocketException)
            {
                Console.WriteLine("A Server is already running!");
            }
            catch (System.IO.IOException)
            {
                Console.WriteLine("All Clients have left! - Exiting");
                server.close();
            }
        }