可以在每次发送数据时,在数据的末尾添加固定的标识符,比如\r\n。
msg += '\r\n';
try {
await this.tcp.send({
data: msg
});
} catch (error) {
console.error(`send failed. ${index}`);
} 这样当解析对端传过来的数据时,首先把所有收到的数据都复制到接收缓冲区中,然后遍历缓冲区查找结束标志,找到后就把结束标志以前的部分作为完整数据包提取出来使用,剩余的部分复制到缓冲区首部进行下一次遍历,直到找不到结束标志为止,代码实现如下:
client.on('message', (value: socket.SocketMessageInfo) => {
// 把接收到的数据复制到缓冲区有效数据尾部
let copyCount = buffer.from(value.message).copy(this.receivedDataBuf, this.receivedDataLen);
// 缓冲区已使用长度加上本次接收的数据长度
this.receivedDataLen += copyCount;
// 缓冲区数据中数据包结束标志的位置
let endFlagPos = this.receivedDataBuf.subarray(0, this.receivedDataLen).indexOf(this.packetEndFlag);
let textDecoder = util.TextDecoder.create('utf-8');
while (endFlagPos > -1) {
// 把数据包结束标志前面的数据转换为字符串
let msgArray = new Uint8Array(this.receivedDataBuf.subarray(0, endFlagPos).buffer);
let msg = textDecoder.decodeToString(msgArray);
this.receivedMsg.push(msg);
// 剩余的未解析数据
let leaveBufData = this.receivedDataBuf.subarray(endFlagPos + 2, this.receivedDataLen);
// 剩余的未解析数据移动到缓冲区头部
for (let pos = 0; pos < leaveBufData.length; pos++) {
this.receivedDataBuf.writeUInt8(leaveBufData.readUInt8(pos), pos);
}
// 重新设置缓冲区已使用长度
this.receivedDataLen = leaveBufData.length;
// 开始查找下一个数据包结束标志
endFlagPos = this.receivedDataBuf.subarray(0, this.receivedDataLen).indexOf(this.packetEndFlag);
}
}); 通过将数据包的总长度声明在数据包的首部,例如前2位字节为固定的包长度,使用小端的16位无符号整数表示,后面是包内容:
let msg = index.toString();
let textEncoder = new util.TextEncoder();
let encodeValue = textEncoder.encodeInto(msg);
let sendBuf = buffer.alloc(2 + encodeValue.byteLength);
// 写入固定包头中的长度信息
sendBuf.writeUInt16LE(encodeValue.byteLength);
// 写入固定包头中的长度信息
sendBuf.write(msg, 2);
try {
await this.tcp.send({
data: sendBuf.buffer
});
} catch (error) {
console.error(`send failed. ${index}`);
} 接收方在读取数据时,就可以先读取前2位字节获取数据包的长度,然后再往后读取这个长度的字节流,就是一个完整的数据包。
client.on('message', (value: socket.SocketMessageInfo) => {
// 把接收到的数据复制到缓冲区有效数据尾部
let copyCount = buffer.from(value.message).copy(this.receivedDataBuf, this.receivedDataLen);
this.receivedDataLen += copyCount;
// 至少写入了3个字节才需要解析
if (this.receivedDataLen < 3) {
return;
}
// 当前数据包长度
let packLen = this.receivedDataBuf.readUInt16LE();
let textDecoder = util.TextDecoder.create('utf-8');
// 当前数据包长度加上固定包体的2字节,如果小于等于缓冲区已使用长度,就可以解析
while ((packLen + 2) <= this.receivedDataLen) {
// 把可变包体中的数据转换为字符串
let msgArray = new Uint8Array(this.receivedDataBuf.subarray(2, packLen + 2).buffer);
let msg = textDecoder.decodeToString(msgArray);
this.receivedMsg.push(msg);
// 剩余的未解析数据
let leaveBufData = this.receivedDataBuf.subarray(packLen + 2, this.receivedDataLen);
// 剩余的未解析数据移动到缓冲区头部
for (let pos = 0; pos < leaveBufData.length; pos++) {
this.receivedDataBuf.writeUInt8(leaveBufData.readUInt8(pos), pos);
}
// 重新设置缓冲区已使用长度
this.receivedDataLen = leaveBufData.length;
// 至少写入了3个字节才需要解析,否则跳出循环
if (this.receivedDataLen < 3) {
break;
}
// 开始查找下一个固定包头中的可变包体长度
packLen = this.receivedDataBuf.readUInt16LE();
}
}); 实现TcpSocket通信,首先需要申请ohos.permission.INTERNET权限,完整代码如下:
import { socket } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { buffer, util } from '@kit.ArkTS';
@Entry
@Component
struct SocketCommunication {
private tcp: socket.TCPSocket = socket.constructTCPSocketInstance();
private tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance();
@State receivedMsg: string[] = [];
// 数据包结束标志
packetEndFlag: string = '\r\n';
// 最大缓存长度
maxBufSize: number = 1024 * 8;
// 接收数据缓冲区
receivedDataBuf: buffer.Buffer = buffer.alloc(this.maxBufSize);
// 缓冲区已使用长度
receivedDataLen: number = 0;
build() {
Column({ space: 20 }) {
Button('init TCP Server')
.fontSize(24)
.fontWeight(FontWeight.Bold)
.onClick(() => {
let listenAddr: socket.NetAddress = {
address: 'localhost',
port: 9588,
family: 1
};
this.tcpServer.listen(listenAddr, (err: BusinessError) => {
if (err) {
console.error('listen fail', JSON.stringify(err));
return;
}
console.info('listen success');
this.getUIContext().getPromptAction().showToast({ message: 'listen success' });
});
});
Button('方案一:固定标识符作为尾部')
.fontSize(24)
.fontWeight(FontWeight.Bold)
.onClick(() => {
this.tcpServer.on('connect', (client: socket.TCPSocketConnection) => {
console.info('client connected');
client.on('close', () => {
console.info('on close success');
});
client.on('message', (value: socket.SocketMessageInfo) => {
// 把接收到的数据复制到缓冲区有效数据尾部
let copyCount = buffer.from(value.message).copy(this.receivedDataBuf, this.receivedDataLen);
// 缓冲区已使用长度加上本次接收的数据长度
this.receivedDataLen += copyCount;
// 缓冲区数据中数据包结束标志的位置
let endFlagPos = this.receivedDataBuf.subarray(0, this.receivedDataLen).indexOf(this.packetEndFlag);
let textDecoder = util.TextDecoder.create('utf-8');
while (endFlagPos > -1) {
// 把数据包结束标志前面的数据转换为字符串
let msgArray = new Uint8Array(this.receivedDataBuf.subarray(0, endFlagPos).buffer);
let msg = textDecoder.decodeToString(msgArray);
this.receivedMsg.push(msg);
// 剩余的未解析数据
let leaveBufData = this.receivedDataBuf.subarray(endFlagPos + 2, this.receivedDataLen);
// 剩余的未解析数据移动到缓冲区头部
for (let pos = 0; pos < leaveBufData.length; pos++) {
this.receivedDataBuf.writeUInt8(leaveBufData.readUInt8(pos), pos);
}
// 重新设置缓冲区已使用长度
this.receivedDataLen = leaveBufData.length;
// 开始查找下一个数据包结束标志
endFlagPos = this.receivedDataBuf.subarray(0, this.receivedDataLen).indexOf(this.packetEndFlag);
}
});
});
let netAddress: socket.NetAddress = {
address: 'localhost',
port: 9588
};
let tcpConnectOptions: socket.TCPConnectOptions = {
address: netAddress,
timeout: 6000
};
this.tcp.connect(tcpConnectOptions, async () => {
console.info('connect success');
for (let index = 0; index < 100; index++) {
let msg = index.toString();
msg += '\r\n';
try {
await this.tcp.send({
data: msg
});
} catch (error) {
console.error(`send failed. ${index}`);
}
}
});
});
Button('方案二:头部标识长度')
.fontSize(24)
.fontWeight(FontWeight.Bold)
.onClick(() => {
this.tcpServer.on('connect', (client: socket.TCPSocketConnection) => {
console.info('client connected');
client.on('close', () => {
console.info('on close success');
});
client.on('message', (value: socket.SocketMessageInfo) => {
// 把接收到的数据复制到缓冲区有效数据尾部
let copyCount = buffer.from(value.message).copy(this.receivedDataBuf, this.receivedDataLen);
this.receivedDataLen += copyCount;
// 至少写入了3个字节才需要解析
if (this.receivedDataLen < 3) {
return;
}
// 当前数据包长度
let packLen = this.receivedDataBuf.readUInt16LE();
let textDecoder = util.TextDecoder.create('utf-8');
// 当前数据包长度加上固定包体的2字节,如果小于等于缓冲区已使用长度,就可以解析
while ((packLen + 2) <= this.receivedDataLen) {
// 把可变包体中的数据转换为字符串
let msgArray = new Uint8Array(this.receivedDataBuf.subarray(2, packLen + 2).buffer);
let msg = textDecoder.decodeToString(msgArray);
this.receivedMsg.push(msg);
// 剩余的未解析数据
let leaveBufData = this.receivedDataBuf.subarray(packLen + 2, this.receivedDataLen);
// 剩余的未解析数据移动到缓冲区头部
for (let pos = 0; pos < leaveBufData.length; pos++) {
this.receivedDataBuf.writeUInt8(leaveBufData.readUInt8(pos), pos);
}
// 重新设置缓冲区已使用长度
this.receivedDataLen = leaveBufData.length;
// 至少写入了3个字节才需要解析,否则跳出循环
if (this.receivedDataLen < 3) {
break;
}
// 开始查找下一个固定包头中的可变包体长度
packLen = this.receivedDataBuf.readUInt16LE();
}
});
});
let netAddress: socket.NetAddress = {
address: 'localhost',
port: 9588
};
let tcpConnectOptions: socket.TCPConnectOptions = {
address: netAddress,
timeout: 6000
};
this.tcp.connect(tcpConnectOptions, async () => {
for (let index = 0; index < 100; index++) {
let msg = index.toString();
let textEncoder = new util.TextEncoder();
let encodeValue = textEncoder.encodeInto(msg);
let sendBuf = buffer.alloc(2 + encodeValue.byteLength);
// 写入固定包头中的长度信息
sendBuf.writeUInt16LE(encodeValue.byteLength);
// 写入固定包头中的长度信息
sendBuf.write(msg, 2);
try {
await this.tcp.send({
data: sendBuf.buffer
});
} catch (error) {
console.error(`send failed. ${index}`);
}
}
});
});
Column() {
Scroll() {
Column() {
ForEach(this.receivedMsg, (item: string) => {
Text(item)
.textAlign(TextAlign.Center)
.width('100%')
.fontSize(24);
});
};
};
}
.border({ width: 1 })
.width('90%')
.height('75%');
}
.margin({ top: 20 })
.height('100%')
.width('100%');
}
}