Skip to content

Instantly share code, notes, and snippets.

@ikouchiha47
Last active September 2, 2016 13:38
Show Gist options
  • Save ikouchiha47/b7de7801608e9d584c944168cbc3ac70 to your computer and use it in GitHub Desktop.
Save ikouchiha47/b7de7801608e9d584c944168cbc3ac70 to your computer and use it in GitHub Desktop.
parsing tcp data
const net = require('net')
const DELIMT = '----AxEB04'
// create a server, when the data arrives, push it to the existing buffer
// convert to string to check wether the delimiter is present
// if not, message is still not completed
// if yes, split the data
function parseDelimitedMessage() {
let buff = new Buffer('');
let server = net.createServer(c => {
c.on('data', function(d) {
buff = Buffer.concat([buff, d])
let data = buff.toString('utf8')
let index = data.indexOf(DELIMT)
if(index > -1) {
buff = buff.slice(0, index)
console.log("Previous Message received", buff.toString('utf8'))
buff = new Buffer(data.slice(index + DELIMT.length))
console.log("New Message started")
} else {
console.log("Reciving Previous Message")
}
})
}).listen(5000, () => {
console.log('listening')
})
}
//here when we have a message we send the length of the incomming message,
// in the next messages have a copy of the total length and recieve the streams upto that length
// rest will be a new message, so create a new Buffer
function parseLengthMessage() {
let len = 0;
let buff;
let server = net.createServer(c => {
c.on('data', data => {
if(!len) {
len = data.readUIntBE(0, 4)
buff = Buffer.from(data.slice(4))
}
if(buff.length + data.length >= len) {
let diff = len - buff.length
// hopefully slice slices bytes/octets
buff = Buffer.concat([buff, data.slice(0, diff)])
console.log(buff.toString('utf8'), "message")
// not set new length
len = data.slice(diff).readUIntBE(0, 4);
buff = Buffer.from(data.slice(diff + 4))
} else {
buff = Buffer.concat([buff, data])
}
})
}).listen(5000, () => {})
}
function generateRandomSentence(totLen, sentLength) {
totLen = totLen || 500
const alphabets = 'abcdefghijklmnopqrstuvwxyz'
let wordSplit = 5
let sentence = '';
for(let i = 0; i < totLen; i++) {
let start = Math.floor((Math.random() * 100) % 21)
let word = alphabets.slice(start, start + wordSplit)
sentence += (word + ( sentence.length % sentLength == 0 ? "\n" : " "))
}
return sentence
}
function sendMessageWithLength(client, message) {
console.log('sending', message, '---')
let buff = Buffer.alloc(4)
let hex = message.length.toString(16)
if(hex.length % 2 !== 0) hex = '0' + hex
buff.writeUIntBE(`0x${hex}`, 0, 4)
client.write(buff)
client.write(Buffer.from(message, 'utf8'))
}
function sendMessageWithDelimit(client, message) {
console.log(message + DELIMT)
let buffer = new Buffer(message + DELIMT)
client.write(buffer)
}
//parseDelimitedMessage()
parseLengthMessage()
let client = net.connect({port: 5000, address: '192.168.1.131'}, () => {
console.log('connected')
for(let i = 1; i <= 5; i++) {
setTimeout(() => {
sendMessageWithLength(client, generateRandomSentence(5, 4))
//sendMessageWithDelimit(client, generateRandomSentence(5, 4))
}, 1000 * i)
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment