I was trying to send serealized object of custom type on server's side and deserealize it on client's side. I want to do that with Messagepack and tcp sockets. But when the bytes comes to client, programm occurred with error: "Failed to deserealize ". Type has two fields: Name and Age.
Server's side:
var tcpListener = new TcpListener(IPAddress.Any, 8888);
try
{
tcpListener.Start();
Console.WriteLine("Server is currently run, waiting for incoming connections");
Person Tom = new Person("Tom",40);
while (true)
{
using var tcpClient = await tcpListener.AcceptTcpClientAsync();
var stream = tcpClient.GetStream();
var data = MessagePackSerializer.Serialize(Tom);
await stream.WriteAsync(data);
Console.WriteLine($"Data was sent to {tcpClient.Client.RemoteEndPoint} client");
}
}
finally
{
tcpListener.Stop();
}
Client's side:
using TcpClient tcpClient= new TcpClient();
await tcpClient.ConnectAsync("127.0.0.1",8888);
Console.WriteLine("Client is run");
try
{
var stream = tcpClient.GetStream();
var resData = MessagePackSerializer.Deserialize<Person>(stream);
Console.WriteLine($"Person's name: {resData.Name} /n Person's age: {resData.Age}");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
How should I deserealize this object correctly? Alreade read topic: Error "This stream does not support seek operations" in C#. I've got the same mistake, but that doesnt help