Skip to content

Commit 3f76419

Browse files
JamesHightbnoordhuis
authored andcommitted
net: add localAddress and localPort to Socket
1 parent 5664dd2 commit 3f76419

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

doc/api/net.markdown

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,17 @@ The string representation of the remote IP address. For example,
419419
The numeric representation of the remote port. For example,
420420
`80` or `21`.
421421

422+
### socket.localAddress
423+
424+
The string representation of the local IP address the remote client is
425+
connecting on. For example, if you are listening on `'0.0.0.0'` and the
426+
client connects on `'192.168.1.1'`, the value would be `'192.168.1.1'`.
427+
428+
### socket.localPort
429+
430+
The numeric representation of the local port. For example,
431+
`80` or `21`.
432+
422433
### socket.bytesRead
423434

424435
The amount of received bytes.

lib/net.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,30 @@ Socket.prototype.__defineGetter__('remotePort', function() {
526526
});
527527

528528

529+
Socket.prototype._getsockname = function() {
530+
if (!this._handle || !this._handle.getsockname) {
531+
return {};
532+
}
533+
if (!this._sockname) {
534+
this._sockname = this._handle.getsockname();
535+
if (this._sockname === null) {
536+
return {};
537+
}
538+
}
539+
return this._sockname;
540+
};
541+
542+
543+
Socket.prototype.__defineGetter__('localAddress', function() {
544+
return this._getsockname().address;
545+
});
546+
547+
548+
Socket.prototype.__defineGetter__('localPort', function() {
549+
return this._getsockname().port;
550+
});
551+
552+
529553
Socket.prototype.write = function(chunk, encoding, cb) {
530554
if (typeof chunk !== 'string' && !Buffer.isBuffer(chunk))
531555
throw new TypeError('invalid data');
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var common = require('../common');
23+
var assert = require('assert');
24+
var net = require('net');
25+
26+
var conns = 0, conns_closed = 0;
27+
28+
var server = net.createServer(function(socket) {
29+
conns++;
30+
assert.equal('127.0.0.1', socket.localAddress);
31+
assert.equal(socket.localPort, common.PORT);
32+
socket.on('end', function() {
33+
server.close();
34+
});
35+
socket.resume();
36+
});
37+
38+
server.listen(common.PORT, '127.0.0.1', function() {
39+
var client = net.createConnection(common.PORT, '127.0.0.1');
40+
client.on('connect', function() {
41+
client.end();
42+
});
43+
});
44+
45+
process.on('exit', function() {
46+
assert.equal(1, conns);
47+
});

0 commit comments

Comments
 (0)