コード例 #1
0
ファイル: Server.cs プロジェクト: umar-qureshi2/UG-Courses
        public static void Start()
        {
            try
            {

                IPHostEntry ipEntry = Dns.GetHostEntry(Dns.GetHostName());
                IPAddress[] addr = ipEntry.AddressList;

                //addr[1] will contain IPAdress of local machine
                TcpListener Server = new TcpListener(IPAddress.Parse("127.0.0.9"), 9876);
                Server.Start();
                TcpClient client = Server.AcceptTcpClient();

                if (Player.State == PlayerState.Available)
                {
                    DialogResult dr = MessageBox.Show("A new challenger\nDo you want to play", "Game Request", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                    if (dr == DialogResult.Yes)
                    {

                        using (Client Client = new Client())
                        {
                            //Read Opponent Name from Client.
                            string opname = Client.Read(client.GetStream());
                            opname = opname.Substring(0, opname.IndexOf('\0'));
                            //Writing Yes to the client.
                            Client.Write(client.GetStream(), "yes");
                            Player.IsServer = true;
                            //Starting Game
                            Application.Run(new Game(client.GetStream(), opname));

                        }
                    }
                    else
                    {
                        using (Client Client = new Client())
                        {
                            //If Player doesnot accept the request.
                            Client.Write(client.GetStream(), "&|&");

                        }
                    }
                }
                else
                {
                    using (Client Client = new Client())
                    {
                        //If Player is not available.
                        Client.Write(client.GetStream(), "&|&");

                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }
        }
コード例 #2
0
ファイル: Game.cs プロジェクト: umar-qureshi2/UG-Courses
        /// <summary>
        /// Reading Indicies from Stream
        /// When Opponent Player is at turn.
        /// </summary>
        private void Read_Stream()
        {
            using (Client c = new Client())
            {
                try
                {
                    while (true)
                    {
                        string data = c.Read(Network_Stream);
                        //lock the shared variables.
                        lock (this)
                        {
                            xindx = Convert.ToInt16(data.Substring(0, data.IndexOf('&')));
                            yindx = Convert.ToInt16(data.Substring(data.IndexOf('&') + 1));
                            //Drawing line without giving point.
                            DrawLine(new Point(0, 0));
                        }

                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

            }
        }
コード例 #3
0
ファイル: LanLobby.cs プロジェクト: umar-qureshi2/UG-Courses
        private void strt_btn_Click(object sender, EventArgs e)
        {
            if (AvailablePlayers.SelectedIndex != -1)
            {
                using (Client client = new Client())
                {
                    NetworkStream ns = client.Connect(ips[AvailablePlayers.SelectedIndex]);
                    client.Write(ns, Player.Name);
                    string data = client.Read(ns);
                    data = data.Substring(0, data.IndexOf('\0'));
                    if (data == "&|&")
                    {
                        MessageBox.Show("Sorry this player is busy", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        ns.Close();
                    }
                    else
                    {
                        Player.IsServer = false;
                        //Starting Game
                        Game game = new Game(ns, names[AvailablePlayers.SelectedIndex]);
                        game.Show();
                        Close();
                    }

                }

            }
            else
                MessageBox.Show("Please Select Any Player", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }