mirror of
https://gitlab.com/wirelos/sprocket-lib.git
synced 2025-12-16 05:24:30 +01:00
Resolve "OTA"
This commit is contained in:
45
tools/tcp_example.js
Normal file
45
tools/tcp_example.js
Normal file
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
In the node.js intro tutorial (http://nodejs.org/), they show a basic tcp
|
||||
server, but for some reason omit a client connecting to it. I added an
|
||||
example at the bottom.
|
||||
Save the following server in example.js:
|
||||
*/
|
||||
|
||||
var net = require('net');
|
||||
|
||||
var server = net.createServer(function(socket) {
|
||||
socket.write('Echo server\r\n');
|
||||
socket.pipe(socket);
|
||||
});
|
||||
|
||||
server.listen(1337, '127.0.0.1');
|
||||
|
||||
/*
|
||||
And connect with a tcp client from the command line using netcat, the *nix
|
||||
utility for reading and writing across tcp/udp network connections. I've only
|
||||
used it for debugging myself.
|
||||
$ 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.) */
|
||||
|
||||
var net = require('net');
|
||||
|
||||
var 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);
|
||||
client.destroy(); // kill client after server's response
|
||||
});
|
||||
|
||||
client.on('close', function() {
|
||||
console.log('Connection closed');
|
||||
});
|
||||
Reference in New Issue
Block a user