-
-
Save raymondlaw/54c851ea73b2e973924fedc485c52a11 to your computer and use it in GitHub Desktop.
TCP Transmission Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const net = require("net"); | |
const crypto = require('crypto'); | |
const server = net.createServer(connection); | |
function connection(socket){ | |
let segments_received = 0; | |
socket.on('data', (data) => { | |
segments_received++; | |
console.log(`Received : ${data.length} Bytes`); | |
if(segments_received === 3){ | |
socket_write(socket, [300,200,100]); | |
} | |
}); | |
socket.on('end', (data) => { | |
console.log("Received FIN"); | |
socket.end(() => process.exit()); | |
}); | |
} | |
function socket_write(socket, bytes = []){ | |
if(bytes.length === 0){ | |
console.log("Sending FIN"); | |
socket.end(); | |
return; | |
} | |
let towrite = bytes.shift(); | |
console.log(`A Sending ${towrite} Bytes`); | |
socket.write(crypto.randomBytes(towrite), | |
() => setTimeout(() => socket_write(socket, bytes), 1000)); | |
} | |
server.listen(35555, () => console.log('server bound')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const net = require("net"); | |
const crypto = require('crypto'); | |
const client = net.createConnection(35555, connection) | |
function connection(){ | |
let segments_received = 0; | |
this.on('end', (data) => { | |
console.log("Received FIN"); | |
setTimeout(()=>{ | |
console.log("Sending FIN"); | |
this.end(); | |
},1000); | |
}); | |
this.on('data', (data) => { | |
segments_received++; | |
console.log(`Received : ${data.length} Bytes`); | |
}); | |
socket_write(this,[100,200,300]); | |
} | |
function socket_write(socket, bytes = []){ | |
if(bytes.length === 0) | |
return; | |
let towrite = bytes.shift(); | |
console.log(`B Sending ${towrite} Bytes`); | |
socket.write(crypto.randomBytes(towrite), | |
() => setTimeout(() => socket_write(socket, bytes), 1000)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment