/// <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(); }
/// <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); }