Skip to content

Commit

Permalink
Only returns status of a port after connection closed.
Browse files Browse the repository at this point in the history
Originally, if trying to immediately connect to a port returned from the status
check, the socket connection may not have finished closing. Now returns the
status after the connection has finished closing on the socket.
  • Loading branch information
baalexander committed Nov 2, 2011
1 parent af6c474 commit fe97267
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/portscanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,31 @@ portscanner.findAPortNotInUse = function(startPort, endPort, host, callback) {
portscanner.checkPortStatus = function(port, host, callback) {
host = host || 'localhost'
var socket = new Socket()
, status = null

// Socket connection established, port is open
socket.on('connect', function() {
status = 'open'
socket.end()
callback(null, 'open')
})

// If no response, assume port is not listening
socket.setTimeout(400)
socket.on('timeout', function() {
status = 'closed'
socket.end()
callback(null, 'closed')
})

// Assuming the port is not open if an error. May need to refine based on
// exception
socket.on('error', function(exception) {
status = 'closed'
socket.destroy()
callback(null, 'closed')
})

// Return after the socket has closed
socket.on('close', function(exception) {
callback(null, status)
})

socket.connect(port, host)
Expand Down

0 comments on commit fe97267

Please sign in to comment.