Skip to content

Commit

Permalink
net: make .address() work for client sockets
Browse files Browse the repository at this point in the history
  • Loading branch information
bnoordhuis committed Nov 22, 2011
1 parent 7defbd2 commit 1b0994d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
19 changes: 9 additions & 10 deletions lib/net.js
Expand Up @@ -164,8 +164,15 @@ Socket.prototype.setKeepAlive = function(setting, msecs) {
}; };




// Also used by Server.prototype.address()
Socket.prototype.address = function() { 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;
}
}; };




Expand Down Expand Up @@ -768,15 +775,7 @@ Server.prototype.listen = function() {
return self; return self;
}; };


Server.prototype.address = function() { Server.prototype.address = Socket.prototype.address;
if (this._handle && this._handle.getsockname) {
return this._handle.getsockname();
} else if (this._pipeName) {
return this._pipeName;
} else {
return null;
}
};


function onconnection(clientHandle) { function onconnection(clientHandle) {
var handle = this; var handle = this;
Expand Down
5 changes: 3 additions & 2 deletions test/simple/test-net-remote-address-port.js
Expand Up @@ -15,12 +15,13 @@ var server = net.createServer(function(socket) {
}); });
}); });


server.listen(common.PORT, 'localhost', function() { server.listen(common.PORT, '127.0.0.1', function() {
var client = net.createConnection(common.PORT, 'localhost'); var client = net.createConnection(common.PORT, '127.0.0.1');
var client2 = net.createConnection(common.PORT); var client2 = net.createConnection(common.PORT);
client.on('connect', function() { client.on('connect', function() {
assert.equal('127.0.0.1', client.remoteAddress); assert.equal('127.0.0.1', client.remoteAddress);
assert.equal(common.PORT, client.remotePort); assert.equal(common.PORT, client.remotePort);
assert.equal('127.0.0.1', client.address().address); // local address
client.end(); client.end();
}); });
client2.on('connect', function() { client2.on('connect', function() {
Expand Down

0 comments on commit 1b0994d

Please sign in to comment.