Question

I have two processes Client and server. This are as follws : This is my client process:-

[Serializable ]
public class retobj
{
    public int a;

}
class client
{
    static void Main(string[] args)
    {
        TcpClient client = new TcpClient();
        client.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5005));
        Console.WriteLine("Connected.");
        retobj ob = new retobj();
        ob.a = 90;
        BinaryFormatter bf = new BinaryFormatter();
        NetworkStream ns = client.GetStream();
        bf.Serialize(ns, ob);
        Console.WriteLine("Data sent.");
        Console.ReadLine();
        ns.Close();
        client.Close();
    }
}

And this is my server process:

[Serializable]
public class retobj
{
    public int a;

}
class server
{
    static void Main(string[] args)
    {
        TcpListener listener = new TcpListener(IPAddress.Any, 5005);
        listener.Start();
        Console.WriteLine("Server started.");
        Socket client = listener.AcceptSocket();
        Console.WriteLine("Accepted client {0}.\n", client.RemoteEndPoint);
        List<string> l = null;
        retobj j = null;
        using (NetworkStream ns = new NetworkStream(client))
        {
            BinaryFormatter bf = new BinaryFormatter();
            j = (retobj )bf.Deserialize(ns);
        }
        //if (l != null)
        //    foreach (var item in l)
        //        Console.WriteLine(item);
        Console.WriteLine(j.a);
        Console.ReadLine();
        client.Close();
        listener.Stop();
    }

But it gives an error like: Error in server process: Unable to find assembly 'ConsoleApplication45, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

Was it helpful?

Solution

When you serialize an object using the BinaryFormatter, it includes information about what assembly the object came from. When deserializing it on the server, it reads that information and looks for the version of the retobj class from the client assembly, which is why you get that error. The one on the server is not the same.

Try moving that class to a Class Library project, and referencing that project from both the client and the server. You don't need two copies.

An alternative approach would be to use an alternate formatter, like the DataContractSerializer, that does not embed assembly information.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top