You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
798 B
36 lines
798 B
|
3 months ago
|
// 最简单的TCP端口检查脚本
|
||
|
|
const net = require('net');
|
||
|
|
|
||
|
|
const PORT = 3001;
|
||
|
|
const HOST = 'localhost';
|
||
|
|
|
||
|
|
console.log(`正在检查 ${HOST}:${PORT} 端口...`);
|
||
|
|
|
||
|
|
const client = new net.Socket();
|
||
|
|
let isOpen = false;
|
||
|
|
|
||
|
|
client.setTimeout(2000);
|
||
|
|
|
||
|
|
client.connect(PORT, HOST, () => {
|
||
|
|
isOpen = true;
|
||
|
|
console.log(`✅ 端口 ${PORT} 已开放!服务器正在运行。`);
|
||
|
|
client.destroy();
|
||
|
|
});
|
||
|
|
|
||
|
|
client.on('error', (e) => {
|
||
|
|
if (e.code === 'ECONNREFUSED') {
|
||
|
|
console.log(`❌ 端口 ${PORT} 未开放或被拒绝连接。`);
|
||
|
|
} else {
|
||
|
|
console.error(`❌ 连接错误: ${e.message}`);
|
||
|
|
}
|
||
|
|
client.destroy();
|
||
|
|
});
|
||
|
|
|
||
|
|
client.on('timeout', () => {
|
||
|
|
console.log(`❌ 端口 ${PORT} 连接超时。`);
|
||
|
|
client.destroy();
|
||
|
|
});
|
||
|
|
|
||
|
|
client.on('close', () => {
|
||
|
|
console.log('\n检查完成。');
|
||
|
|
});
|