我在网上找了一个例子。
前面的握手能成功,发送也可以成功,但是发送的时候如果长度太大就无法发送。
想问一下是为什么?感觉他是在原来的前面加了报头不这是什么,请高手指导一下,谢谢。
sc.Send(PackData(msg));
        /// <summary>
        /// 打包服务器数据
        /// </summary>
        /// <param name="message">数据</param>
        /// <returns>数据包</returns>
        private static byte[] PackData(string message)
        {
            byte[] contentBytes = null;
            byte[] temp = Encoding.UTF8.GetBytes(message);            if (temp.Length < 126)
            {
                contentBytes = new byte[temp.Length + 2];
                contentBytes[0] = 0x81;
                contentBytes[1] = (byte)temp.Length;
                Array.Copy(temp, 0, contentBytes, 2, temp.Length);
            }
            else if (temp.Length < 0xFFFF)
            {
                contentBytes = new byte[temp.Length + 4];
                contentBytes[0] = 0x81;
                contentBytes[1] = 126;
                contentBytes[2] = (byte)(temp.Length & 0xFF);
                contentBytes[3] = (byte)(temp.Length >> 8 & 0xFF);
                Array.Copy(temp, 0, contentBytes, 4, temp.Length);
            }
            else
            {
                // 暂不处理超长内容  
            }            return contentBytes;
        }

解决方案 »

  1.   

    代码中不是写了嘛,<126,< 0xFFFF, 暂不处理超长内容>0xFFFF 时contentBytes= null了
      

  2.   

    这个规则应该在你们程序通讯协议里自己规定的,你若不知道协议,你可以这样测试一下,看看对付解析会不会成功/// <summary>
            /// 打包服务器数据
            /// </summary>
            /// <param name="message">数据</param>
            /// <returns>数据包</returns>
            private static byte[] PackData(string message)
            {
                byte[] contentBytes = null;
                byte[] temp = Encoding.UTF8.GetBytes(message);            if (temp.Length < 126)
                {
                    contentBytes = new byte[temp.Length + 2];
                    contentBytes[0] = 0x81;
                    contentBytes[1] = (byte)temp.Length;
                    Array.Copy(temp, 0, contentBytes, 2, temp.Length);
                }
                else if (temp.Length < 0xFFFF)
                {
                    contentBytes = new byte[temp.Length + 4];
                    contentBytes[0] = 0x81;
                    contentBytes[1] = 126;
                    contentBytes[2] = (byte)(temp.Length & 0xFF);
                    contentBytes[3] = (byte)(temp.Length >> 8 & 0xFF);
                    Array.Copy(temp, 0, contentBytes, 4, temp.Length);
                }
                else
                {
                   contentBytes =  temp;
                }                     return contentBytes;
            }
      

  3.   

    试了你的方式,不行那我还不如直接返回temp呢,何必要拷贝呢?
      

  4.   

    貌似,<126 时 ,数据包后面加两位校验位,,<0xFFFF 时 ,数据包后面加4位校验位,用途应该是用来判读数据的准确性完整性的
      

  5.   


    WEBSOCKET 协议是根据包的长度,而设定不同的包头的
      

  6.   

    http://www.oursnet.net/ 这个就有可以发大容量的WebSocket了