为什么 Mac OS 指定的 TCP 端口没有监听?

Why is the Mac OS specified TCP port not listening?

我有问题。

我用C++写程序,指定端口60000。

m_pConn.sin_port = htons( 60000 );

然后,当程序 (TCPFileSe) 启动时,我注意到监听端口不是 60000。

这是我的监听端口列表..

christof-kims-Mac:~ christof-kim$ lsof -i | grep LISTEN
rapportd    639 christof-kim    3u  IPv4 0x1b8366a443833957      0t0  TCP *:49234 (LISTEN)
rapportd    639 christof-kim    4u  IPv6 0x1b8366a4437c0def      0t0  TCP *:49234 (LISTEN)
**TCPFileSe 24454 christof-kim    3u  IPv4 0x1b8366a446445957      0t0  TCP *:50087 (LISTEN)**

当我第二个程序开始时..

christof-kims-Mac:~ christof-kim$ lsof -i | grep LISTEN
rapportd    639 christof-kim    3u  IPv4 0x1b8366a443833957      0t0  TCP *:49234 (LISTEN)
rapportd    639 christof-kim    4u  IPv6 0x1b8366a4437c0def      0t0  TCP *:49234 (LISTEN)
**TCPFileSe 24647 christof-kim    3u  IPv4 0x1b8366a446445957      0t0  TCP *:50117 (LISTEN)**

我不明白为什么LISTENING端口不是60000

你能帮我看看为什么LISTENING端口是50087、50117吗? 或者,Mac OS 是 Not supported specified port???

我需要指定端口(例如 60000)。

此代码是为 Mac OS 10.13.6 编写的 c++ 语言。 我试过 Mac OS 控制台线工具。

sockaddr_in m_pConn;

m_pConn.sin_family = AF_INET;
m_pConn.sin_addr.s_addr = inet_addr( "0.0.0.0" );
m_pConn.sin_port = htons( 60000 );

bind(m_nSocket, (struct sockaddr *) &m_pConn, sizeof(m_pConn));
socklen_t len = sizeof(m_pConn);

// Listen on the socket.
if ( listen( m_nSocket, 1 ) == -1 )
    printf( "Error listening on socket.\n");

问题是我用了using namespace std

//using namespace std;

所以,我添加了以下代码,然后它就起作用了。

using std::ifstream;
using std::ios;

问题可能与此有关: Compiling code that uses socket function bind() with libcxx fails