实验客户端//结构体
    [Serializable()]
    struct ServerData
    {
        public string file_repospath; 
        public string type;  
    }        /// <summary> 
        /// 反序列化 
        /// </summary> 
        /// <param name="data">数据缓冲区</param> 
        /// <returns>对象</returns> 
        public static object Deserialize(byte[] data)
        {
            BinaryFormatter formatter = new BinaryFormatter();
            MemoryStream rems = new MemoryStream(data);
            data = null;
            return formatter.Deserialize(rems);
        }
        /// <summary> 
        /// 序列化 
        /// </summary> 
        /// <param name="data">要序列化的对象</param> 
        /// <returns>返回存放序列化后的数据缓冲区</returns> 
        public static byte[] Serialize(object data)
        {
            BinaryFormatter formatter = new BinaryFormatter();
            MemoryStream rems = new MemoryStream();
            formatter.Serialize(rems, data);
            return rems.GetBuffer();
        }
        static void Main(string[] args)
        {
            //发送库信息到服务端
            ServerData m_data; 
            m_data.type = "A";
            m_data.iscalflag = false;
            m_data.completeflag = false;
            m_data.file_path = "3";
            m_data.pre_file_content = "4";
            m_data.file_repospath = "1111111111";
            m_data.file_content = "222222222222222";
            //客户端调试调试序列换与反序列化,正常
            Byte[] nsendBytes = Serialize(m_data);
            Object oObjTemp = Deserialize(nsendBytes);
            ServerData ceshi = (ServerData)oObjTemp;
            System.Console.WriteLine(ceshi.file_repospath);
            System.Console.WriteLine(ceshi.file_content);
            //
            TcpClient mClient = new TcpClient("10.78.20.113", 12096);
            //发到服务端
            if (mClient != null)
            {
                System.Console.WriteLine("按任意键发送数据");
                System.Console.ReadKey();
                NetworkStream ns = mClient.GetStream();
                Byte[] sendBytes = Serialize(m_data);
                ns.Write(sendBytes, 0, sendBytes.Length);
                ns.Flush();
             }服务端代码//结构体
    [Serializable()]
    struct ServerData
    {
        public string file_repospath; 
        public string type;  
    }        /// <summary> 
        /// 反序列化 
        /// </summary> 
        /// <param name="data">数据缓冲区</param> 
        /// <returns>对象</returns> 
        public static object Deserialize(byte[] data)
        {
            BinaryFormatter formatter = new BinaryFormatter();
            MemoryStream rems = new MemoryStream(data);
            data = null;
            return formatter.Deserialize(rems);
        }
        /// <summary> 
        /// 序列化 
        /// </summary> 
        /// <param name="data">要序列化的对象</param> 
        /// <returns>返回存放序列化后的数据缓冲区</returns> 
        public static byte[] Serialize(object data)
        {
            BinaryFormatter formatter = new BinaryFormatter();
            MemoryStream rems = new MemoryStream();
            formatter.Serialize(rems, data);
            return rems.GetBuffer();
        }//接收部分如下:
                            byte[] bytes = new byte[2048];
                            int bytesread = 0;
                            byte[] mSumBytes = null;
                            //StringBuilder m_CompleteMessage = new StringBuilder();
                            do
                            {
                                bytesread = ns.Read(bytes, 0, bytes.Length);
                                mSumBytes = AddBytes(mSumBytes, bytes, bytesread);
                            }
                            while (ns.DataAvailable);
                            ns.Flush();
                            //序列化结构体对象
                            Object ObjTemp = Deserialize(mSumBytes);//这里错了我对比了下客户端序列化发送的 byte[]和服务端收到的byte[]是一样的,同样的数据在客户端反序列化就没有问题,在服务端在调用反序列化的时候就错了。

解决方案 »

  1.   

    AddBytes是我自己写的函数,来叠加接收到的字节流内容
            //叠加2个byte[]:b1实际字符是满的,b2的实际字符是b2_real_data_size
            static byte[] AddBytes(byte[] b1, byte[] b2, int b2_real_data_size)
            {
                if (b1 == null)
                {
                    byte[] newbyte = new byte[b2_real_data_size];
                    Array.Copy(b2, 0, newbyte, 0, b2_real_data_size);
                    return newbyte;
                }
                else
                {
                    int bytelong = b1.Length + b2_real_data_size;
                    byte[] addsum = new byte[bytelong];
                    Array.Copy(b1, 0, addsum, 0, b1.Length);
                    Array.Copy(b2, 0, addsum, b1.Length, b2_real_data_size);
                    return addsum;
                }
            }
      

  2.   

    ServerData结构所在的项目名称、namespace等客户端与服务端要一致
      

  3.   

    你确定发送数据与接收到的数据时一样的?
    组合数组用 Buffer.BlockCopy();试试
      

  4.   

    是的,一共发送和接受200个字节,我一个一个对着看了的。反序列化时在语句return formatter.Deserialize(rems);发生异常
    异常的第一句居然是:
    无法找到程序集“ConsoleApplication1Client, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null”。也就是说客户端的一些序列化信息,服务器无法识别。
      

  5.   

    ConsoleApplication1Client是我客户端的工程名称
      

  6.   

    二进制序列化,被序列化的实例的类型信息以及数据都一起被序列化,
    2#已经告诉要怎么做了.将ServerData定义到一个独立class library,服务端和客户端引用同一个dll
    确保namespace以及其他的必要信息都相同