~yolanda.robla/ubuntu/trusty/nodejs/add_distribution

« back to all changes in this revision

Viewing changes to doc/api/net.markdown

  • Committer: Package Import Robot
  • Author(s): Jérémy Lal
  • Date: 2013-08-14 00:16:46 UTC
  • mfrom: (7.1.40 sid)
  • Revision ID: package-import@ubuntu.com-20130814001646-bzlysfh8sd6mukbo
Tags: 0.10.15~dfsg1-4
* Update 2005 patch, adding a handful of tests that can fail on
  slow platforms.
* Add 1004 patch to fix test failures when writing NaN to buffer
  on mipsel.

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
## net.createServer([options], [connectionListener])
10
10
 
11
11
Creates a new TCP server. The `connectionListener` argument is
12
 
automatically set as a listener for the ['connection'](#event_connection_)
13
 
event.
 
12
automatically set as a listener for the ['connection'][] event.
14
13
 
15
14
`options` is an object with the following defaults:
16
15
 
17
16
    { allowHalfOpen: false
18
17
    }
19
18
 
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.
24
23
 
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
26
25
on port 8124:
27
26
 
28
27
    var net = require('net');
51
50
 
52
51
    nc -U /tmp/echo.sock
53
52
 
54
 
## net.connect(arguments...)
55
 
## net.createConnection(arguments...)
56
 
 
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
59
 
emitted.
60
 
 
61
 
The arguments for these methods change the type of connection:
62
 
 
63
 
* `net.connect(port, [host], [connectListener])`
64
 
* `net.createConnection(port, [host], [connectListener])`
65
 
 
66
 
  Creates a TCP connection to `port` on `host`. If `host` is omitted,
67
 
  `'localhost'` will be assumed.
68
 
 
69
 
* `net.connect(path, [connectListener])`
70
 
* `net.createConnection(path, [connectListener])`
71
 
 
72
 
  Creates unix socket connection to `path`.
 
53
## net.connect(options, [connectionListener])
 
54
## net.createConnection(options, [connectionListener])
 
55
 
 
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.
 
58
 
 
59
For TCP sockets, `options` argument should be an object which specifies:
 
60
 
 
61
  - `port`: Port the client should connect to (Required).
 
62
 
 
63
  - `host`: Host the client should connect to. Defaults to `'localhost'`.
 
64
 
 
65
  - `localAddress`: Local interface to bind to for network connections.
 
66
 
 
67
For UNIX domain sockets, `options` argument should be an object which specifies:
 
68
 
 
69
  - `path`: Path the client should connect to (Required).
 
70
 
 
71
Common options are:
 
72
 
 
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.
73
76
 
74
77
The `connectListener` parameter will be added as an listener for the
75
 
['connect'](#event_connect_) event.
 
78
['connect'][] event.
76
79
 
77
80
Here is an example of a client of echo server as described previously:
78
81
 
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');
83
87
    });
92
96
To connect on the socket `/tmp/echo.sock` the second line would just be
93
97
changed to
94
98
 
95
 
    var client = net.connect('/tmp/echo.sock', function() { //'connect' listener
 
99
    var client = net.connect({path: '/tmp/echo.sock'},
 
100
 
 
101
## net.connect(port, [host], [connectListener])
 
102
## net.createConnection(port, [host], [connectListener])
 
103
 
 
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
 
107
['connect'][] event.
 
108
 
 
109
## net.connect(path, [connectListener])
 
110
## net.createConnection(path, [connectListener])
 
111
 
 
112
Creates unix socket connection to `path`.
 
113
The `connectListener` parameter will be added as an listener for the
 
114
['connect'][] event.
96
115
 
97
116
## Class: net.Server
98
117
 
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.
101
120
 
102
 
### server.listen(port, [host], [listeningListener])
 
121
### server.listen(port, [host], [backlog], [callback])
103
122
 
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.
107
126
 
 
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).
 
131
 
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.
112
135
 
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)
128
151
 
129
152
 
130
 
### server.listen(path, [listeningListener])
 
153
### server.listen(path, [callback])
131
154
 
132
155
Start a UNIX socket server listening for connections on the given `path`.
133
156
 
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.
 
160
 
 
161
### server.listen(handle, [callback])
 
162
 
 
163
* `handle` {Object}
 
164
* `callback` {Function}
 
165
 
 
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.
 
168
 
 
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.
 
172
 
 
173
Listening on a file descriptor is not supported on Windows.
 
174
 
 
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.
138
179
 
139
 
### server.close()
 
180
### server.close([callback])
140
181
 
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'`
143
186
event.
144
187
 
145
 
 
146
188
### server.address()
147
189
 
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' }`
151
195
 
152
196
Example:
153
197
 
163
207
 
164
208
Don't call `server.address()` until the `'listening'` event has been emitted.
165
209
 
 
210
### server.unref()
 
211
 
 
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.
 
215
 
 
216
### server.ref()
 
217
 
 
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.
 
221
 
166
222
### server.maxConnections
167
223
 
168
224
Set this property to reject connections when the server's connection count gets
169
225
high.
170
226
 
 
227
It is not recommended to use this option once a socket has been sent to a child
 
228
with `child_process.fork()`.
 
229
 
171
230
### server.connections
172
231
 
 
232
This function is **deprecated**; please use [server.getConnections()][] instead.
173
233
The number of concurrent connections on the server.
174
234
 
175
 
 
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.
 
238
 
 
239
`net.Server` is an [EventEmitter][] with the following events:
 
240
 
 
241
### server.getConnections(callback)
 
242
 
 
243
Asynchronously get the number of concurrent connections on the server. Works
 
244
when sockets were sent to forks.
 
245
 
 
246
Callback should take two arguments `err` and `count`.
177
247
 
178
248
### Event: 'listening'
179
249
 
188
258
 
189
259
### Event: 'close'
190
260
 
191
 
Emitted when the server closes.
 
261
Emitted when the server closes. Note that if connections exist, this
 
262
event is not emitted until all connections are ended.
192
263
 
193
264
### Event: 'error'
194
265
 
228
299
opened as a unix socket to that path.
229
300
 
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.
233
303
 
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
237
 
the exception.
 
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.
238
307
 
239
308
The `connectListener` parameter will be added as an listener for the
240
 
['connect'](#event_connect_) event.
 
309
['connect'][] event.
241
310
 
242
311
 
243
312
### socket.bufferSize
261
330
 
262
331
### socket.setEncoding([encoding])
263
332
 
264
 
Sets the encoding (either `'ascii'`, `'utf8'`, or `'base64'`) for data that is
265
 
received. Defaults to `null`.
266
 
 
267
 
### socket.setSecure()
268
 
 
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.
271
 
 
 
333
Set the encoding for the socket as a Readable Stream. See
 
334
[stream.setEncoding()][] for more information.
272
335
 
273
336
### socket.write(data, [encoding], [callback])
274
337
 
338
401
 
339
402
### socket.address()
340
403
 
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' }`
 
408
 
 
409
### socket.unref()
 
410
 
 
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.
 
414
 
 
415
### socket.ref()
 
416
 
 
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.
344
420
 
345
421
### socket.remoteAddress
346
422
 
352
428
The numeric representation of the remote port. For example,
353
429
`80` or `21`.
354
430
 
 
431
### socket.localAddress
 
432
 
 
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'`.
 
436
 
 
437
### socket.localPort
 
438
 
 
439
The numeric representation of the local port. For example,
 
440
`80` or `21`.
 
441
 
355
442
### socket.bytesRead
356
443
 
357
444
The amount of received bytes.
361
448
The amount of bytes sent.
362
449
 
363
450
 
364
 
`net.Socket` instances are EventEmitters with the following events:
 
451
`net.Socket` instances are [EventEmitter][] with the following events:
365
452
 
366
453
### Event: 'connect'
367
454
 
374
461
 
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
378
 
information.)
 
464
(See the [Readable Stream][] section for more information.)
379
465
 
380
466
Note that the __data will be lost__ if there is no listener when a `Socket`
381
467
emits a `'data'` event.
434
520
 
435
521
Returns true if input is a version 6 IP address, otherwise returns false.
436
522
 
 
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