I've been trying to fix this problem for quite a while now and it's a fairly annoying one. Basically, I have a XNA program that connects to a server and downloads pictures from it to use as texture2D. Everything works fine when tested in LAN, but when I send the program to a friend to test it over WAN, the image ends up randomly corrupted. In most cases, the start of the image is there but after that it's just the same color. I've tried all solutions I could find around and nothing seems to fix it. Here's my code for sending/receiving the picture:
Client
NetworkStream nws = tcpcl.GetStream();
while (true)
{
byte[] buffer = new byte[1024*1024*5];
int buffpos = 0;
while (nws.DataAvailable == false) Thread.Sleep(50);
while (nws.DataAvailable == true)
{
buffer[buffpos] = (byte)nws.ReadByte();
buffpos++;
}
byte[] actData = new byte[buffpos];
for (int I = 0; I < buffpos; I++)
{
actData[I] = buffer[I];
}
string message = ASCIIEncoding.ASCII.GetString(actData);
string ID = message.Substring(0, 1);
string data = message.Substring(1);
if (actData[0] == 1)
{
int namelength = BitConverter.ToInt32(actData, 1);
byte[] _name= new byte[namelength];
Array.Copy(actData, 5, _name, 0, namelength);
string name = ASCIIEncoding.ASCII.GetString(_name);
int avatarlength = BitConverter.ToInt32(actData, 5 + namelength);
byte[] avatar = new byte[avatarlength];
Array.Copy(actData, 9 + namelength, avatar, 0, avatarlength)
;
File.WriteAllBytes("content/" + name + ".png", avatar);
FileStream fs= File.OpenRead("Content/" + name + ".png");
User _user = new User(name, Texture2D.FromStream(GraphicsDevice, fs));
fs.Close();
User.AddUser(_user);
}
}
Server
us.endpoint = new IPEndPoint(groupEP.Address, groupEP.Port);
us.gameenabled = true;
us.x = Game.spawnx;
us.y = Game.spawny;
byte[] player_avatar = Game.playerAvatar(us.Username);
byte[] setup= new byte[5+player_avatar.Length];
setup[0] = (byte)maxcls;
BitConverter.GetBytes(player_avatar.Length).CopyTo(setup, 1);
player_avatar.CopyTo(setup, 5);
NetworkStream nws = us.clientsock.GetStream();
nws.Write(setup, 0, setup.Length);
Efficiency aside, is there anything in the code that could cause said problem over WAN?