MCPcopy Create free account
hub / github.com/Marus/cortex-debug / isPortInUse

Method isPortInUse

src/tcpportscanner.ts:57–89  ·  view source on GitHub ↗

* Checks to see if the port is in use by creating a server on that port. You should use the function * `isPortInUseEx()` if you want to do a more exhaustive check or a general purpose use for any host * * @param port port to use. Must be > 0 and <= 65535 * @param host host ip ad

(port: number, host: string, seq: number = 9999)

Source from the content-addressed store, hash-verified

55 * in which case the Node.js default rules apply.
56 */
57 public static isPortInUse(port: number, host: string, seq: number = 9999): Promise<boolean> {
58 ConsoleLog(`isPortInUse: testing port ${host}:${port}`);
59 return new Promise((resolve, reject) => {
60 const server = net.createServer((c) => {
61 });
62 server.once('error', (e) => {
63 const code: string = (e as any).code;
64 if (code && (code === 'EADDRINUSE') || (code === 'EACCES')) {
65 // console.log(`port ${host}:${port} is used`, code);
66 if (code === 'EACCES') {
67 // Technically, EACCES means permission denied, so we consider it as used
68 ConsoleLog(`isPortInUse: port ${host}:${port} returned code EACCES?, ${seq}`);
69 }
70 ConsoleLog(`isPortInUse: port ${host}:${port} is busy, ${seq}`);
71 resolve(true); // Port in use
72 } else {
73 // This should never happen so, log it always
74 ConsoleLog(`isPortInUse: port ${host}:${port} unexpected error , ${seq}`, e);
75 reject(e); // some other failure
76 }
77 server.close();
78 });
79
80 server.once('close', () => {
81 ConsoleLog(`isPortInUse: port ${host}:${port} is free, ${seq}`);
82 resolve(false);
83 });
84
85 server.listen(port, host, () => {
86 server.close();
87 });
88 });
89 }
90
91 /**
92 * Checks to see if the port is in use by creating a server on that port if a localhost or alias

Callers 1

isPortInUseExMethod · 0.45

Calls 2

ConsoleLogFunction · 0.70
closeMethod · 0.65

Tested by

no test coverage detected