为什么我使用 BinaryFormatter 得到 SerializationException?

Why I get a SerializationException with BinaryFormatter?

我正在开发一个 .NET 应用程序,服务器通过 TCP/IP 从网络摄像头向客户端发送 JPG 压缩图像。对于 serialization/deserialization,我使用 BinaryFormatter class。在我的台式电脑 (client/Windows 10) 和我的笔记本电脑 (server/Windows 10) 之间进行测试时,在长 运行 中一切正常。使用 LattePanda (server/Windows 7) 时,大约 3-5 分钟后 运行 出现以下错误(我 send/receive 每秒 30 帧):

An unhandled exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.dll

Additional information: The input stream is not a valid binary format. The starting contents (in bytes) are: 00-00-00-01-00-00-00-FF-FF-FF-FF-01-00-00-00-00-00 ...

这是服务器代码的一个片段:

    private void distribute(Camera camera, Mat frame) {
        if(clientSockets!=null) {
            if(clientSockets.Count > 0) {
                if(camera.Streaming) {
                    // compress and encapsulate raw image with jpg algorithm
                    CameraImage packet = new CameraImage(camera.Id, frame, camera.CodecInfo, camera.EncoderParams);
                    packet.SystemId = Program.Controller.Identity.Id;
                    packet.SequenceNumber = curSeqNum;
                    byte[] content;
                    using(MemoryStream ms = new MemoryStream()) {
                        BinaryFormatter bf = new BinaryFormatter();
                        bf.Serialize(ms, packet);
                        content = ms.ToArray();
                    }
                    byte[] payload = new byte[content.Length+4];
                    // prefix with packet length
                    Array.Copy(BitConverter.GetBytes(content.Length), 0, payload, 0, 4);
                    // append payload after length header
                    Array.Copy(content, 0, payload, 4, content.Length);
                    // distribute to connected clients
                    this.distribute(payload);
                }
            }
        }
    }

    private void distribute(byte[] bytes) {
        if(Program.Launched) {
            lock(syncobj) {
                // distribute to connected clients
                for(int i=clientSockets.Count-1; i>=0; i--) {
                    try {
                        clientSockets[i].Send(bytes, bytes.Length, SocketFlags.None);
                    } catch(SocketException) {
                        clientSockets.RemoveAt(i);
                    }
                }
            }
        }
    }

这是客户端代码的一个片段:

    private void receive() {
        try {
            while(running) {
                if((available = clientSocket.Receive(buffer, 4, SocketFlags.None)) > 0) {
                    // receive bytes from tcp stream
                    int offset = 0;
                    int bytesToRead = BitConverter.ToInt32(buffer, 0);
                    byte[] data = new byte[bytesToRead];
                    while(bytesToRead > 0) {
                        int bytesReceived = clientSocket.Receive(data, offset, bytesToRead, SocketFlags.None);
                        offset += bytesReceived;
                        bytesToRead -= bytesReceived;
                    }
                    // deserialize byte array to network packet
                    NetworkPacket packet = null;
                    using(MemoryStream ms = new MemoryStream(data)) {
                        BinaryFormatter bf = new BinaryFormatter();
                        packet = (NetworkPacket)bf.Deserialize(ms);
                    }
                    // deliver network packet to listeners
                    if(packet!=null) {
                        this.onNetworkPacketReceived?.Invoke(packet);
                    }
                    // update network statistics
                    NetworkStatistics.getInstance().TotalBytesRtpIn += data.Length;
                }
            }
        } catch(SocketException ex) {
            onNetworkClientDisconnected?.Invoke(agent.SystemId);
        } catch(ObjectDisposedException ex) {
            onNetworkClientDisconnected?.Invoke(agent.SystemId);
        } catch(ThreadAbortException ex) {
            // allows your thread to terminate gracefully
            if(ex!=null) Thread.ResetAbort();
        }
    }

知道为什么我只在一台机器上获得 SerializationException 而在另一台机器上没有吗?安装了不同的 mscorlib.dll 库?如何查看相关(?)库的版本?

正如 Scott 所建议的,我用更安全的方法替换了读取消息 header 的不安全行:

    private void receive() {
        try {
            while(running) {
                if(clientSocket.Available>=4) {
                    // receive header bytes from tcp stream
                    byte[] header = new byte[4];
                    clientSocket.Receive(header, 4, SocketFlags.None);
                    int bytesToRead = BitConverter.ToInt32(header, 0);
                    // receive body bytes from tcp stream
                    int offset = 0;
                    byte[] data = new byte[bytesToRead];
                    while(bytesToRead > 0) {
                        int bytesReceived = clientSocket.Receive(data, offset, bytesToRead, SocketFlags.None);
                        offset += bytesReceived;
                        bytesToRead -= bytesReceived;
                    }
                    // deserialize byte array to network packet
                    NetworkPacket packet = null;
                    using(MemoryStream ms = new MemoryStream(data)) {
                        BinaryFormatter bf = new BinaryFormatter();
                        packet = (NetworkPacket)bf.Deserialize(ms);
                    }
                    // deliver network packet to listeners
                    if(packet!=null) {
                        this.onNetworkPacketReceived?.Invoke(packet);
                    }
                    // update network statistics
                    NetworkStatistics.getInstance().TotalBytesRtpIn += data.Length;
                }
            }
        } catch(SocketException ex) {
            onNetworkClientDisconnected?.Invoke(agent.SystemId);
        } catch(ObjectDisposedException ex) {
            onNetworkClientDisconnected?.Invoke(agent.SystemId);
        } catch(ThreadAbortException ex) {
            // allows your thread to terminate gracefully
            if(ex!=null) Thread.ResetAbort();
        }
    }

现在运行完美了:)非常感谢您的帮助!

这是 your answer 的调整版本。现在如果 clientSocket.Available < 4running == true 你有一个空的 while(true) { } 循环。这将占用一个 cpu 核心的 100%,但没有做任何有用的工作。

在系统缓冲区中有 4 个字节之前,不要循环什么也不做,而是使用与为消息的有效负载所做的相同类型的循环,并将其加载到 [=31] 的字节数组=]. (我还将读取有效负载数据的循环简化为我不常用的循环。)

private void receive() {
    try {
        while(running) {
            int offset = 0;
            byte[] header = new byte[4];

            // receive header bytes from tcp stream
            while (offset < header.Length) {
                offset += clientSocket.Receive(header, offset, header.Length - offset, SocketFlags.None);
            }
                int bytesToRead = BitConverter.ToInt32(header, 0);
                // receive body bytes from tcp stream
                offset = 0;
                byte[] data = new byte[bytesToRead];
                while(offset < data.Length) {
                    offset += clientSocket.Receive(data, offset, data.Length - offset, SocketFlags.None);
                }
                // deserialize byte array to network packet
                NetworkPacket packet = null;
                using(MemoryStream ms = new MemoryStream(data)) {
                    BinaryFormatter bf = new BinaryFormatter();
                    packet = (NetworkPacket)bf.Deserialize(ms);
                }
                // deliver network packet to listeners
                if(packet!=null) {
                    this.onNetworkPacketReceived?.Invoke(packet);
                }
                // update network statistics 
                NetworkStatistics.getInstance().TotalBytesRtpIn += data.Length;
            }
        }
    } catch(SocketException ex) {
        onNetworkClientDisconnected?.Invoke(agent.SystemId);
    } catch(ObjectDisposedException ex) {
        onNetworkClientDisconnected?.Invoke(agent.SystemId);
    } catch(ThreadAbortException ex) {
        // allows your thread to terminate gracefully
        if(ex!=null) Thread.ResetAbort();
    }
}

但是,如果您从 System.Net.Socket class 切换到 System.Net.TcpClient class,您可以大大简化您的代码。首先,如果您不需要 TotalBytesRtpIn 进行更新,您可以停止发送 header,反序列化不需要它,因为 BinaryFormatter 已经将其长度存储为有效负载的内部字段。然后您需要做的就是从 TcpClient 获取 NetworkStream 并在数据包传入时对其进行处理。

private TcpClient _client; // Set this wherever you had your original Socket set up.

private void receive() {
    try {
        using(var stream = _client.GetStream()) {
            BinaryFormatter bf = new BinaryFormatter();
            while(running) {


#region This part is not needed if you are only going to deserialize the stream and not update TotalBytesRtpIn, make sure the server stops sending the header too!
                    int offset = 0;
                    byte[] header = new byte[4];

                    // receive header bytes from tcp stream
                    while (offset < header.Length) {
                        offset += stream.Read(header, offset, header.Length - offset);
                    }
                    int bytesToRead = BitConverter.ToInt32(header, 0);
#endregion
                    packet = (NetworkPacket)bf.Deserialize(stream);

                    // deliver network packet to listeners
                    if(packet!=null) {
                        this.onNetworkPacketReceived?.Invoke(packet);
                    }
                    // update network statistics
                    NetworkStatistics.getInstance().TotalBytesRtpIn += bytesToRead;
                }
            }
        }
    //These may need to get changed.
    } catch(SocketException ex) {
        onNetworkClientDisconnected?.Invoke(agent.SystemId);
    } catch(ObjectDisposedException ex) {
        onNetworkClientDisconnected?.Invoke(agent.SystemId);
    } catch(ThreadAbortException ex) {
        // allows your thread to terminate gracefully
        if(ex!=null) Thread.ResetAbort();
    }
}