Exemple #1
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();
        }