Пример #1
0
        public void Networking()
        {
            try
            {
                p = new OtherPlayer(MyPlayer.worldX, MyPlayer.worldY, MyPlayer.moveX, MyPlayer.moveY, MyPlayer.isAttacking, MyPlayer.worldX2, MyPlayer.worldY2,
                                    Luncher.StartGame.username, animationfilepath, MyPlayer.experience, MyPlayer.accountType);

                MemoryStream    ms = new MemoryStream();
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(ms, p);
                byte[] data = ms.GetBuffer();
                UdpClient.Send(data, data.Length, iPEndPoint);
                Console.WriteLine("sent");

                MemoryStream    ms1          = new MemoryStream();
                BinaryFormatter bf1          = new BinaryFormatter();
                byte[]          dataRecevied = UdpClient.Receive(ref iPEndPoint);
                ms1 = new MemoryStream(dataRecevied);
                OtherPlayer.players = (Dictionary <string, OtherPlayer>)bf1.Deserialize(ms1);
                Console.WriteLine("recieved");
            }
            catch (Exception)
            {
                MessageBox.Show("error");
                Networking();
            }
        }
Пример #2
0
        private void connectLbl_Click(object sender, EventArgs e)
        {
            client        = new TcpClient(serveripTxtBox.Text, 8080);
            streamReader  = new StreamReader(client.GetStream());
            streamWriter  = new StreamWriter(client.GetStream());
            networkStream = client.GetStream();

            streamWriter.WriteLine(1); streamWriter.Flush();
            streamWriter.WriteLine(usernameTxtBox.Text); streamWriter.Flush();
            streamWriter.WriteLine(passwordTxtBox.Text); streamWriter.Flush();

            response = streamReader.ReadLine();

            if (response.Equals("true"))
            {
                username = usernameTxtBox.Text;

                me = (OtherPlayer)bfTCP.Deserialize(networkStream);
                MyPlayer.worldX      = me.worldX;
                MyPlayer.worldY      = me.worldY;
                MyPlayer.moveX       = me.moveX;
                MyPlayer.moveY       = me.moveY;
                MyPlayer.experience  = me.experience;
                MyPlayer.accountType = me.accountType;

                Game = new GameForm();
                Game.Show();
                Luncher.StartGame.Hide();
            }
            else
            {
                errorLbl.Text = response;
                streamReader.Close();
                streamWriter.Close();
                networkStream.Close();
                client.Close();
            }
        }