Server端发送从数据库里查出的Dataset数据,已经用序列化转换为Byte类型,但是苦于不知道在同步的时候如果用分包来发送数据?
Socket服务端的的代码:
 #region 发送Socket包    byte[] SendByte = new byte[1024];//定义发送字节
    SendByte = GetBinaryFormatDataSet(Result);//调用序列化方法
    //int Scount = SendByte.Length / 1024;
   NetworkStream netWriteStream = new NetworkStream(accSock);
   netWriteStream.Write(SendByte, 0, SendByte.Length);   #endregion客户端代码:
 #region 接收Socket数据   byte[] ReceiveByte = new byte[1024];
  netStream = client.GetStream();
  //int len;//= ReceiveByte.Length;  //len = netStream.Read(ReceiveByte, 0, ReceiveByte.Length);
  recvStr = Encoding.UTF8.GetString(ReceiveByte).TrimEnd('\0');
  netStream.Close();
 #endregion

解决方案 »

  1.   

    你的意思是不是说序列化后的DATASET可能不止1024字节,所以要分包来发送,你想知道如何能分包发送吗?
      

  2.   

    To xu_2007 :是啊,序列化Dataset后大于1024,所以我不知道如何分包发送,所以求个例子,给个具体的代码看看
    谢谢
      

  3.   

    接收端:
    private void redataxx(messinfo mess,receiveinfo data)
            {
                int i = this.list.findlist<messinfo>(mess.msgcount, mess.msgtype);
                if (i == 9999)
                {
                    messinfo me = new messinfo();
                    me.msgcount = mess.msgcount;
                    me.msgcontext = new byte[mess.msgcount];
                    me.msgtype = mess.msgtype;
                    me.msgtypexx = mess.msgtypexx;
                    me.msgii = mess.msgii;
                    Buffer.BlockCopy(mess.msgcontext, 0, me.msgcontext, 0, mess.msgcontext.Length);
                    this.list.Add(me);
                }
                else
                {
                    Buffer.BlockCopy(mess.msgcontext, 0, this.list[i].msgcontext, this.list[i].msgii, mess.msgcontext.Length);
                    this.list[i].msgii = mess.msgii;
                    if (this.list[i].msgcontext.Length == this.list[i].msgii)
                    {
                        this.list[i].msgtypexx = 0;
                        this.list[i].msgtype = mess.msgtype;
                        data.buffer = Serialization.Binaryobjecttobyte<messinfo>(this.list[i]);
                        this.redata(data);
                        this.list.RemoveAt(i);   
                    }
                }
            }发送端:
      public static void Send(this TcpServer tcpserver, NetworkStream net,byte[] buff,int size,int type)
            {
                int count = buff.Length / size, sendsum = 0, offset = 0;
                if (buff.Length % size != 0)
                    count++;
                while (sendsum < count)
                {
                    byte[] buffer = new byte[size];
                    if (offset + buffer.Length > buff.Length)
                        buffer = new byte[buff.Length - offset];
                    Buffer.BlockCopy(buff, offset, buffer, 0,buffer.Length);
                   offset += buffer.Length;
                   sendsum++;
                    messinfo mess = new messinfo();
                    mess.msgcontext = buffer;
                    mess.msgcount = buff.Length;
                    mess.msgii = offset;
                    mess.msgtype = type;
                    mess.msgtypexx = 1;
                    tcpserver.send<messinfo>(net, mess);
                    Thread.Sleep(200);
                }
            }把特殊消息储存到集合的扩展方法:
    public static int findlist<T>(this List<T> list, int msgcount, int msgtype)where T:messinfo 
            {
                for (int i = 0; i < list.Count; i++)
                {
                    if (list[i].msgcount == msgcount && list[i].msgtype == msgtype)
                        return i;
                }
                return 9999;
            }其中接收端的MESS参数是消息数据包类,DATA是TCP接收事件传来的接收信息,里面包括与发送端通信的网络字节流对象等等,这些在你的程序中你需要根据自己的程序结构修改;这个分包发送的原理是如果要发送的包大于我定义的大小,比如1024字节,那么就把它定义为特殊消息包,所以在上面的发送端里面的MESS.MSGTYPEXX为1,这就说明是特殊消息包,那么在接收端的接收事件中要分析如果MESS.MSGTYPEXX为1的话则进入上面的接收端的代码区;相信上面的代码你应该能够看懂,如果有看不懂的再跟贴!
      

  4.   

    To xu_2007 : 我刚研究了下你的代码,有点深度啊,能加你的QQ或者MSN请教下吗?
      

  5.   

    查询一下MSDN里面有四个关于SOCKET的实例,很不错
      

  6.   

    建议楼主采用分次读取然后转存的方式:                int BufferSize = 1024;
                    byte[] buffer = new byte[BufferSize];
                    int bytesRead;          // 读取的字节数
                      MemoryStream msStream = new MemoryStream();
                    do
                    {
                        bytesRead = streamToClient.Read(buffer, 0, BufferSize);
                        msStream.Write(buffer, 0, bytesRead);
                    } while (bytesRead > 0);                buffer = msStream.GetBuffer();
                    string msg = Encoding.Unicode.GetString(buffer);这个应该能满足您的需要,这是server段接受数据的时候的情况,客户端类似哈.