Exemplo n.º 1
0
        void Connect()
        {
            IPEndPoint iep = new IPEndPoint(IPAddress.Parse(IP), Port);

            Client.Connect(iep);
            if (Client.Connected == true)
            {
                // namePlayer = tb_PlayerName.Text.ToString();
                //byte[] sentByte = Encoding.ASCII.GetBytes(namePlayer);
                //client.Send(sentByte);
                if (openedForm == null)
                {
                    openedForm = new room();
                    openedForm.Show();
                }
            }
            //Client.Close();
        }
Exemplo n.º 2
0
        void listenFromServer()
        {
            while (true)
            {
                //try
                //{

                byte[] byteReceive = new byte[1024];
                Client.Receive(byteReceive);
                if (byteReceive != null)
                {
                    object   obj       = DeserializeData(byteReceive);
                    string[] rcvString = (string[])obj;
                    //----------------------------------------------
                    string str = rcvString[0];
                    if (str[0] == 'P')
                    {
                        string[] arrstr = str.Split(':');
                        //ip_port_server = ip_port_server.Replace("P:", string.Empty);
                        string ip_port_server = arrstr[1] + ":" + arrstr[2];
                        string name           = arrstr[3];
                        string acceptString   = "";
                        //MessageBox.Show("accept");
                        //Form acc = new Accept(ip_port_server);
                        //acc.Show();
                        DialogResult dialogResult = MessageBox.Show("Người chơi " + ip_port_server + " muốn chơi cờ với bạn. Bạn có đồng ý không ?", "Thông báo", MessageBoxButtons.YesNo);
                        if (dialogResult == DialogResult.Yes)
                        {
                            acceptString    = "Y:" + ip_port_server + ":" + name;
                            ipAndPort       = "C:" + ip_port_server;
                            nameOtherPlayer = name;
                            byte[] byteSend = Encoding.ASCII.GetBytes(acceptString);
                            Client.Send(byteSend);
                            Client.Close();
                            Form frm = new chessBoard();
                            frm.ShowDialog();
                        }
                        else if (dialogResult == DialogResult.No)
                        {
                            acceptString = "N:" + ip_port_server;
                            byte[] byteSend = Encoding.ASCII.GetBytes(acceptString);
                            Client.Send(byteSend);
                        }
                        continue;
                        //Mở form bàn cờ, kết nối đến người chơi đóng vai trò server bằng ip, port trong rcvString
                        //  return;
                    }
                    if (str[0] == 'Y')
                    {
                        //client.Client.Disconnect(true);
                        //string ip_port_remove = str.Replace("Y:", string.Empty);
                        string[] arrstr = str.Split(':');
                        //ipAndPort = str.Replace("Y", "S");
                        ipAndPort       = "S:" + arrstr[1] + ":" + arrstr[2];
                        nameOtherPlayer = arrstr[3];
                        client.Client.Close();
                        //MessageBox.Show("Nguoi choi da dong y ghep doi");
                        if (openedForm != null)
                        {
                            openedForm.Close();
                            openedForm = null;
                        }

                        Form frm = new chessBoard();
                        frm.ShowDialog();

                        break;
                    }
                    if (str[0] == 'N')
                    {
                        MessageBox.Show("Nguoi choi KHONG dong y ghep doi");
                        break;
                    }
                    //----------------------------------------------

                    listUser.Clear();
                    foreach (string s in rcvString)
                    {
                        listUser.Add(s);
                    }
                }

                //}
                //catch
                //{

                //}
            }
        }