Created
June 4, 2019 05:50
-
-
Save zzzgit/58b28f35f87d7558273521a9ba383ea2 to your computer and use it in GitHub Desktop.
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
/* | |
And connect with a tcp client from the command line using netcat, the *nix | |
utility for reading and writing across tcp/udp network connections. | |
$ netcat 127.0.0.1 1337 | |
You should see: | |
> Echo server | |
*/ | |
/* Or use this example tcp client written in node.js. (Originated with | |
example code from | |
http://www.hacksparrow.com/tcp-socket-programming-in-node-js.html.) */ | |
const net = require('net') | |
const client = new net.Socket() | |
client.connect(1337, '127.0.0.1', function () { | |
console.log('Connected') | |
client.write('Hello, server! Love, Client.') | |
}) | |
client.on('data', function (data) { | |
console.log('Received: ' + data.constructor.name) | |
client.destroy() // kill client after server's response | |
}) | |
client.on('close', function () { | |
console.log('Connection closed') | |
}) |
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 server = net.createServer(function (socket) { | |
console.log('連接了:', socket.remotePort) | |
socket.on("data", (buffer)=>{ | |
console.log(buffer.toString()) | |
socket.write(buffer) | |
}) | |
socket.on("end", ()=>{ | |
console.log("end 了") | |
}) | |
}) | |
server.listen(1345, '127.0.0.1') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment