Пример #1
0
        private void OnReceive(IAsyncResult ar)
        {
            try
            {
                clientSocket.EndReceive(ar);

                //Convert the bytes received into an object of type Data
                Data msgReceived = new Data(byteData);

                //Accordingly process the message received
                switch (msgReceived.cmdCommand)
                {
                    case Command.Login:
                        playercount = msgReceived.playerCount;
                        break;

                    case Command.Create:

                        playerID = msgReceived.playerID;
                        break;

                    case Command.Logout:
                        break;

                    case Command.Move:

                        break;
                    case Command.Message:
                        break;

                    case Command.List:

                        break;
                }
                byteData = new byte[1024];

                //Start listening to receive more data from the user
                clientSocket.BeginReceiveFrom(byteData, 0, byteData.Length, SocketFlags.None, ref epServer,
                                           new AsyncCallback(OnReceive), null);

            }
            catch (ObjectDisposedException)
            { }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
Пример #2
0
        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
             strName = "nicktest";
             ship ship1 = new ship();
             ship1.shipx = 0;
             ship1.shipy = 0;
             ship1.shipObj = Content.Load<Texture2D>("test");
             shipList.Add(ship1);
             ship ship2 = new ship();
             ship2.shipx = 300;
             ship2.shipy = 300;
             ship2.shipObj = Content.Load<Texture2D>("test");
             shipList.Add(ship2);

                //Using UDP sockets
                clientSocket = new Socket(AddressFamily.InterNetwork,
                    SocketType.Dgram, ProtocolType.Udp);

                IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
                IPAddress ipAddress = ipHostInfo.AddressList[0];
                Console.WriteLine(ipAddress);

                //Server is listening on port 1000
                IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 11000);

                epServer = (EndPoint)ipEndPoint;

                Data msgToSend = new Data();
                msgToSend.cmdCommand = Command.Login;
                msgToSend.strMessage = null;
                msgToSend.strName = strName;

                byte[] byteData = msgToSend.ToByte();

                //Login to the server
                clientSocket.BeginSendTo(byteData, 0, byteData.Length,
                    SocketFlags.None, epServer, new AsyncCallback(OnSend), null);

                byteData = new byte[1024];
                //Start listening to the data asynchronously
                clientSocket.BeginReceiveFrom(byteData, 0, byteData.Length, SocketFlags.None, ref epServer,
                                           new AsyncCallback(OnReceive),null);

            base.Initialize();
        }
Пример #3
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if(Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Right)){
                Data msgToSend = new Data();

                msgToSend.strName = strName;
                msgToSend.playerID = playerID;
                msgToSend.strMessage = "right";
                msgToSend.cmdCommand = Command.Move;

                byte[] byteData = msgToSend.ToByte();
                //Send it to the server
                clientSocket.BeginSendTo(byteData, 0, byteData.Length, SocketFlags.None, epServer, new AsyncCallback(OnSend), null);
            }

            if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Left))
            {
                Data msgToSend = new Data();

                msgToSend.strName = strName;

                msgToSend.playerID = playerID;
                msgToSend.strMessage = "left";
                msgToSend.cmdCommand = Command.Move;

                byte[] byteData = msgToSend.ToByte();
                //Send it to the server
                clientSocket.BeginSendTo(byteData, 0, byteData.Length, SocketFlags.None, epServer, new AsyncCallback(OnSend), null);
            }
            if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Up))
            {
                Data msgToSend = new Data();

                msgToSend.strName = strName;

                msgToSend.playerID = playerID;
                msgToSend.strMessage = "up";
                msgToSend.cmdCommand = Command.Move;

                byte[] byteData = msgToSend.ToByte();
                //Send it to the server
                clientSocket.BeginSendTo(byteData, 0, byteData.Length, SocketFlags.None, epServer, new AsyncCallback(OnSend), null);
            }
            if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Down))
            {
                Data msgToSend = new Data();

                msgToSend.strName = strName;

                msgToSend.playerID = playerID;
                msgToSend.strMessage = "down";
                msgToSend.cmdCommand = Command.Move;

                byte[] byteData = msgToSend.ToByte();
                //Send it to the server
                clientSocket.BeginSendTo(byteData, 0, byteData.Length, SocketFlags.None, epServer, new AsyncCallback(OnSend), null);
            }

            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here

            base.Update(gameTime);
        }