Example #1
0
        public void ConnectToServer()
        {
            IPAddress  ip        = IPAddress.Parse("172.20.10.4");
            IPEndPoint ipe       = new IPEndPoint(ip, 1111);
            TcpClient  tcpClient = new TcpClient();

            try
            {
                tcpClient.Connect(ipe);
                if (tcpClient.Connected)
                {
                    MessageBox.Show("連線成功");
                    CommunicationBase cb = new CommunicationBase();
                    cb.SendData("測試訊息", tcpClient);
                    MessageBox.Show(cb.ReceiveData(tcpClient));
                }
                else
                {
                    Console.WriteLine("連線失敗");
                }
            }
            catch (Exception e)
            {
                tcpClient.Close();
                Console.WriteLine(e.Message);
            }
        }
Example #2
0
 private void btn1_Click(object sender, EventArgs e)
 {
     if (_tcpClient.Connected)
     {
         Random random = new Random();
         int    attHp  = random.Next(100);
         if (attHp > 0)
         {
             _cb.SendData("1" + attHp, _tcpClient);
             tbContent.Text += "使用「普通攻擊」傷害了 " + attHp + "Hp \r\n";
             string[] datas  = _cb.ReceiveData(_tcpClient).Split(',');
             int      loseHp = int.Parse(datas[1]);
             tbContent.Text += "遭受「反擊」受到傷害 " + loseHp + "Hp \r\n";
             UserHp         -= loseHp;
             labHP.Text      = UserHp.ToString();
             labMHP.Text     = datas[0].ToString();
         }
         else
         {
             MessageBox.Show("無效攻擊!!");
         }
     }
 }
Example #3
0
        private void ListenClient()
        {
            TcpClient client = tcpClient;
            Thread    thread = ClThread;

            while (true)
            {
                try
                {
                    CommunicationBase cb   = new CommunicationBase();
                    string            data = cb.ReceiveData(client);
                    string            cmd  = data.Substring(0, 1);
                    int     attHp          = 0;
                    Monster monster        = (Monster)HT[client];
                    switch (cmd)
                    {
                    case "1":
                        attHp = int.Parse(data.Substring(1));
                        monster.BeAttacked(attHp);
                        break;

                    case "9":
                        HT.Remove(client);
                        thread.Abort();
                        break;
                    }
                    //MessageBox.Show(monster.HP.ToString());
                    string msg = monster.HP.ToString() + "," + new Random().Next(50).ToString();
                    cb.SendData(msg, client);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    throw;
                }
            }
        }