SoFunction
Updated on 2025-03-09

Quickly scan the port and discover the web server address in the LAN (80)

It is quite convenient to scan ports in it, and there are generally two ways to broadcast and poll. Even if using broadcast and scanning, messages sent using broadcast are sometimes blocked by the router, so they are not reliable.

In use, you can directly try to connect to a port of the directory host. If a connection can be established, it means that the address exists in the server.

var socket = new Socket()
(port, host)
('connect', function() {
 //Find the port and host addresses})

Therefore, as long as 255 scans are performed, all web servers within the same network segment can be found. The complete example code is as follows: Scanning is extremely fast and can be completed in seconds.

var net   = require('net')
var Socket = 
//The start network segment to be scanned can be replaced with 192.168.0var ip   = '10.0.0'
var port  = 80
var scan = function(host, cb) {
 var socket = new Socket()
 var status = null
 (1500)
 ('connect', function() {
  ()
  cb && cb(null, host)
 })
 ('timeout', function() {
  ()
  cb && cb(new Error('timeout'), host)
 })
 ('error', function(err) {
  cb && cb(err, host)
 })
 ('close', function(err) {
 })
 (port, host)
}
for(var i = 1; i <= 255; i++ ) {
 scan(ip+'.'+i, function(err, host) {
  if (err) {
   ('Not found', host)
   return
  }
  ("Found: ", host)
 })
}

Output result:

Found: 10.0.0.1
Found: 10.0.0.3
Not found 10.0.0.255
......

Summarize

The above is the medium-fast scanning port introduced by the editor and discovering the web server address (80) in the LAN. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!