Search code examples
c++macostcpportlisten

Why is the Mac OS specified TCP port not listening?


I have a problem.

I wrote program in C++, specified port 60000.

m_pConn.sin_port = htons( 60000 );

and then, when program (TCPFileSe) started, I noticed that LISTENING port is not 60000.

Here is my listening port list..

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)**

when I program second started..

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)**

I don't understand why LISTENING port is not 60000.

Could you help me why LISTENING port is 50087, 50117? Or, Mac OS is Not supported specified port???

I need for specified port (e.g 60000).

This code is just writed language c++ for Mac OS 10.13.6. I've tried on Mac OS console line tool.

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");

Solution

  • The issue is that I used using namespace std.

    //using namespace std;
    

    So, I added the following code, and it then worked.

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

    Maybe the issue is related to this: Compiling code that uses socket function bind() with libcxx fails