SoFunction
Updated on 2025-03-03

Methods to detect whether the port is occupied

Preface

When learning tcp/ip, I often encounter some things related to ports. When writing some things on the network, sometimes some ports used are prompted: the port has been occupied. Ahhhh, it's quite depressed, and then I think about figuring it out. I won't say much below, let's take a look at the detailed introduction together.

Nodejs detects whether the port is occupied

When opening the local service, there is a situation where the current port has been used by another project, causing the service to fail to start.

Then, we use a simple ten lines of code to detect whether the port has been occupied.

Ideas

If we want to know whether the port is occupied, we can open a new service and listen to the port. If it is successfully opened, it means that the port has not been occupied. Otherwise, the port has been used by other programs.

Now that we have the basic idea, let’s start writing the code.

Basic code

var net = require('net')

// Check whether the port is occupiedfunction portIsOccupied (port) {
 // Create a service and listen to the port var server = ().listen(port)

 ('listening', function () { // Execute this code to indicate that the port is not occupied () // Close service ('The port【' + port + '】 is available.') // Console output information })

 ('error', function (err) {
 if ( === 'EADDRINUSE') { // The port has been used  ('The port【' + port + '】 is occupied, please change other port.')
 }
 })
}

// implementportIsOccupied(1987)

test

implementnode , under normal circumstances, the port [1987] will be output.

Use ssr to set the current directory to a static server. The use of ssr can be found here/jaywcjlove/ssr
SSR will enable port 1987 by default.

At this time, execute againnode , output The port【1987】 is occupied, please change other port.

Make the program more perfect

So far, we have been able to detect whether the port is occupied.

However, if I want to detect another port, I have to change the port number of the file every time, which is quite troublesome.

So is there a more perfect implementation method? Of course there is!

There is such a fun thing in Nodejs. It is an array, and the first two values ​​are the location of the node program and the location of the currently running file.

The magic is that when we run the program, the passed parameters can be obtained through.

For example, node -p 1987 will be added to the end of the array.

Now that we understand the principle, let’s write a more perfect program.

Console passes parameters to program

Modify portIsOccupied(1987) to:

var port = 80 // Default port 80if ([2] === '-p') {
 port = parseInt([3])
}
portIsOccupied(port)

implement

where [port] is the port you want to detect.

node  -p [port]

Summarize

The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.