输入流不是有效的二进制格式。TCP错误c#

输入流不是有效的二进制格式。TCP错误c#,c#,serialization,tcp,network-programming,C#,Serialization,Tcp,Network Programming,我一直在开发一个文件共享应用程序,它可以将任意大小的文件分块,序列化每个分块,并使用TCP连接有序地发送。但是我在两个系统之间接收时遇到了一个问题,错误是“输入流不是有效的二进制格式。起始内容(以字节为单位)是:0D-0A-00-01-00-00-FF-FF-FF-FF-01-00-00-00”,但发送方发送了适当数量的字节。是因为我在使用networkstream的beginRead和beginWrite方法还是什么?。请问我做错了什么? 我的客户代码 public void SendMess

我一直在开发一个文件共享应用程序,它可以将任意大小的文件分块,序列化每个分块,并使用TCP连接有序地发送。但是我在两个系统之间接收时遇到了一个问题,错误是“输入流不是有效的二进制格式。起始内容(以字节为单位)是:0D-0A-00-01-00-00-FF-FF-FF-FF-01-00-00-00”,但发送方发送了适当数量的字节。是因为我在使用networkstream的beginRead和beginWrite方法还是什么?。请问我做错了什么? 我的客户代码

public void SendMessage(string path)
   {
        NetworkStream file = new NetworkStream(client);

        try {
            foreach (Chunk item in ChunkAlgorithm.ReadChunk(path))
            {

                using (MemoryStream stream = new MemoryStream())
                {
                    BinaryFormatter serialize = new BinaryFormatter();
                    serialize.Serialize(stream, item);
                    stream.Seek(0, SeekOrigin.Begin);
                    SentBytes = stream.ToArray();
                    file.BeginWrite(SentBytes, 0, SentBytes.Length, MessageSentCallBack, null);
                    //Console.WriteLine($"sent: {SentBytes.Length} bytes");
                    Thread.Sleep(1);
                }
            }
            Console.WriteLine("Done sending bytes");
        }catch(Exception ex)
        {
            Console.WriteLine($"ERROR OCURRED: {ex.Message}");
        }
      //client.BeginSend(empty, 0, 0, 0, MessageSentCallBack, null);

    }
我的服务器代码

      public void StartReceiving()
    {


            file.BeginRead(receivingBytes, 0, receivingBytes.Length, ReceivedCallBack,null);

    }

    private void ReceivedCallBack(IAsyncResult ar)
    {
        try {
            string savepath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test1.mp4");


            int receivedLength = file.EndRead(ar);

            using (MemoryStream stream = new MemoryStream(receivingBytes))
            {

                BinaryFormatter formatter = new BinaryFormatter();
                Chunk ReceivedChunk = formatter.Deserialize(stream) as Chunk;

                if (ReceivedChunk.LastChunk)
                {
                    ChunkList.Add(ReceivedChunk);
                    Console.WriteLine($"Received Chunk: {ReceivedChunk.chunkCount} of {receivedLength} bytes");
                    couple(ChunkList, savepath);
                }
                else
                {
                    ChunkList.Add(ReceivedChunk);
                    Console.WriteLine($"Received Chunk: {ReceivedChunk.chunkCount} of {receivedLength} bytes");
                }

            }
            Thread.Sleep(1);
            StartReceiving();

        }catch(Exception ex)
        {
            Console.WriteLine($"ERROR OCURRED: {ex.Message}");
        }

    }

    private static void couple(List<Chunk> ChunkList,string savepath)
    {
        Console.WriteLine("Coupling...");
        ChunkAlgorithm.couple(ChunkList, savepath);
        Console.WriteLine("Coupling Done...");
        Console.WriteLine("Received All Data...");
    }
public void StartReceiving()
{
BeginRead(receivingBytes,0,receivingBytes.Length,ReceivedCallBack,null);
}
收到的专用作废回拨(IAsyncResult ar)
{
试一试{
字符串savepath=Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),“Test1.mp4”);
int receivedLength=file.EndRead(ar);
使用(MemoryStream stream=新的MemoryStream(receivingBytes))
{
BinaryFormatter formatter=新的BinaryFormatter();
Chunk ReceivedChunk=格式化程序。将(流)反序列化为Chunk;
if(ReceivedChunk.LastChunk)
{
ChunkList.Add(ReceivedChunk);
WriteLine($“接收的块:{ReceivedChunk.chunkCount}个{receivedLength}字节”);
耦合(ChunkList,savepath);
}
其他的
{
ChunkList.Add(ReceivedChunk);
WriteLine($“接收的块:{ReceivedChunk.chunkCount}个{receivedLength}字节”);
}
}
睡眠(1);
StartReceiving();
}捕获(例外情况除外)
{
WriteLine($“出现错误:{ex.Message}”);
}
}
私有静态void耦合(列表ChunkList、字符串保存路径)
{
控制台。写入线(“耦合…”);
ChunkAlgorithm.couple(ChunkList,savepath);
控制台。写入线(“耦合完成…”);
Console.WriteLine(“已接收所有数据…”);
}

BinaryFormatter
格式是未记录的专有格式。如果你发送一些可读的东西,比如JSON字符串,你是什么意思?。。。我正在序列化文件的一部分内容并发送它。对,而且文件的格式没有文档记录,所以很难看出哪里出了问题。使用可读的、有文档记录的格式进行测试可能会使调试更容易。好的,我明白你的意思,但是在调试时,我注意到发送方发送了实际字节数,而接收方在每次运行时都没有在不同的点接收到实际字节数。所以我认为这是并发性的问题。啊-我在你的问题中没有看到任何东西表明接收到的字节数较少,只是输入流不是有效的二进制格式,这可能是由于许多不同的原因,例如发送和接收系统之间的端号差异或缺少类型标识
BinaryFormatter
流“繁琐”,不能保证在使用不同DLL版本或体系结构的计算机之间兼容。您可能希望您的问题提供一个更简单的示例,以消除
BinaryFormatter
的不确定性。