Hello, my name is Eric and I have a little problem with networking in c#
this is my update method code:
The Paddle.Update method:
This is going to be a pong multiplayer game (therefore the paddle names). Before when the guest was sending it's package before recieving, the code worked except for that when playing as the guest the host's X position was set to the guest's X position. I was testing if changing so that the guest and host recieved data before sending it would solve the problem.
By doing so the host gets the error message: "Unable to read beyond the end of the stream". How do I solve this problem?
this is my update method code:
C#:
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
switch (gameState)
{
case (GameState.PlayingAsHost):
session.Update();
while (session.LocalGamers[0].IsDataAvailable) //Recieve data
{
NetworkGamer sender;
PacketReader reader = new PacketReader();
session.LocalGamers[0].ReceiveData(reader, out sender);
guestPaddle.Position = reader.ReadVector2();
}
hostPaddle.Update(); //Update paddle position
var packetWriter = new PacketWriter(); //Send data
packetWriter.Write(new Vector2(hostPaddle.Position.X, 50));
session.LocalGamers[0].SendData(packetWriter, SendDataOptions.InOrder);
if (session.AllGamers.Count < 2) //has a player dissconnected?
{
session.Dispose();
gameState = GameState.NotSignedIn;
}
break;
case (GameState.PlayingAsGuest):
session.Update();
while (session.LocalGamers[0].IsDataAvailable)//Recieve data
{
NetworkGamer sender;
PacketReader reader = new PacketReader();
session.LocalGamers[0].ReceiveData(reader, out sender);
hostPaddle.Position = reader.ReadVector2();
}
guestPaddle.Update(); //Update paddle position
var packetWriter = new PacketWriter(); //Send data
packetWriter.Write(new Vector2(guestPaddle.Position.X, 50));
session.LocalGamers[0].SendData(packetWriter, SendDataOptions.InOrder);
if (session.AllGamers.Count < 2)
{
session.Dispose();
gameState = GameState.NotSignedIn;
}
break;
}
}
C#:
internal void Update()
{
MathHelper.Clamp((float)Position.X, 0, 800);
Position = new Vector2(Mouse.GetState().X, Position.Y);
Hitbox = new Rectangle((int)Position.X, (int)Position.Y, Texture.Width, Texture.Height);
}
By doing so the host gets the error message: "Unable to read beyond the end of the stream". How do I solve this problem?
Last edited: