tcp 收发数据demo(带粘包处理解决方案)
发布于 8 年前 作者 lvgithub 5446 次预览 最后一次回复是 8 年前 来自 分享
项目地址: https://github.com/lvgithub/stick 进入example目录,1、运行server.js 2、运行client.js即可测试demo
server
const net = require('net')
const stick_package = require('../index')
let tcp_server = net.createServer(function (socket) {
socket.stick = new stick_package(1024).setReadIntBE('32')
socket.on('data', function (data) {
socket.stick.putData(data)
})
socket.stick.onData(function (data) {
// 解析包头长度
let head = new Buffer(4)
data.copy(head, 0, 0, 4)
// 解析数据包内容
let body = new Buffer(head.readInt32BE())
data.copy(body, 0, 4, head.readInt32BE())
console.log('data length: ' + head.readInt32BE())
console.log('body content: ' + body.toString())
})
socket.stick.onError(function (error) {
console.log('stick data error:' + error)
})
socket.on('close', function (err) {
console.log('client disconnected')
})
})
tcp_server.on('error', function (err) {
throw err
})
tcp_server.listen(8080, function () {
console.log('tcp_server listening on 8080')
})
client
const net = require('net')
const client = net.createConnection({ port: 8080, host: '127.0.0.1' }, function () {
let body = Buffer.from('username=123&password=1234567,qwe')
// 写入包头
let headBuf = new Buffer(4)
headBuf.writeUInt32BE(body.byteLength, 0)
console.log('data length: ' + headBuf.readInt32BE())
// 发送包头
client.write(headBuf)
// 发送包内容
client.write(body)
console.log('data body: ' + body.toString())
})
client.on('data', function (data) {
console.log(data.toString())
})
client.on('end', function () {
console.log('disconnect from server')
})
6 回复
up
TCP挺多框架的。着几年一直用的pomelo这些都不用在处理了。 贴一个5 6年前自己写的 -----------------------------------------------------------------------package_system.js------------------------------------------------------------------------------------------------- function exPackag(aCallback) { this.mBuffer = new Array();// 缓冲容器 this.mBuffer_len = 0;// 协议长度 this.mData_len_Median = 2;// 协议的长度所占的位数 this.mCallback = aCallback;// 回调函数 }
exPackag.prototype.putData = function(aData) { //mLogger.ConsoleLog("PS putData: " + aData.length); if (this.mBuffer_len == 0) { if (aData.length < this.mData_len_Median) { mLogger.SetDebug(‘包头不全直接忽略’); return; } else { this.mBuffer_len = getIntToByte16(aData) + this.mData_len_Median; } }
};
function getIntToByte16(aRecv) { var targets = (aRecv[0] & 0xff) | ((aRecv[1] << 8) & 0xff00); return targets; }
exports.exPackag = exPackag; -----------------------------------------------------------------------game_index.js------------------------------------------------------------------------------------------------- function GameMain(aPort,aCallback){
}
exports.GameMain = GameMain;
谢谢楼主,这个包很好用。给你star !
@jude90 谢谢支持,欢迎一起维护升级
https://github.com/lvgithub/stick 更新demo
up