服务器将未固定的字节数组从一个客户端传递到另一个客户端 [英] Server passing unfixed byte array from one client to another

查看:111
本文介绍了服务器将未固定的字节数组从一个客户端传递到另一个客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请一些帮助,虽然我对C#不太了解

我需要让服务器执行以下操作:
将字节数组从一个客户端传递到另一个客户端

我不知道如何在服务器中保存字节数组(我不知道大小,即灵活的大小)以将其发送到第二个客户端的问题

我有这段代码将保留字节数组另存为具有固定大小的字节数组

some help please , while I don''t know much about C#

I need to make server do the following :
passing byte array from one client to another

the problem I dont know how to save the byte array ( i dont know the size ,i.e flexible size) in the server to send it to second client

I have this code to save the reserve byte array as a byte array with fixed size

private void HandleClient(object client)
       {
           TcpClient tcpClient = (TcpClient)client;
           NetworkStream clientStream = tcpClient.GetStream();

           int bytesRead;

           while (true)
           {
               bytesRead = 0;

               try
               {

                   //blocks until a client sends a message
                   bytesRead = clientStream.Read(message, 0, 4096);
               }
               catch
               {
                   //a socket error has occured
                   break;
               }
               if (bytesRead == 0)
               {
                   //the client has disconnected from the server
                   break;
               }
           }
           tcpClient.Close();
       }




如何将字节数组保存在streem中,以便同时通过服务器从第一个客户端再次发送到第二个客户端?




how I can save the byte array in streem to send again at the same time from first client to second client through the server?

推荐答案

您需要的称为序列化.请开始了解它:
http://en.wikipedia.org/wiki/Serialization [ http://msdn.microsoft.com/en-us/library/7ay27kt9%28v = vs.100%29.aspx [ ^ ].

有几种方法可以序列化同一对象.

即使序列化可以与使用您在代码示例中显示的签名传递的对象一起使用,也可以以对(编译时)对象System.Object实例的引用的形式进行,这就是序列化器/格式化程序所使用的,通常,需要专门准备将对象序列化.类型和/或成员应标记有特殊属性,否则类型可以实现ISerializable接口;也就是说,类型和成员应支持序列化或描述数据协定.

但是,.NET中的序列化思想是基于反射的.序列化程序查找类型的meta-data,以找出应该序列化哪些成员,以及如何递归分析成员的类型.由于反射操作相对昂贵,因此可以通过System.Reflection.Emit快速解决生成串行化程序集的性能问题.并且这对于用户来说是完全透明的,但是用户可以通过项目属性决定使用此功能.当然,只有相同的元数据用于序列化的次数超过一次(通常是这种情况),它才有回报.

最有可能的是,您将使用二进制格式化程序System.Runtime.Serialization.Formatters.Binary.BinaryFormatter,请参见:
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.formatters.binary.binaryformatter.aspx [ http://msdn.microsoft.com/en-us/library/system.bitconverter.aspx [^ ].

可以使用ToBytesToChars方法使用类型System.Text.Encoding对字符串进行序列化,请参见:
http://msdn.microsoft.com/en-us/library/system.text. encoding.aspx [^ ].

为避免数据丢失,请使用Unicode UTF之一,最好使用UTF-8.

另请参阅我最近的回答以及一些详细信息和链接:
我应该使用数据类型来阅读中文和英文字符 [ ^ ].

而且,通过这种手动序列化,您可以显着提高性能.

—SA
What you need is called serialization. Please start learning about it:
http://en.wikipedia.org/wiki/Serialization[^],
http://msdn.microsoft.com/en-us/library/7ay27kt9%28v=vs.100%29.aspx[^].

There are several ways to serialize the same object.

Even though serialization can work with the object passed using the signature you show in your code sample, in the form of the reference to the instance of the (compile-time) object System.Object, and this is what serializers/formatters work with, normally it required that the object would be specially prepared to be serialized. The types and/or members should be marked with special attributes, or a type can implement ISerializable interface; that is, the types and members should support serialization or describe a data contract.

Nevertheless, the idea of serialization in .NET is based on Reflection. The serializer looks up the type''s meta-data to find out what members should be serialized and how, throw analyzing the types of the members, recursively. As Reflection operations are relatively expensive, the performance problem is solved throw generation of serialization assembly on the fly through System.Reflection.Emit; and this is done in a way completely transparent for the user, but the user can decide to use this feature through the project properties. Of course, it only pays off if the same meta-data is used for serialization more than once, which is often the case.

Most likely, you will use the binary formatter System.Runtime.Serialization.Formatters.Binary.BinaryFormatter, please see:
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.formatters.binary.binaryformatter.aspx[^].

For completeness, I want to say that you can serialize or deserialize objects of some simple types manually member by member, using serialization of primitive-type members using the class System.BitConverter.
http://msdn.microsoft.com/en-us/library/system.bitconverter.aspx[^].

Strings can be serialized using the type System.Text.Encoding using the methods ToBytes and ToChars, please see:
http://msdn.microsoft.com/en-us/library/system.text.encoding.aspx[^].

To avoid loss of data, use one of Unicode UTF''s, preferably UTF-8.

Please also see my recent answer with some detail and links:
data type should i use to read chinese and english characters[^].

Also, with such manual serialization you can considerably increase performance.

—SA


这篇关于服务器将未固定的字节数组从一个客户端传递到另一个客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆