1. OkSocket framework message structure
@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();
}
}
According to the above code, the length value of the message string in the format of the enlarged end of the first 4 bytes of the message body is required, and then the message string is added
2. Corresponding C# code
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. Sticky package processing (there is no scenario to trigger the test, just write according to the solution idea)
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);
}
}