Retrieving bound port/address while listening for connections (InterprocessConnectionServer)


#1

I’ve created a TCP Server class that implements the InterprocessConnectionServer, creating InterprocessConnection instances for each client that connects. Everything works great, except for one thing: I would like to be able to get the the actual port and address that the InterprocessConnectionServer is bound to, and on which it is listening. I’m creating the server with a port of 0, so there’s no way of knowing (easily) which port was provided by the OS.

It would be great if serverAddress() and serverPort() functions were provided, or alternately, if the InterprocessConnectionServer’s socket member was made protected so I could add those functions to my own TCP Server class.

Thanks!


#2

What are you trying to do?


#3

What do you mean exactly? I thought my initial post was fairly clear.

I would like to be able to get the the actual port and address that the InterprocessConnectionServer is bound to, and on which it is listening.


#4

Sorry, you were perfectly clear. I was just wondering what you were using it for … curiosity rather than helping I’m afraid.


#5

I would be using it to display the listen address and port on the UI, so I know which endpoint the client should connect to. The address isn’t so much of a problem since I can store the address that gets passed to beginWaitingForSocket(), but the port is crucial.


#6

Normally when you put up a server you’d provide a known port number, and ask the server to listen on all interfaces unless there’s some specific (Security probably) reason to have it listen on a specific interface.

I didn’T know it was possible to get bind to listen on a random address… does it work providing 0 as a port number for the server?


#7

Oh - apparently so:
https://www.dnorth.net/2012/03/17/the-port-0-trick/


#8

From JUCE looks like this is what you’d need, but … the StreamingSocket in the interprocess connection server is private.

int StreamingSocket::getBoundPort() const noexcept
{
    return SocketHelpers::getBoundPort (handle);
}

#9

https://github.com/WeAreROLI/JUCE/commit/ff6d01bc254bbaf359b474f53da9a1534b19b90e


#10

Awesome, thanks t0m! Will this be included in the next JUCE update?


#11

Yes, or you can get it now by using the develop branch from GitHub.