9
9
## net.createServer([options], [connectionListener])
11
11
Creates a new TCP server. The `connectionListener` argument is
12
automatically set as a listener for the ['connection'](#event_connection_)
12
automatically set as a listener for the ['connection'][] event.
15
14
`options` is an object with the following defaults:
17
16
{ allowHalfOpen: false
20
If `allowHalfOpen` is `true`, then the socket won't automatically send FIN
19
If `allowHalfOpen` is `true`, then the socket won't automatically send a FIN
21
20
packet when the other end of the socket sends a FIN packet. The socket becomes
22
21
non-readable, but still writable. You should call the `end()` method explicitly.
23
See ['end'](#event_end_) event for more information.
22
See ['end'][] event for more information.
25
Here is an example of a echo server which listens for connections
24
Here is an example of an echo server which listens for connections
28
27
var net = require('net');
52
51
nc -U /tmp/echo.sock
54
## net.connect(arguments...)
55
## net.createConnection(arguments...)
57
Construct a new socket object and opens a socket to the given location. When
58
the socket is established the ['connect'](#event_connect_) event will be
61
The arguments for these methods change the type of connection:
63
* `net.connect(port, [host], [connectListener])`
64
* `net.createConnection(port, [host], [connectListener])`
66
Creates a TCP connection to `port` on `host`. If `host` is omitted,
67
`'localhost'` will be assumed.
69
* `net.connect(path, [connectListener])`
70
* `net.createConnection(path, [connectListener])`
72
Creates unix socket connection to `path`.
53
## net.connect(options, [connectionListener])
54
## net.createConnection(options, [connectionListener])
56
Constructs a new socket object and opens the socket to the given location.
57
When the socket is established, the ['connect'][] event will be emitted.
59
For TCP sockets, `options` argument should be an object which specifies:
61
- `port`: Port the client should connect to (Required).
63
- `host`: Host the client should connect to. Defaults to `'localhost'`.
65
- `localAddress`: Local interface to bind to for network connections.
67
For UNIX domain sockets, `options` argument should be an object which specifies:
69
- `path`: Path the client should connect to (Required).
73
- `allowHalfOpen`: if `true`, the socket won't automatically send
74
a FIN packet when the other end of the socket sends a FIN packet.
75
Defaults to `false`. See ['end'][] event for more information.
74
77
The `connectListener` parameter will be added as an listener for the
75
['connect'](#event_connect_) event.
77
80
Here is an example of a client of echo server as described previously:
79
82
var net = require('net');
80
var client = net.connect(8124, function() { //'connect' listener
83
var client = net.connect({port: 8124},
84
function() { //'connect' listener
81
85
console.log('client connected');
82
86
client.write('world!\r\n');
92
96
To connect on the socket `/tmp/echo.sock` the second line would just be
95
var client = net.connect('/tmp/echo.sock', function() { //'connect' listener
99
var client = net.connect({path: '/tmp/echo.sock'},
101
## net.connect(port, [host], [connectListener])
102
## net.createConnection(port, [host], [connectListener])
104
Creates a TCP connection to `port` on `host`. If `host` is omitted,
105
`'localhost'` will be assumed.
106
The `connectListener` parameter will be added as an listener for the
109
## net.connect(path, [connectListener])
110
## net.createConnection(path, [connectListener])
112
Creates unix socket connection to `path`.
113
The `connectListener` parameter will be added as an listener for the
97
116
## Class: net.Server
99
118
This class is used to create a TCP or UNIX server.
100
119
A server is a `net.Socket` that can listen for new incoming connections.
102
### server.listen(port, [host], [listeningListener])
121
### server.listen(port, [host], [backlog], [callback])
104
123
Begin accepting connections on the specified `port` and `host`. If the
105
124
`host` is omitted, the server will accept connections directed to any
106
125
IPv4 address (`INADDR_ANY`). A port value of zero will assign a random port.
127
Backlog is the maximum length of the queue of pending connections.
128
The actual length will be determined by your OS through sysctl settings such as
129
`tcp_max_syn_backlog` and `somaxconn` on linux. The default value of this
130
parameter is 511 (not 512).
108
132
This function is asynchronous. When the server has been bound,
109
['listening'](#event_listening_) event will be emitted.
110
the last parameter `listeningListener` will be added as an listener for the
111
['listening'](#event_listening_) event.
133
['listening'][] event will be emitted. The last parameter `callback`
134
will be added as an listener for the ['listening'][] event.
113
136
One issue some users run into is getting `EADDRINUSE` errors. This means that
114
137
another server is already running on the requested port. One way of handling this
127
150
(Note: All sockets in Node set `SO_REUSEADDR` already)
130
### server.listen(path, [listeningListener])
153
### server.listen(path, [callback])
132
155
Start a UNIX socket server listening for connections on the given `path`.
134
157
This function is asynchronous. When the server has been bound,
158
['listening'][] event will be emitted. The last parameter `callback`
159
will be added as an listener for the ['listening'][] event.
161
### server.listen(handle, [callback])
164
* `callback` {Function}
166
The `handle` object can be set to either a server or socket (anything
167
with an underlying `_handle` member), or a `{fd: <n>}` object.
169
This will cause the server to accept connections on the specified
170
handle, but it is presumed that the file descriptor or handle has
171
already been bound to a port or domain socket.
173
Listening on a file descriptor is not supported on Windows.
175
This function is asynchronous. When the server has been bound,
135
176
['listening'](#event_listening_) event will be emitted.
136
the last parameter `listeningListener` will be added as an listener for the
177
the last parameter `callback` will be added as an listener for the
137
178
['listening'](#event_listening_) event.
180
### server.close([callback])
141
Stops the server from accepting new connections. This function is
142
asynchronous, the server is finally closed when the server emits a `'close'`
182
Stops the server from accepting new connections and keeps existing
183
connections. This function is asynchronous, the server is finally
184
closed when all connections are ended and the server emits a `'close'`
185
event. Optionally, you can pass a callback to listen for the `'close'`
146
188
### server.address()
148
Returns the bound address and port of the server as reported by the operating system.
190
Returns the bound address, the address family name and port of the server
191
as reported by the operating system.
149
192
Useful to find which port was assigned when giving getting an OS-assigned address.
150
Returns an object with two properties, e.g. `{"address":"127.0.0.1", "port":2121}`
193
Returns an object with three properties, e.g.
194
`{ port: 12346, family: 'IPv4', address: '127.0.0.1' }`
164
208
Don't call `server.address()` until the `'listening'` event has been emitted.
212
Calling `unref` on a server will allow the program to exit if this is the only
213
active server in the event system. If the server is already `unref`d calling
214
`unref` again will have no effect.
218
Opposite of `unref`, calling `ref` on a previously `unref`d server will *not*
219
let the program exit if it's the only server left (the default behavior). If
220
the server is `ref`d calling `ref` again will have no effect.
166
222
### server.maxConnections
168
224
Set this property to reject connections when the server's connection count gets
227
It is not recommended to use this option once a socket has been sent to a child
228
with `child_process.fork()`.
171
230
### server.connections
232
This function is **deprecated**; please use [server.getConnections()][] instead.
173
233
The number of concurrent connections on the server.
176
`net.Server` is an `EventEmitter` with the following events:
235
This becomes `null` when sending a socket to a child with
236
`child_process.fork()`. To poll forks and get current number of active
237
connections use asynchronous `server.getConnections` instead.
239
`net.Server` is an [EventEmitter][] with the following events:
241
### server.getConnections(callback)
243
Asynchronously get the number of concurrent connections on the server. Works
244
when sockets were sent to forks.
246
Callback should take two arguments `err` and `count`.
178
248
### Event: 'listening'
228
299
opened as a unix socket to that path.
230
301
Normally this method is not needed, as `net.createConnection` opens the
231
socket. Use this only if you are implementing a custom Socket or if a
232
Socket is closed and you want to reuse it to connect to another server.
302
socket. Use this only if you are implementing a custom Socket.
234
This function is asynchronous. When the ['connect'](#event_connect_) event is
235
emitted the socket is established. If there is a problem connecting, the
236
`'connect'` event will not be emitted, the `'error'` event will be emitted with
304
This function is asynchronous. When the ['connect'][] event is emitted the
305
socket is established. If there is a problem connecting, the `'connect'` event
306
will not be emitted, the `'error'` event will be emitted with the exception.
239
308
The `connectListener` parameter will be added as an listener for the
240
['connect'](#event_connect_) event.
243
312
### socket.bufferSize
262
331
### socket.setEncoding([encoding])
264
Sets the encoding (either `'ascii'`, `'utf8'`, or `'base64'`) for data that is
265
received. Defaults to `null`.
267
### socket.setSecure()
269
This function has been removed in v0.3. It used to upgrade the connection to
270
SSL/TLS. See the [TLS section](tls.html#tLS_) for the new API.
333
Set the encoding for the socket as a Readable Stream. See
334
[stream.setEncoding()][] for more information.
273
336
### socket.write(data, [encoding], [callback])
339
402
### socket.address()
341
Returns the bound address and port of the socket as reported by the operating
342
system. Returns an object with two properties, e.g.
343
`{"address":"192.168.57.1", "port":62053}`
404
Returns the bound address, the address family name and port of the
405
socket as reported by the operating system. Returns an object with
406
three properties, e.g.
407
`{ port: 12346, family: 'IPv4', address: '127.0.0.1' }`
411
Calling `unref` on a socket will allow the program to exit if this is the only
412
active socket in the event system. If the socket is already `unref`d calling
413
`unref` again will have no effect.
417
Opposite of `unref`, calling `ref` on a previously `unref`d socket will *not*
418
let the program exit if it's the only socket left (the default behavior). If
419
the socket is `ref`d calling `ref` again will have no effect.
345
421
### socket.remoteAddress
352
428
The numeric representation of the remote port. For example,
431
### socket.localAddress
433
The string representation of the local IP address the remote client is
434
connecting on. For example, if you are listening on `'0.0.0.0'` and the
435
client connects on `'192.168.1.1'`, the value would be `'192.168.1.1'`.
439
The numeric representation of the local port. For example,
355
442
### socket.bytesRead
357
444
The amount of received bytes.
375
462
Emitted when data is received. The argument `data` will be a `Buffer` or
376
463
`String`. Encoding of data is set by `socket.setEncoding()`.
377
(See the [Readable Stream](stream.html#readable_stream) section for more
464
(See the [Readable Stream][] section for more information.)
380
466
Note that the __data will be lost__ if there is no listener when a `Socket`
381
467
emits a `'data'` event.
435
521
Returns true if input is a version 6 IP address, otherwise returns false.
523
['connect']: #net_event_connect
524
['connection']: #net_event_connection
525
['end']: #net_event_end
526
[EventEmitter]: events.html#events_class_events_eventemitter
527
['listening']: #net_event_listening
528
[Readable Stream]: stream.html#stream_readable_stream
529
[stream.setEncoding()]: stream.html#stream_stream_setencoding_encoding