-
Notifications
You must be signed in to change notification settings - Fork 1
/
node-api-net.txt
247 lines (177 loc) · 8.37 KB
/
node-api-net.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
*node-api-net.txt* For Node.js module `net` version v3.3.0.
Node.js Documentation
https://nodejs.org
==============================================================================
CONTENTS *node-api-contents*
1. Intro |node-api-net|
2. Methods |node-api-net-methods|
2.1. net.createServer([options][, connectionListener]) |node-api-net.createServer()|
2.2. net.connect(options[, connectListener]) |node-api-net.connect()|
2.3. net.createConnection(options[, connectListener]) |node-api-net.createConnection()|
2.4. net.connect(port[, host][, connectListener]) |node-api-net.connect()|
2.5. net.createConnection(port[, host][, connectListener]) |node-api-net.createConnection()|
2.6. net.connect(path[, connectListener]) |node-api-net.connect()|
2.7. net.createConnection(path[, connectListener]) |node-api-net.createConnection()|
2.8. net.isIP(input) |node-api-net.isIP()|
2.9. net.isIPv4(input) |node-api-net.isIPv4()|
2.10. net.isIPv6(input) |node-api-net.isIPv6()|
3. Properties |node-api-net-properties|
==============================================================================
INTRO *node-api-net*
Stability: 2 - Stable
The `net` module provides you with an asynchronous network wrapper. It contains
functions for creating both servers and clients (called streams). You can include
this module with `require('net');`.
==============================================================================
METHODS *node-api-net-methods*
------------------------------------------------------------------------------
net.createServer([options][, connectionListener]) *node-api-net.createServer()*
Creates a new TCP server. The `connectionListener` argument is
automatically set as a listener for the ['connection'][] event.
`options` is an object with the following defaults:
>
{
allowHalfOpen: false,
pauseOnConnect: false
}
<
If `allowHalfOpen` is `true`, then the socket won't automatically send a FIN
packet when the other end of the socket sends a FIN packet. The socket becomes
non-readable, but still writable. You should call the `end()` method explicitly.
See ['end'][] event for more information.
If `pauseOnConnect` is `true`, then the socket associated with each incoming
connection will be paused, and no data will be read from its handle. This allows
connections to be passed between processes without any data being read by the
original process. To begin reading data from a paused socket, call `resume()`.
Here is an example of an echo server which listens for connections
on port 8124:
>
var net = require('net');
var server = net.createServer(function(c) { //'connection' listener
console.log('client connected');
c.on('end', function() {
console.log('client disconnected');
});
c.write('hello\r\n');
c.pipe(c);
});
server.listen(8124, function() { //'listening' listener
console.log('server bound');
});
<
Test this by using `telnet`:
>
telnet localhost 8124
<
To listen on the socket `/tmp/echo.sock` the third line from the last would
just be changed to
>
server.listen('/tmp/echo.sock', function() { //'listening' listener
<
Use `nc` to connect to a UNIX domain socket server:
>
nc -U /tmp/echo.sock
<
------------------------------------------------------------------------------
net.connect(options[, connectListener]) *node-api-net.connect()*
A factory function, which returns a new *node-api-net_class_net_socket*
and automatically connects with the supplied `options`.
The options are passed to both the *node-api-net_class_net_socket*
constructor and the *node-api-net_socket_connect_options_connectlistener*
method.
The `connectListener` parameter will be added as a listener for the
['connect'][] event once.
Here is an example of a client of the previously described echo server:
>
var net = require('net');
var client = net.connect({port: 8124},
function() { //'connect' listener
console.log('connected to server!');
client.write('world!\r\n');
});
client.on('data', function(data) {
console.log(data.toString());
client.end();
});
client.on('end', function() {
console.log('disconnected from server');
});
<
To connect on the socket `/tmp/echo.sock` the second line would just be
changed to
>
var client = net.connect({path: '/tmp/echo.sock'});
<
------------------------------------------------------------------------------
net.createConnection(options[, connectListener]) *node-api-net.createConnection()*
A factory function, which returns a new *node-api-net_class_net_socket*
and automatically connects with the supplied `options`.
The options are passed to both the *node-api-net_class_net_socket*
constructor and the *node-api-net_socket_connect_options_connectlistener*
method.
The `connectListener` parameter will be added as a listener for the
['connect'][] event once.
Here is an example of a client of the previously described echo server:
>
var net = require('net');
var client = net.connect({port: 8124},
function() { //'connect' listener
console.log('connected to server!');
client.write('world!\r\n');
});
client.on('data', function(data) {
console.log(data.toString());
client.end();
});
client.on('end', function() {
console.log('disconnected from server');
});
<
To connect on the socket `/tmp/echo.sock` the second line would just be
changed to
>
var client = net.connect({path: '/tmp/echo.sock'});
<
------------------------------------------------------------------------------
net.connect(port[, host][, connectListener]) *node-api-net.connect()*
A factory function, which returns a new
*node-api-net_class_net_socket* and automatically connects to the
supplied `port` and `host`.
If `host` is omitted, `'localhost'` will be assumed.
The `connectListener` parameter will be added as a listener for the
['connect'][] event once.
------------------------------------------------------------------------------
net.createConnection(port[, host][, connectListener]) *node-api-net.createConnection()*
A factory function, which returns a new
*node-api-net_class_net_socket* and automatically connects to the
supplied `port` and `host`.
If `host` is omitted, `'localhost'` will be assumed.
The `connectListener` parameter will be added as a listener for the
['connect'][] event once.
------------------------------------------------------------------------------
net.connect(path[, connectListener]) *node-api-net.connect()*
A factory function, which returns a new unix
*node-api-net_class_net_socket* and automatically connects to the
supplied `path`.
The `connectListener` parameter will be added as a listener for the
['connect'][] event once.
------------------------------------------------------------------------------
net.createConnection(path[, connectListener]) *node-api-net.createConnection()*
A factory function, which returns a new unix
*node-api-net_class_net_socket* and automatically connects to the
supplied `path`.
The `connectListener` parameter will be added as a listener for the
['connect'][] event once.
------------------------------------------------------------------------------
net.isIP(input) *node-api-net.isIP()*
Tests if input is an IP address. Returns 0 for invalid strings,
returns 4 for IP version 4 addresses, and returns 6 for IP version 6 addresses.
------------------------------------------------------------------------------
net.isIPv4(input) *node-api-net.isIPv4()*
Returns true if input is a version 4 IP address, otherwise returns false.
------------------------------------------------------------------------------
net.isIPv6(input) *node-api-net.isIPv6()*
Returns true if input is a version 6 IP address, otherwise returns false.
==============================================================================
PROPERTIES *node-api-net-properties*
vim:tw=78:ts=8:ft=help