Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何反序列化从网络接收的文件_C#_Serialization_Tcpclient - Fatal编程技术网

C# 如何反序列化从网络接收的文件

C# 如何反序列化从网络接收的文件,c#,serialization,tcpclient,C#,Serialization,Tcpclient,我正在开发一个软件,我需要一些帮助 我有一个客户端和服务器。服务器将序列化文本文件并将其发送到客户端 我目前的进展是:客户端接收到二进制数组,但无法将其作为原始文本文件写入光盘 private void ListenPort() { TcpListener _TcpListener= new _TcpListener(7381); byte[] received_binary= new byte[1024]; _TcpListener.Start(); whil

我正在开发一个软件,我需要一些帮助

我有一个客户端和服务器。服务器将序列化文本文件并将其发送到客户端

我目前的进展是:客户端接收到二进制数组,但无法将其作为原始文本文件写入光盘

private void ListenPort()
{
    TcpListener _TcpListener= new _TcpListener(7381);
    byte[] received_binary= new byte[1024];

    _TcpListener.Start();
    while (true) 
    { 
        Socket Soket = _TcpListener.AcceptSocket();
        Soket.Receive(received_binary, received_binary.Length, 0);
    }
}

是否需要将其反序列化为字符串?这是我不久前写的一个函数,可能会有帮助

 public static T BinaryDeserializeObject<T>(byte[] serializedType)
    {
        if (serializedType == null)
            throw new ArgumentNullException("serializedType");

        if (serializedType.Length.Equals(0))
            throw new ArgumentException("byte array cannot be empty"));

        T deserializedObject;

        using (MemoryStream memoryStream = new MemoryStream(serializedType))
        {
            BinaryFormatter deserializer = new BinaryFormatter();
            deserializedObject = (T)deserializer.Deserialize(memoryStream);
        }

        return deserializedObject;
    }

主要问题似乎是您忽略了receive的返回值。这将返回每次读取的字节数。您应该循环,直到这是非正的,每次处理(例如,仅写入多个字节的文件流):即使缓冲区更大。

一种可能的解决方案是将文本文件作为一系列字符串或字节数组加载并发送。字节数组方法可能是最简洁、最有效的方法,因为它可以在发送过程中进行压缩,使用该方法调用发送的应用程序将如下所示:

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

using NetworkCommsDotNet;

namespace Client
{
    class Program
    {
        static void Main(string[] args)
        {
            byte[] bytesToSend = File.ReadAllBytes("testFile.txt");
            TCPConnection.GetConnection(new ConnectionInfo("127.0.0.1", 10000)).SendObject("TextFileData", bytesToSend);

            Console.WriteLine("Press any key to exit client.");
            Console.ReadKey(true);
            NetworkComms.Shutdown();
        }
    }
}
和服务器:

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

using NetworkCommsDotNet;

namespace Server
{
    class Program
    {
        static void Main(string[] args)
        {
            NetworkComms.AppendGlobalIncomingPacketHandler<byte[]>("TextFileData", (packetHeader, connection, incomingData) => 
            {
                    Console.WriteLine("Received TextFileData");
                    File.WriteAllBytes("testFile.txt", incomingData);
            });

            TCPConnection.StartListening(true);

            Console.WriteLine("Server ready. Press any key to shutdown server.");
            Console.ReadKey(true);
            NetworkComms.Shutdown();
        }
    }
}

显然,您需要从网站下载NetworkCommsDotNet DLL,以便将其添加到“使用NetworkCommsDotNet”参考中。另请参见客户机示例中的服务器IP地址当前为127.0.0.1,如果在同一台计算机上同时运行服务器和客户机,则应该可以这样做。有关更多信息,请查看或文章。

看起来像是当前发生的事情的副本?在跳转到BinaryFormatter之前,我建议询问OP序列化文本文件意味着什么。毕竟,文件已经完全序列化了。这些数据通常只是文件的原始字节。BinaryFormatter的性能非常糟糕。有一些优秀的连续器,比如protobuf net,如果你想要的话。是的,我是protobuf net的超级粉丝,我相信上面的Marc Gravell写道——我最近一直在用protobuf net处理一些很重的东西,它只是执行而已!我发布的代码来自我第一次使用c时编写的一个古老的库,但Op确实说他收到了一个二进制数组,他无法将其转换为字符串,而这段代码将完成转换它的工作。