jjzjj

c# - 通过网络发送自定义结构 - SerializationException

coder 2023-09-18 原文

早上好,我有一个 TCP 服务器和一个 TCP 客户端。 这是服务器的代码:

public static class Server
{
    private static IPEndPoint endPoint;

    private static TcpListener tcpServer;
    private static List<Client> clients;
    private static Thread threadListen;

    private static ASCIIEncoding encoding;

    public static void Initialize(IPAddress allowedIPAddress, int port)
    {
        endPoint = new IPEndPoint(allowedIPAddress, port);

        tcpServer = new TcpListener(endPoint);
        clients = new List<Client>();
        threadListen = new Thread(new ThreadStart(Listen));

        encoding = new ASCIIEncoding();
    }

    public static void Start()
    {
        threadListen.Start();
    }

    public static byte[] PacketToArray(Packet packet)
    {
        IFormatter formatter = new BinaryFormatter();
        MemoryStream stream = new MemoryStream();

        formatter.Serialize(stream, packet);
        byte[] packetArray = stream.GetBuffer();

        stream.Close();

        return packetArray;
    }

    public static Packet ArrayToPacket(byte[] array)
    {
        IFormatter formatter = new BinaryFormatter();
        MemoryStream stream = new MemoryStream(array);

        Packet packet = new Packet();
        packet = (Packet) formatter.Deserialize(stream);

        stream.Close();

        return packet;
    }

    public static void Send(Client target, Packet packet)
    {
        byte[] packetArray = PacketToArray(packet);

        target.networkStream.Write(packetArray, 0, packetArray.Length);
        target.networkStream.Flush();
        OnSend(packet);
    }

    private static void Listen()
    {
        tcpServer.Start();

        while (true)
        {
            try
            {
                Client client = new Client();
                client.tcpClient = tcpServer.AcceptTcpClient();
                client.networkStream = client.tcpClient.GetStream();
                client.thread = new Thread(new ParameterizedThreadStart(HandleCommunication));

                clients.Add(client);
                client.thread.Start(client);
                OnJoin(client);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: " + ex.Message);
                break;
            }
        }
    }

    private static void HandleCommunication(object client)
    {
        Client handleClient = (Client) client;

        byte[] messageBuffer = new byte[4096];
        int bytesRead = 0;

        while (true)
        {
            bytesRead = 0;

            try
            {
                bytesRead = handleClient.networkStream.Read(messageBuffer, 0, messageBuffer.Length);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: " + ex.Message);
                break;
            }

            if (bytesRead == 0)
            {
                clients.Remove(handleClient);
                OnLeave(handleClient);
                break;
            }

            Packet packet = ArrayToPacket(messageBuffer);
            OnReceive(packet);
        }
    }

    private static void OnStart()
    {
        Console.WriteLine("Server started on port .");
    }

    private static void OnJoin(Client client)
    {
        Console.WriteLine("Client [ID] connected.");

        Send(client, PacketGenerator.Generate(OpCodes.opHandshake, null));
    }

    private static void OnLeave(Client client)
    {
        Console.WriteLine("Client [ID] disconnected.");
    }

    private static void OnSend(Packet packet)
    {
        Console.WriteLine("Server: " + packet.opcode.ToString() + " | " + packet.message);
    }

    private static void OnReceive(Packet packet)
    {
        Console.WriteLine("Client [ID]: " + packet.opcode.ToString() + " | " + packet.message);
    }

    public struct Client
    {

        public TcpClient tcpClient;
        public NetworkStream networkStream;
        public Thread thread;

    }

}

这是客户端的代码:

[Serializable]
public struct Packet
{

    public int opcode;
    public string message;

}

public static class Client
{

    private static IPEndPoint endPoint;

    private static TcpClient tcpClient;
    private static NetworkStream networkStream;
    private static Thread threadCommunication;

    private static ASCIIEncoding encoding;

    public static void Initialize(string ipAddress, int port)
    {
        endPoint = new IPEndPoint(IPAddress.Parse(ipAddress), port);
        tcpClient = new TcpClient();
        networkStream = null;
        threadCommunication = new Thread(new ThreadStart(HandleCommunication));

        encoding = new ASCIIEncoding();
    }

    public static void Connect()
    {
        tcpClient.Connect(endPoint);
        threadCommunication.Start();
        Console.WriteLine("Connected to server.");

        networkStream = tcpClient.GetStream();
    }

    public static byte[] PacketToArray(Packet packet)
    {
        IFormatter formatter = new BinaryFormatter();
        MemoryStream stream = new MemoryStream();

        formatter.Serialize(stream, packet);
        byte[] packetArray = stream.GetBuffer();

        stream.Close();

        return packetArray;
    }

    public static Packet ArrayToPacket(byte[] array)
    {
        IFormatter formatter = new BinaryFormatter();
        MemoryStream stream = new MemoryStream(array);

        Packet packet = new Packet();
        packet = (Packet)formatter.Deserialize(stream);

        stream.Close();

        return packet;
    }

    public static void Send(Packet packet)
    {
        byte[] packetArray = PacketToArray(packet);

        networkStream.Write(packetArray, 0, packetArray.Length);
        networkStream.Flush();
        OnSend(packet);
    }

    private static void HandleCommunication()
    {
        byte[] messageBuffer = new byte[4096];
        int bytesRead = 0;

        while (true)
        {
            bytesRead = 0;

            try
            {
                bytesRead = networkStream.Read(messageBuffer, 0, messageBuffer.Length);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: " + ex.Message);
                break;
            }

            if (bytesRead == 0)
            {
                Console.WriteLine("Connection closed.");
                break;
            }

            Packet packet = ArrayToPacket(messageBuffer);
            OnReceive(packet);
        }
    }

    private static void OnSend(Packet packet)
    {
        Console.WriteLine("Client [ID]: " + packet.opcode.ToString() + " | " + packet.message);
    }

    private static void OnReceive(Packet packet)
    {
        Console.WriteLine("Server: " + packet.opcode.ToString() + " | " + packet.message);

        switch (packet.opcode)
        {

            case OpCodes.opHandshake:
                Send(PacketGenerator.Generate(OpCodes.opHandshake, null));
                break;

        }
    }

}

但是当我从服务器向客户端发送数据包(握手)时,客户端在“packet = (Packet) formatter.Deserialize(stream);”处的“ArrayToPacket”函数中出现异常。 确切的消息是:找不到程序集“ComDee,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null”。 (ComDee 是运行服务器的程序集)。

为什么?客户端的包结构与服务端的结构相同。

更新

我编辑了服务器和客户端类并使用了 protobuf-net。 但是客户端类中的“OnReceive”不会被调用。 问题出在哪里? 服务器类:

[ProtoContract]
public struct Packet
{

    [ProtoMember(1)] public int opcode;
    [ProtoMember(2)] public string message;

}

public static class Server
{

    private static IPEndPoint endPoint;

    private static TcpListener tcpServer;
    private static List<Client> clients;
    private static Thread threadListen;

    private static ASCIIEncoding encoding;

    public static void Initialize(IPAddress allowedIPAddress, int port)
    {
        endPoint = new IPEndPoint(allowedIPAddress, port);

        tcpServer = new TcpListener(endPoint);
        clients = new List<Client>();
        threadListen = new Thread(new ThreadStart(Listen));

        encoding = new ASCIIEncoding();
    }

    public static void Start()
    {
        threadListen.Start();
    }

    public static byte[] PacketToArray(Packet packet)
    {
        IFormatter formatter = new BinaryFormatter();
        MemoryStream stream = new MemoryStream();

        Serializer.Serialize<Packet>(stream, packet);
        byte[] packetArray = stream.GetBuffer();

        stream.Close();

        return packetArray;
    }

    public static Packet ArrayToPacket(byte[] array)
    {
        IFormatter formatter = new BinaryFormatter();
        MemoryStream stream = new MemoryStream(array);

        Packet packet = new Packet();
        packet = Serializer.Deserialize<Packet>(stream);

        stream.Close();

        return packet;
    }

    public static void Send(Client target, Packet packet)
    {
        byte[] packetArray = PacketToArray(packet);

        target.networkStream.Write(packetArray, 0, packetArray.Length);
        target.networkStream.Flush();
        OnSend(packet);
    }

    private static void Listen()
    {
        tcpServer.Start();
        OnStart();

        while (true)
        {
            try
            {
                Client client = new Client();
                client.tcpClient = tcpServer.AcceptTcpClient();
                client.networkStream = client.tcpClient.GetStream();
                client.thread = new Thread(new ParameterizedThreadStart(HandleCommunication));

                clients.Add(client);
                client.thread.Start(client);
                OnJoin(client);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: " + ex.Message);
                break;
            }
        }
    }

    private static void HandleCommunication(object client)
    {
        Client handleClient = (Client) client;

        byte[] messageBuffer = new byte[4096];
        int bytesRead = 0;

        while (true)
        {
            bytesRead = 0;

            try
            {
                bytesRead = handleClient.networkStream.Read(messageBuffer, 0, messageBuffer.Length);
            }
            catch
            {
                clients.Remove(handleClient);
                OnLeave(handleClient);
                break;
            }

            if (bytesRead == 0)
            {
                Packet packet = ArrayToPacket(messageBuffer);
                OnReceive(packet);
            }
        }
    }

    private static void OnStart()
    {
        Console.WriteLine("Server started.");
    }

    private static void OnJoin(Client client)
    {
        Console.WriteLine("Client [ID] connected.");

        Send(client, PacketGenerator.Generate(OpCodes.opHandshake, null));
    }

    private static void OnLeave(Client client)
    {
        Console.WriteLine("Client [ID] disconnected.");
    }

    private static void OnSend(Packet packet)
    {
        Console.WriteLine("Server: " + packet.opcode.ToString() + " | " + packet.message);
    }

    private static void OnReceive(Packet packet)
    {
        Console.WriteLine("Client [ID]: " + packet.opcode.ToString() + " | " + packet.message);
    }

    public struct Client
    {

        public TcpClient tcpClient;
        public NetworkStream networkStream;
        public Thread thread;

    }

}

这是客户端类:

[ProtoContract]
public struct Packet
{

    [ProtoMember(1)]
    public int opcode;
    [ProtoMember(2)]
    public string message;

}

public static class Client
{

    private static IPEndPoint endPoint;

    private static TcpClient tcpClient;
    private static NetworkStream networkStream;
    private static Thread threadCommunication;

    private static ASCIIEncoding encoding;

    public static void Initialize(string ipAddress, int port)
    {
        endPoint = new IPEndPoint(IPAddress.Parse(ipAddress), port);
        tcpClient = new TcpClient();
        networkStream = null;
        threadCommunication = new Thread(new ThreadStart(HandleCommunication));

        encoding = new ASCIIEncoding();
    }

    public static void Connect()
    {
        tcpClient.Connect(endPoint);
        threadCommunication.Start();
        Console.WriteLine("Connected to server.");

        networkStream = tcpClient.GetStream();
    }

    public static byte[] PacketToArray(Packet packet)
    {
        IFormatter formatter = new BinaryFormatter();
        MemoryStream stream = new MemoryStream();

        Serializer.Serialize<Packet>(stream, packet);
        byte[] packetArray = stream.GetBuffer();

        stream.Close();

        return packetArray;
    }

    public static Packet ArrayToPacket(byte[] array)
    {
        IFormatter formatter = new BinaryFormatter();
        MemoryStream stream = new MemoryStream(array);

        Packet packet = new Packet();
        packet = Serializer.Deserialize<Packet>(stream);

        stream.Close();

        return packet;
    }

    public static void Send(Packet packet)
    {
        byte[] packetArray = PacketToArray(packet);

        networkStream.Write(packetArray, 0, packetArray.Length);
        networkStream.Flush();
        OnSend(packet);
    }

    private static void HandleCommunication()
    {
        byte[] messageBuffer = new byte[4096];
        int bytesRead = 0;

        while (true)
        {
            bytesRead = 0;

            try
            {
                bytesRead = networkStream.Read(messageBuffer, 0, messageBuffer.Length);
            }
            catch
            {
                Console.WriteLine("Connection closed.");
                break;
            }

            if (bytesRead == 0)
            {
                Packet packet = ArrayToPacket(messageBuffer);
                OnReceive(packet);
            }
        }
    }

    private static void OnSend(Packet packet)
    {
        Console.WriteLine("Client [ID]: " + packet.opcode.ToString() + " | " + packet.message);
    }

    private static void OnReceive(Packet packet)
    {
        Console.WriteLine("Server: " + packet.opcode.ToString() + " | " + packet.message);

        switch (packet.opcode)
        {

            case OpCodes.opHandshake:
                Send(PacketGenerator.Generate(OpCodes.opHandshake, null));
                break;

        }
    }

}

最佳答案

类型由它们的程序集限定范围,BinaryFormatter 序列化类型元数据(程序集限定名称等)。在两个地方拥有相同的副本是不够的:它们不是相同的类型,除非它们来自相同的程序集

另请注意,BinaryFormatter 在线路上存储的内容非常冗长。如果您想要解决这两个问题而无需手动执行所有序列化,那么 protobuf-net 将有所帮助:

  • 在电线上很密
  • 它不绑定(bind)特定类型;只要 A 和 B 看起来相似,就可以用 A 序列化并用 B 反序列化

例如:

[ProtoContract]
public struct Packet {    
    [ProtoMember(1)] public int opcode;
    [ProtoMember(2)] public string message;
}

并使用 Serializer.Serialize 而不是 BinaryFormatter

关于c# - 通过网络发送自定义结构 - SerializationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14727791/

有关c# - 通过网络发送自定义结构 - SerializationException的更多相关文章

  1. ruby - Facter::Util::Uptime:Module 的未定义方法 get_uptime (NoMethodError) - 2

    我正在尝试设置一个puppet节点,但ruby​​gems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由ruby​​gems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby

  2. ruby - 使用 ruby​​ 将 HTML 转换为纯文本并维护结构/格式 - 2

    我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h

  3. ruby - 通过 rvm 升级 ruby​​gems 的问题 - 2

    尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub

  4. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  5. ruby - 通过 erb 模板输出 ruby​​ 数组 - 2

    我正在使用puppet为ruby​​程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby​​不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这

  6. ruby-on-rails - form_for 中不在模型中的自定义字段 - 2

    我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢

  7. ruby - 主要 :Object when running build from sublime 的未定义方法 `require_relative' - 2

    我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby​​1.9+ 关于ruby-主要:Objectwhenrun

  8. ruby - 通过 ruby​​ 进程共享变量 - 2

    我正在编写一个gem,我必须在其中fork两个启动两个webrick服务器的进程。我想通过基类的类方法启动这个服务器,因为应该只有这两个服务器在运行,而不是多个。在运行时,我想调用这两个服务器上的一些方法来更改变量。我的问题是,我无法通过基类的类方法访问fork的实例变量。此外,我不能在我的基类中使用线程,因为在幕后我正在使用另一个不是线程安全的库。所以我必须将每个服务器派生到它自己的进程。我用类变量试过了,比如@@server。但是当我试图通过基类访问这个变量时,它是nil。我读到在Ruby中不可能在分支之间共享类变量,对吗?那么,还有其他解决办法吗?我考虑过使用单例,但我不确定这是

  9. ruby - 通过 RVM (OSX Mountain Lion) 安装 Ruby 2.0.0-p247 时遇到问题 - 2

    我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search

  10. ruby-on-rails - Enumerator.new 如何处理已通过的 block ? - 2

    我在理解Enumerator.new方法的工作原理时遇到了一些困难。假设文档中的示例:fib=Enumerator.newdo|y|a=b=1loopdoy[1,1,2,3,5,8,13,21,34,55]循环中断条件在哪里,它如何知道循环应该迭代多少次(因为它没有任何明确的中断条件并且看起来像无限循环)? 最佳答案 Enumerator使用Fibers在内部。您的示例等效于:require'fiber'fiber=Fiber.newdoa=b=1loopdoFiber.yieldaa,b=b,a+bendend10.times.m

随机推荐