Bonsoir,
je suis actuellement entrain de faire une petite application client/serveur asynchrone où je rencontre un souci lorsque je souhaite récupérer ma variable 'name' lors de la sérialisation :
public byte[] Serialize()
{
List<byte> byteList = new List<byte>();
byteList.AddRange(BitConverter.GetBytes(this.Age)); // 4 octets
byteList.AddRange(BitConverter.GetBytes((short)this.Sex)); // 2 octets
byteList.AddRange(BitConverter.GetBytes((short)this.Nationality.Length)); // 2 octets
byteList.AddRange(Encoding.ASCII.GetBytes(this.Nationality)); // Length
byteList.AddRange(BitConverter.GetBytes((short)this.Name.Length)); // 2 octets
byteList.AddRange(Encoding.ASCII.GetBytes(this.Name)); // Length
return byteList.ToArray();
}
public static Person Deserialize(byte[] data)
{
int age = BitConverter.ToInt32(data, 0);
SexEnum sex = (SexEnum)BitConverter.ToInt16(data, 4);
short nationalityLength = BitConverter.ToInt16(data, 6);
string nationality = Encoding.ASCII.GetString(data,8, nationalityLength);
short nameLength = BitConverter.ToInt16(data, 8 + nationalityLength);
string name = Encoding.ASCII.GetString(data, 8 + nationalityLength + nameLength, nameLength);
return new Person(age, name, sex, nationality);
}
Le résultat obtenu est le suivant : https://www.noelshack.com/2018-28-3-1531343989-capture.png
Au lieu d'obtenir Perospero j'obtient les 2 dernières lettres.
Par ailleurs j'aimerais savoir comment faire une communication réseau d'objet via le NetworkStream du socket avec les class binaryWriter et binaryReader, une fois que j'écris dans le flux du socket je ne sais pas comment je peux envoyer et récupérer de l'autre coté mes données afin de les désérialiser avec binaryReader
Merci pour toutes aides