简体   繁体   中英

Sending a class over networkstream

So I am trying to send a 'Packet' class over a network stream with the following code:

IFormatter formatter = new BinaryFormatter();
NetworkStream stream = client.GetStream();
formatter.Serialize(stream, packet);


stream.Flush();
stream.Close();
client.Close();

using this class:

[Serializable]
public class Packet
{

    public string header;
    public string content;
    public int size = 0;

    public Packet(string header, string content)
    {
        this.header = header;
        this.content = content;

        size = Encoding.ASCII.GetByteCount(header) + Encoding.ASCII.GetByteCount(content);
    }
}

But I am getting the following error when reading on the other side:

'System.Runtime.Serialization.SerializationException: Unable to find assembly 'Client, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.'

This is my reading code:

NetworkStream ns = client.GetStream();
IFormatter formatter = new BinaryFormatter();
Packet p = (Packet)formatter.Deserialize(ns);
MessageBox.Show(p.header);
return p;

Any idea why this is happening?

EDIT:

Server side Packet class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Server
{

    public class Packet
    {
        public string header;
        public string content;
        public int size = 0;

        public Packet(string header, string content)
        {
            this.header = header;
            this.content = content;

            size = Encoding.ASCII.GetByteCount(header) + Encoding.ASCII.GetByteCount(content);
        }
    }
}

You can't binary serialize an object from one assembly and deserialize it against a class from a different assembly.

You need to have a third assembly that you reference from both the client and the server.

When you deserialise from a BinaryFormatter the class must be available. This is what the error is saying.

I'm assuming that the Packet class is definied in the Client.dll. If so, then just at a reference to Client.dll in the "Server" project and remove the Packet definition in the server.

General practice is to have a DataModel assembly that can be shared with Client and Server.

Also if you use the XmlSerializer instead of BinaryFormatter, then you can have different implementations of the class on both client and server.

You have created two separate (although functionally identical) Packet classes, and your client cannot deserialize a different type than the one that was serialized, even if they have the same name and structure.

Try defining the Packet class in a separate third project/assembly of the Class Library type. Afterwards, reference that project or assembly from both your client and server. To be even more correct, you would define an Interface IPacket in this class library and simply implement it in both your client and server.

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM