简体   繁体   English

C#,基于套接字通信的二进制序列化

[英]C#, Binary Serialization over Socket Communication

I use Binary Serialization Object to Read or Write Data Stream and previously if we are going to write/read them we have to compile the "instance class" to be DLL file so we have same Assemblies between them, is it right? 我使用二进制序列化对象读取或写入数据流,以前如果要写入/读取它们,则必须将“实例类”编译为DLL文件,因此它们之间具有相同的程序集,对吗?

well now, here is my problem: 好吧,这是我的问题:

this is my Binary Sender Code 这是我的二进制发件人代码

public void SendBinarySerial(object graph)
{
    IFormatter formatter = new BinaryFormatter();

    System.Net.Sockets.NetworkStream strm = mClient.GetStream(); //get TCPclient stream

    formatter.Serialize(strm, graph);
}

and here is my Binary Receiver code: 这是我的二进制接收器代码:

private void doRead()
{
    do 
     {
       if (mClient.GetStream().CanRead) 
          {
             System.Net.Sockets.NetworkStream strm = mClient.GetStream();

              IFormatter formatter = new BinaryFormatter();

              object mydat = (object)formatter.Deserialize(strm);//deserialize data receiver         
           }
    } while (true);
}

And this is my "instance class" code that i want to send to the entire of clients. 这是我要发送给整个客户的“实例类”代码。 ok Let's consider the following code: 好的,让我们考虑以下代码:

[Serializable()]
public class DataSend
{
    public List<Bitmap> MainData = new List<Bitmap>();
    public List<string> Title = new List<string>();
}

As you can see , there are several List Variables such as Bitmap and string. 如您所见,有几个列表变量,例如位图和字符串。 the Bitmap data is for image data. 位图数据用于图像数据。 if I fill it with many images it will increase the size of variable memory, maybe about 20 mb. 如果我用很多图像填充它,则会增加可变内存的大小,可能约为20 mb。

After that I'm about to send them to the entire of clients. 之后,我将把它们发送给整个客户。 Well, here is my question: 好吧,这是我的问题:

Using this technique, if the size object is about 20 mb, will this Work well? 使用此技术,如果大小对象约为20 mb,是否可以正常工作? I know that Socket Communication has a size limit for sending data 我知道套接字通信有发送数据的大小限制

I don't want to define any protocols at all, I just want to use Socket Communication although maybe this is not best way. 我根本不想定义任何协议,我只想使用套接字通信,尽管这也许不是最好的方法。

Thanks for your response. 感谢您的答复。

I wouldn't worry about size limits using sockets. 我不会担心使用套接字的大小限制。 From my experience it's possible to read/write arbitrary sizes from or to NetworkStream. 根据我的经验,可以从NetworkStream读取/写入任意大小。 I would however worry about the serializability of the Bitmap objects. 但是,我会担心Bitmap对象的可序列化性。 As far as i know this will not serialize the image data. 据我所知,这不会序列化图像数据。

The BinaryFormatter will really start to struggle once your file size is above several MB. 一旦文件大小超过数MB,BinaryFormatter就会真正开始挣扎。 Have you considered using an open source communication library which uses more sophisticated serialisation techniques? 您是否考虑过使用使用更复杂的序列化技术的开源通信库

Using networkcomms.net your send method would become (you just provide the RAW object and it does all of the serialisation for you): 使用networkcomms.net,您的send方法将变为(您只提供RAW对象,它会为您完成所有序列化):

NetworkComms.SendObject("DataPacket", ipAddress, port, object);

On the receive end you code would be: 在接收端,您的代码将是:

NetworkComms.AppendGlobalIncomingPacketHandler<object>("DataPacket", (header, connection, incomingObject) => { object mydat = incomingObject; });
TCPConnection.StartListening();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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