Permalink
Browse files

net: make .address() work for client sockets

  • Loading branch information...
bnoordhuis committed Nov 22, 2011
1 parent 7defbd2 commit 1b0994de81cd2748e51c735f1e68573ba4cbdfe7
Showing with 12 additions and 12 deletions.
  1. +9 −10 lib/net.js
  2. +3 −2 test/simple/test-net-remote-address-port.js
View
@@ -164,8 +164,15 @@ Socket.prototype.setKeepAlive = function(setting, msecs) {
};
+// Also used by Server.prototype.address()
Socket.prototype.address = function() {
- return this._handle.getsockname();
+ if (this._handle && this._handle.getsockname) {
+ return this._handle.getsockname();
+ } else if (this._pipeName) {
+ return this._pipeName;
+ } else {
+ return null;
+ }
};
@@ -768,15 +775,7 @@ Server.prototype.listen = function() {
return self;
};
-Server.prototype.address = function() {
- if (this._handle && this._handle.getsockname) {
- return this._handle.getsockname();
- } else if (this._pipeName) {
- return this._pipeName;
- } else {
- return null;
- }
-};
+Server.prototype.address = Socket.prototype.address;
function onconnection(clientHandle) {
var handle = this;
@@ -15,12 +15,13 @@ var server = net.createServer(function(socket) {
});
});
-server.listen(common.PORT, 'localhost', function() {
- var client = net.createConnection(common.PORT, 'localhost');
+server.listen(common.PORT, '127.0.0.1', function() {
+ var client = net.createConnection(common.PORT, '127.0.0.1');
var client2 = net.createConnection(common.PORT);
client.on('connect', function() {
assert.equal('127.0.0.1', client.remoteAddress);
assert.equal(common.PORT, client.remotePort);
+ assert.equal('127.0.0.1', client.address().address); // local address
client.end();
});
client2.on('connect', function() {

0 comments on commit 1b0994d

Please sign in to comment.