简体   繁体   English

如何停止收听端口

[英]how to stop listening to a port

I have a linux application written in c++. 我有一个用c ++编写的linux应用程序。 The application listens to a socket on a certain port. 应用程序侦听某个端口上的套接字。 I implemented this using ACE Acceptor. 我使用ACE Acceptor实现了这个。 In addition the application starts postgresql database using the init script /etc/init.d/postgresql start by calling the ACE_OS::system function. 另外,应用程序使用初始化脚本/etc/init.d/postgresql通过调用ACE_OS :: system函数来启动postgresql数据库。

The problem I am having is: When the application exits, the port is still occupied. 我遇到的问题是:当应用程序退出时,端口仍然被占用。 When I run netstat I see that the postgres is listening to that port. 当我运行netstat时,我看到postgres正在侦听该端口。 (This only happens if I start postgres from the application on any given port). (这仅在我从任何给定端口上的应用程序启动postgres时发生)。

Is there a way to close the port? 有没有办法关闭港口? Why does postgres listen to that port? 为什么postgres监听那个端口?

Is there a way to close the port? 有没有办法关闭港口?

Yes. 是。 Close the socket, or set FD_CLOEXEC on the underlying file descriptor. 关闭套接字,或在底层文件描述符上设置FD_CLOEXEC。

Or ... wrap your call to the child process ( ...postgresql start ) with something that will close fds higher than stderr: 或者...用将关闭fds高于stderr的东西包装对子进程( ...postgresql start )的调用:

ACE_OS::system("perl -MPOSIX -e 'POSIX::close($_) for 3 .. sysconf(_SC_OPEN_MAX); exec @ARGV' /etc/init.d/postgresql start");

or similar. 或类似的。 Tuck that in a script to make it look nicer. 把它放在脚本中让它看起来更漂亮。

Why does postgres listen to that port? 为什么postgres监听那个端口?

Your child processes (and their children) are inheriting your open file descriptors, including the socket your c++ app opens. 您的子进程(及其子进程)将继承您的打开文件描述符,包括c ++应用程序打开的套接字。

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

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