简体   繁体   English

Node.JS:重用套接字?

[英]Node.JS: Reusing sockets?

I tries to execute this script:我尝试执行此脚本:

require('net').createServer().listen('/tmp/test');

First time script runs successfully, but on the second start node.js throwing error: Error: listen EADDRINUSE第一次脚本成功运行,但在第二次启动时 node.js 抛出错误:错误:监听 EADDRINUSE

Is there any way to reuse sockets (unix and tcp too)?有没有办法重用套接字(unix 和 tcp 也是)?

Apparently your script/listener/server from the first call is still running.显然,您第一次调用的脚本/侦听器/服务器仍在运行。 You have to kill the script/process that is listening in order to use the same port again.您必须终止正在侦听的脚本/进程才能再次使用相同的端口。

huh? 是吧?

Node is already listening to 'tmp/test' , how would it listen to it again? 节点已经在监听'tmp / test',它将如何再次监听呢?

Like you cannot allow two applications to listen on ONE PORT, so is with SOCKET. 就像您不允许两个应用程序监听一个端口一样,SOCKET也是如此。 One should only be allowed by one. 一个只能由一个允许。

The 'right' way (according to the Node.js docs) is to close the server with server.close() , this removes the creates domain socket from the file system. “正确”的方法(根据 Node.js 文档)是使用server.close()关闭服务器,这会从文件系统中删除创建域套接字。

So if you run the server again you don't have the 'EADDRINUSE' error because it recreates|removes a new domain socket each time.因此,如果您再次运行服务器,则不会出现“EADDRINUSE”错误,因为它每次都会重新创建|删除一个新的域套接字。

On windows (with named pipes) we don't have this problem, once the server process is closed the OS automatically removes the created named pipe在 Windows(带有命名管道)上我们没有这个问题,一旦服务器进程关闭,操作系统会自动删除创建的命名管道

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM