Ejemplo n.º 1
0
 static void Main(string[] args)
 {
     OnStartup();
     try
     {
         Task.Factory.StartNew(() => { server.Listen(); });
     }
     catch (Exception ex)
     {
         server.Disconnect();
         Console.WriteLine(ex.Message);
     }
     Console.ReadLine();
 }
Ejemplo n.º 2
0
        public Form1()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;

            listRoom   = new Dictionary <string, Room>();
            listPlayer = new Dictionary <string, Player>();

            LoadQuestion();

            //IP: Địa chỉ của server
            IP     = new IPEndPoint(IPAddress.Any, 9999);
            server = new TCPServer(IP);

            Thread listen = new Thread(() =>
            {
                while (true)
                {
                    Socket client = server.Listen();
                    if (client != null)
                    {
                        Thread receive = new Thread(() => {
                            while (true)
                            {
                                String buff = server.Receive(client);

                                String[] str = buff.Split('$');
                                foreach (var item in str)
                                {
                                    if (item != String.Empty)
                                    {
                                        Message mes = new Message(item);
                                        AddMessage(mes.Sender + ": " + mes.Opcode + " " + mes.Payload);
                                        ProcessData(client, mes);
                                    }
                                }
                            }
                        });
                        receive.IsBackground = true;
                        receive.Start();
                    }
                }
            });

            listen.IsBackground = true;
            listen.Start();
        }
Ejemplo n.º 3
0
        private static void StartTCPServer(string ipAddress, int port)
        {
            TCPServer tcpServer = new TCPServer(new ConsoleMessageProcessor(), ipAddress, port);

            tcpServer.Listen();
        }