Net

IoT.js provides asynchronous networking through Net module. You can use this module with require('net') and create both servers and clients.

net.connect(options[, connectListener])

  • options {Object} An object which specifies the connection options.
  • connectListener {Function} Listener for the 'connect' event.
  • Returns {net.Socket}.

Creates a new net.Socket and automatically connects with the supplied options. The options object specifies the following information:

  • port {number} Port connect to (required).
  • host {string} Host connect to (optional, Default: localhost).
  • family {number} Version of IP stack.

The options are passed to both the net.Socket constructor and the socket.connect method. The connectListener is automatically registered as a 'connect' event listener.

Example

  1. var net = require('net');
  2. var port = 22702;
  3. var echo_msg = '';
  4. var socket = net.connect({port: port, family: 4}, function() {
  5. socket.end('Hello IoT.js');
  6. });
  7. socket.on('data', function(data) {
  8. echo_msg += data;
  9. });
  10. socket.on('end', function() {
  11. console.log(echo_msg);
  12. });

net.connect(port[, host][, connectListener])

  • port {number} Port the client should connect to.
  • host {string} Host the client should connect to. Default: localhost.
  • connectListener {Function} Listener for the 'connect' event.
  • Returns {net.Socket}.

Creates a new net.Socket and automatically connects to the supplied port and host. If host is omitted, localhost will be assumed. The connectListener is automatically registered as a 'connect' event listener.

Example

  1. var net = require('net');
  2. var port = 22702;
  3. var host = '127.0.0.1';
  4. var echo_msg = '';
  5. var socket = net.connect(port, host, function() {
  6. socket.end('Hello IoT.js');
  7. });
  8. socket.on('data', function(data) {
  9. echo_msg += data;
  10. });
  11. socket.on('end', function() {
  12. console.log(echo_msg);
  13. });

net.createConnection(options[, connectListener])

  • options {Object} An object which specifies the connection options.
  • connectListener {Function} Listener for the 'connect' event.
  • Returns {net.Socket}.

Creates a new net.Socket and automatically connects with the supplied options. The options are passed to both the net.Socket constructor and the socket.connect method. The options object specifies the following information:

  • port {number} Port connect to (required).
  • host {string} Host connect to (optional, Default: localhost).
  • family {number} Version of IP stack.

The connectionListener is automatically registered as a 'connect' event listener.

Example

  1. var net = require('net');
  2. var port = 80;
  3. var echo_msg = '';
  4. var socket = net.createConnection({port: port, family: 4}, function() {
  5. socket.end('Hello IoT.js');
  6. });
  7. socket.on('data', function(data) {
  8. echo_msg += data;
  9. });
  10. socket.on('end', function() {
  11. console.log(echo_msg);
  12. });

net.createConnection(port[, host][, connectListener])

  • port {number} Port the client should connect to.
  • host {string} Host the client should connect to. Default: localhost.
  • connectListener {Function} Listener for the 'connect' event.
  • Returns {net.Socket}.

Creates a new net.Socket and automatically connects to the supplied port and host. It is equivalent to new net.Socket() followed by socket.connect(). If host is omitted, localhost will be assumed. The connectionListener is automatically registered as a 'connect' event listener.

Example

  1. var net = require('net');
  2. var port = 22702;
  3. var host = '127.0.0.1';
  4. var echo_msg = '';
  5. var socket = net.createConnection(port, host, function() {
  6. socket.end('Hello IoT.js');
  7. });
  8. socket.on('data', function(data) {
  9. echo_msg += data;
  10. });
  11. socket.on('end', function() {
  12. console.log(echo_msg);
  13. });

net.createServer([options][, connectionListener])

  • options {Object} An object which specifies the connection options Default: { allowHalfOpen: false }.
  • connectListener {Function} Listener for the 'connection' event.
  • Returns {net.Server}.

Creates a TCP server according to options. The connectionListener is automatically registered as a 'connection' event listener. If allowHalfOpen is true, then the socket becomes non-readable, but still writable. You should call the socket.end() method explicitly.

Example

  1. var net = require('net');
  2. var port = 22702;
  3. var server = net.createServer(
  4. {
  5. allowHalfOpen: true
  6. },
  7. function(socket) {
  8. server.close();
  9. }
  10. );
  11. server.listen(port);
  12. server.on('connection', function(socket) {
  13. var msg = '';
  14. socket.on('data', function(data) {
  15. msg += data;
  16. });
  17. socket.on('end', function() {
  18. socket.end(msg);
  19. });
  20. });

Class: net.Server

This class is used to create a TCP or local server. You can create net.Server instance with net.createServer().

server.close([closeListener])

  • closeListener {Function} Listener for the 'close' event.

Stops listening new arriving connection. Server socket will be finally closed when all existing connections are closed, then emits 'close' event. The closeListener is registered as a 'close' event listener.

Example

  1. var net = require('net');
  2. var server = net.createServer();
  3. var port = 22704;
  4. var timeout = 1000;
  5. server.listen(port);
  6. /* ... */
  7. server.close();

server.listen(port[, host][, backlog][, listenListener])

  • port {number} Port the client should connect to.
  • host {string} Host the client should connect to.
  • backlog {number} The maximum length of the queue of pending connections. Default: 511.
  • listenListener {Function} Listener for the 'listening' event.
  • Returns: {Object} The self instance of net.Server.

Begin accepting connections on the specified port and hostname. If the hostname is omitted, the server will accept connections on any IPv4 address (0.0.0.0).

Example

  1. var net = require('net');
  2. var server = net.createServer();
  3. var port = 22709;
  4. server.listen(port);

server.listen(options[, listenListener])

  • options {Object} An object which specifies the connection options.
  • listenListener {Function} Listener for the 'listening' event.

It behaves as the server.listen(port[, host][, backlog][, listenListener]) function above.

The option object supports the following properties:

  • port {number} Port the client should connect to.
  • host {string} Host the client should connect to.
  • backlog {number} The maximum length of the queue of pending connections. Default: 511.

Example

  1. var net = require('net');
  2. var server = net.createServer();
  3. server.listen({port: 80, host: 'localhost'});

Event: ‘close’

  • callback {Function}

Emitted when server has closed the connection.

Note that this event will be emitted after all existing connections are closed.

Example

  1. var net = require('net');
  2. var serverCloseEvent = 0;
  3. var server = net.createServer();
  4. var port = 80;
  5. server.listen(port);
  6. server.on('connection', function(socket) {
  7. server.on('close', function() {
  8. serverCloseEvent++;
  9. });
  10. });

Event: ‘connection(socket)’

  • callback {Function}
    • socket {Net.Socket}

Emitted when new connection is established.

Example

  1. var net = require('net');
  2. var server = net.createServer();
  3. var port = 80;
  4. server.listen(port);
  5. server.on('connection', function(socket) {
  6. var msg = '';
  7. socket.on('data', function(data) {
  8. msg += data;
  9. });
  10. });

Event: ‘error’

  • callback {Function}

Emitted when an error occurs.

Example

  1. var assert = require('assert');
  2. var net = require('net');
  3. var port = 80;
  4. var msg = 'Hello IoT.js';
  5. var server = net.createServer();
  6. /* ... */
  7. server.on('error', function() {
  8. assert.fail();
  9. server.close();
  10. });

Event: ‘listening’

  • callback {Function}

Emitted when server has been started listening.

Example

  1. var net = require('net');
  2. var port = 80;
  3. var server = net.createServer(
  4. {
  5. allowHalfOpen: true
  6. },
  7. function(socket) {
  8. server.close();
  9. }
  10. );
  11. server.listen(port);
  12. server.on('listening', function() {
  13. console.log('started listening');
  14. });

Class: net.Socket

This object is an abstraction of a TCP or a local socket. net.Socket inherits from Stream.Duplex. They can be created by the user (used as a client with connect()) or by the IoT.js engine (passed to the user through the ‘connection’ event of a server).

new net.Socket([options])

  • options {Object} An optional object which specifies the socket information.
  • Returns {net.Socket}.

Construct a new socket object. The options object specifies only the following information: allowHalfOpen {boolean}.

Example

  1. var net = require('net');
  2. var socket = new net.Socket();

socket.connect(options[, connectListener])

  • options {Object} An object which specifies the connection information.
  • connectListener {Function} Listener for the 'connect' event.
  • Returns {net.Socket}.

Creates a new socket object and automatically connects with the supplied options.

The options object specifies following information:

  • port {number} Port connect to (required).
  • host {string} Host connect to (optional, Default: localhost).
  • family {number} Version of IP stack.

The connectionListener is automatically registered as a 'connect' event listener which will be emitted when the connection is established.

Example

  1. var net = require('net');
  2. var port = 22702;
  3. var socket = new net.Socket();
  4. socket.connect({port: port, family: 4}, function() {
  5. socket.end('Hello IoT.js');
  6. });

socket.connect(port[, host][, connectListener])

  • port {number} Port the client should connect to.
  • host {string} Host the client should connect to. Default: localhost.
  • connectListener {Function} Listener for the 'connect' event.
  • Returns {net.Socket}.

Creates a new socket and automatically connects with the supplied port and host.

connectionListener is automatically registered as a 'connect' event listener which will be emitted when the connection is established.

Example

  1. var net = require('net');
  2. var port = 80;
  3. var socket = new net.Socket();
  4. socket.connect(port, '127.0.0.1', function() {
  5. socket.end('Hello IoT.js');
  6. });

socket.destroy()

Ensures that no more I/O activity happens on the socket and destroys the socket as soon as possible.

Example

  1. var net = require('net');
  2. var port = 80;
  3. var socket = new net.Socket();
  4. socket.connect(port, '127.0.0.1', function() {
  5. socket.end('Hello IoT.js');
  6. });
  7. /* ... */
  8. socket.destroy();

socket.end([data][, callback])

  • data {Buffer|string}
  • callback {Function}

Half-closes the socket. The socket is no longer writable. If data is given it is equivalent to socket.write(data) followed by socket.end().

Example

  1. var net = require('net');
  2. var server = net.createServer();
  3. var port = 4010;
  4. server.listen(port);
  5. server.on('connection', function(socket) {
  6. socket.on('data', function(data) {
  7. socket.end('Hello IoT.js');
  8. });
  9. });

socket.pause()

Pauses reading data.

Example

  1. var net = require('net');
  2. var server = net.createServer();
  3. var port = 4010;
  4. server.listen(port);
  5. server.on('connection', function(socket) {
  6. // as soon as connection established, pause the socket
  7. socket.pause();
  8. });

socket.resume()

Resumes reading data after a call to pause().

Example

  1. var net = require('net');
  2. var timers = require('timers');
  3. var server = net.createServer();
  4. var port = 80;
  5. var socket = new net.Socket();
  6. var msg = "";
  7. /* ... */
  8. server.listen(port);
  9. server.on('connection', function(socket) {
  10. socket.on('data', function(data) {
  11. msg += data;
  12. socket.end();
  13. });
  14. socket.on('close', function() {
  15. server.close();
  16. });
  17. // as soon as connection established, pause the socket
  18. socket.pause();
  19. // resume after 2 secs
  20. timers.setTimeout(function() {
  21. socket.resume();
  22. }, 2000);
  23. });

socket.setKeepAlive([enable][, initialDelay])

  • enable {boolean} Default: false.
  • initialDelay {number} **Default:** 0`.

Enables or disables keep-alive functionality.

Example

  1. var net = require('net');
  2. var keepalive_sock = new net.Socket();
  3. keepalive_sock.setKeepAlive(true, 1000);

socket.setTimeout(timeout[, callback])

  • timeout {number} Timeout number.
  • callback {Function} Registered as a 'timeout' event listener.

Sets timeout for the socket.

If the socket is inactive for timeout milliseconds, 'timeout' event will emit.

Example

  1. var net = require('net');
  2. var server = net.createServer();
  3. var port = 40;
  4. var timeout = 2000;
  5. var msg = '';
  6. server.listen(port);
  7. server.on('connection', function(socket) {
  8. socket.setTimeout(timeout, function() {
  9. socket.end();
  10. });
  11. });

socket.write(data[, callback])

  • data {Buffer|string} Data to write.
  • callback {Function} Executed function (when the data is finally written out).

Sends data on the socket.

The optional callback function will be called after the given data is flushed through the connection.

Example

  1. var net = require('net');
  2. var timers = require('timers');
  3. var writeat = 1000;
  4. var socket = new net.Socket();
  5. /* ... */
  6. socket.on('connect', function() {
  7. timers.setTimeout(function() {
  8. socket.write('Hello IoT.js');
  9. }, writeat);
  10. });

Event: ‘connect’

  • callback {Function}

Emitted after connection is established.

Example

  1. var net = require('net');
  2. var port = 80;
  3. var count = 40;
  4. /* ... */
  5. for (var i = 0; i < count; ++i) {
  6. (function(i) {
  7. var socket = new net.Socket();
  8. socket.connect(port, "localhost");
  9. socket.on('connect', function() {
  10. socket.end(i.toString());
  11. });
  12. /* ... */
  13. })(i);
  14. }

Event: ‘close’

  • callback {Function}

Emitted when the socket has been closed.

Example

  1. var net = require('net');
  2. var server = net.createServer();
  3. var port = 80;
  4. server.listen(port);
  5. server.on('connection', function(socket) {
  6. /* ... */
  7. socket.on('close', function() {
  8. server.close();
  9. });
  10. });

Event: ‘data’

  • callback {Function}

The data is given an argument (data: Buffer|string).

Emitted when data is received from the connection.

Example

  1. var net = require('net');
  2. var msg = "";
  3. var socket = new net.Socket();
  4. /* ... */
  5. socket.on('data', function(data) {
  6. msg += data;
  7. });

Event: ‘drain’

  • callback {Function}

Emitted when the write buffer becomes empty.

Example

  1. var net = require('net');
  2. var port = 22703;
  3. var limit = 200;
  4. var server = net.createServer();
  5. server.listen({ port: port });
  6. /* ... */
  7. server.on('connection', function(socket) {
  8. var i = 0;
  9. var writing = function() {
  10. var ok;
  11. do {
  12. ok = socket.write("" + (i % 10));
  13. if (++i == limit) {
  14. socket.end();
  15. ok = false;
  16. }
  17. } while (ok);
  18. };
  19. socket.on('drain', writing);
  20. writing();
  21. });

Event: ‘end’

  • callback {Function}

Emitted when FIN packet received.

Example

  1. var net = require('net');
  2. var socket = new net.Socket();
  3. /* ... */
  4. socket.on('end', function() {
  5. socket.end();
  6. });

Event: ‘error’

  • callback {Function}
    • err {Error}

Emitted when an error occurs.

Example

  1. var assert = require('assert');
  2. var net = require('net');
  3. var bad_sock = new net.Socket();
  4. bad_sock.on('error', function(err){
  5. assert.equal(err instanceof Error, true);
  6. });

Event: ‘lookup’

  • callback {Function}
    • err {Error}
    • address {string}
    • family {string|null}

Emitted after resolving hostname.

Example

  1. var socket = new net.Socket();
  2. var msg = "";
  3. var lookupHandled = false;
  4. socket.on('lookup', function(err, ip, family) {
  5. lookupHandled = true;
  6. });

Event: ‘timeout’

  • callback {Function}`

Emitted when the connection remains idle for the specified timeout.

Example

  1. var net = require('net');
  2. var timedout = false;
  3. // Try connect to host that is not exist (Reserved address)
  4. var socket = net.createConnection(11111, '192.0.2.1');
  5. socket.setTimeout(1000);
  6. socket.on('timeout', function() {
  7. timedout = true;
  8. socket.destroy();
  9. });