C# 给OkSocket服务端发送消息(封包,拆包,粘包)

1.OkSocket框架消息结构

 @Override
  public byte[] parse() {
    //根据服务器的解析规则,构建byte数组
    byte[] body = str.getBytes(Charset.defaultCharset());
    ByteBuffer bb = ByteBuffer.allocate(4 + body.length);
    bb.order(ByteOrder.BIG_ENDIAN);
    bb.putInt(body.length);
    bb.put(body);
    return bb.array();
  }
}

根据上面代码,需要消息体前4个字节放大端格式的消息字符串长度值,然后再加上消息字符串

2.对应的C#代码

string strMsg = this.tbSendMsg.Text.Trim();
                byte[] data = new byte[4 + strMsg.Length];

                var head = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(strMsg.Length));
                var body = Encoding.UTF8.GetBytes(strMsg);

                Array.Copy(head, 0, data, 0, head.Length);
                Array.Copy(body, 0, data, head.Length, body.Length);

3.粘包处理(未能有场景触发测试,只是按照解决方案思路编写)

while (true)
                {
                    byte[] buffer = new byte[2048];
                    // 接收的字节
                    int r = socketSend.Receive(buffer); // 阻塞操作

                    if (r == 0)
                    {
                        return;
                    }

                    // 拼接上次多余字节
                    if (moreReciveData?.Length > 0)
                    {
                        Array.Copy(moreReciveData, 0, buffer, 0, moreReciveData.Length);
                    }

                    // OkSocket 前4个字节放消息体长度
                    if (r < 4)
                    {
                        continue;
                    }

                    // 获取消息体长度
                    byte[] rHead = new byte[4];
                    Array.Copy(buffer, 0, rHead, 0, 4);
                    var bodyLength = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(rHead, 0));

                    // 消息体缺失
                    if (4 + bodyLength > r)
                    {
                        this.tbLog.Invoke(receiveMsgCallBack, "服务端:" + socketSend.RemoteEndPoint + " 接收消息内容不够长度");
                        continue;
                    }
                    else if (4 + bodyLength == r) // 刚好接收一个消息长度
                    {
                        string response = Encoding.UTF8.GetString(buffer, 4, bodyLength);
                        this.tbLog.Invoke(receiveMsgCallBack, "服务端:" + socketSend.RemoteEndPoint + " 接收消息:" + response);
                    }
                    else if (4 + bodyLength < r) // 接收超过一个消息长度
                    {
                        string response = Encoding.UTF8.GetString(buffer, 4, bodyLength);
                        this.tbLog.Invoke(receiveMsgCallBack, "服务端:" + socketSend.RemoteEndPoint + " 接收消息:" + response);

                        var more = Encoding.UTF8.GetString(buffer, 4 + bodyLength, r - 4 - bodyLength);
                        this.tbLog.Invoke(receiveMsgCallBack, "服务端:" + socketSend.RemoteEndPoint + " 接收多余的消息:" + more);
                        moreReciveData = new byte[r - 4 - bodyLength];
                        Array.Copy(buffer, 4 + bodyLength, moreReciveData, 0, r - 4 - bodyLength);
                    }
                }

4.GitHub代码

https://github.com/harrylsp/AndroidADBDriver

你可能感兴趣的:(网络编程,c#,Socket,OkSocket,粘包,拆包)