3

I would like to have two windows applications which communicate over TCP/IP using windows sockets. In this, I want my program to automatically choose the available free port for connection establishment.

Is there a way to find the free TCP port using a C or C++ program?

To be precise, I'd like to automatically detect a free port on the Server side and let the client know the same port(to connect to server).

Thanks for your time!

Krish
  • 31
  • 1
  • 3
  • You can brute force high (non-standard) ports. (where both the server and client will try and the client will keep searching till a suitable port establishes a connection (a handshake) ) – Ronnie Jan 06 '16 at 14:24
  • Finding a free port is clear from [here](http://stackoverflow.com/questions/1365265/on-localhost-how-to-pick-a-free-port-number). But how this port info could be communicated to Client? – Krish Jan 06 '16 at 15:07
  • 1
    Why do you want to do this? This is going to be a pain with firewalls and whatnot. – CodeCaster Jan 06 '16 at 19:32
  • @Ronnie: simply scanning the ports is not enough. There will be many open ports that the client could potentially connect to, and not all of them will offer a handshake protocol to discover the type of server connected to. But if you have your own server offer a handshake greeting to connected clients, your client can look for that greeting and ignore any other servers it connects to that don't offer it. – Remy Lebeau Jan 06 '16 at 19:33
  • @RemyLebeau, I was trying to say that only. Maybe my words didn't express my expression :) – Ronnie Jan 07 '16 at 07:56
  • @Krish The application will execute the url `http://localhost:47162/` and the browser process will launch and show the user that url. – Ian Boyd Jan 14 '22 at 18:50
  • @IanBoyd what does that have to do with this issue? – Remy Lebeau Jan 15 '22 at 02:09
  • @RemyLebeau A question was asked *'how can this port information be communicated to the client?'*. I was answering the question - so the person would have the answer. – Ian Boyd Jan 15 '22 at 02:41

1 Answers1

5

I'd like to automatically detect a free port on the Server side

Simply bind() your listening TCP socket to port 0. The OS will pick an available port for you. You can then use getsockname() to retrieve the port that was selected.

let the client know the same port(to connect to server).

You would have to publish the selected TCP listening port somewhere that the client can query it from when needed.

If the client and server are on the same network subnet, one simple solution is to have the server open a separate listening UDP socket on a fixed port, and then have the client send a UDP broadcast to the subnet broadcast IP on that port. When the server receives the broadcast, it can send a reply back to the client specifying the TCP listening port. Then the client can connect to the TCP server on that port.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Unfortunately sometimes we're not using sockets for listening ourselves, and instead pass that off to [a Windows api](https://learn.microsoft.com/en-us/windows/win32/api/http/nf-http-httpaddurl). So before we call the function, we need a way to *"find a free TCP port in Windows using 'C'"* – Ian Boyd Jan 14 '22 at 18:52
  • @IanBoyd Sorry, there is simply no way to do that. Only the OS knows which ports are free, and there is no API to query that info. Besides, even if such a query were possible, it would be a [TOCTTOU race condition](https://en.wikipedia.org/wiki/Time-of-check_to_time-of-use) anyway, as the found port might get used by someone else before you have a chance to open it yourself. The only safe (and sane) way to use a free port is to let the OS pick it for you at the time when `bind()`'ing or `connect()`'ing a socket. – Remy Lebeau Jan 15 '22 at 02:11
  • @IanBoyd In any case, I don't see what any of this has to do with that API you linked to (`HttpAddUrl()`). What is the ACTUAL issue you are trying to solve exactly? Be specific. This is bordering on [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) territory. That API appears to require the pre-existing TCP port that the HTTP server is actually using to listen for inbound connections. So finding a *free* port would not make sense anyway. – Remy Lebeau Jan 15 '22 at 02:16
  • I need to find a free TCP port - so i can listen on that port. And obviously `bind()` is not an option, because i'm not listening with the sockets api. So, we need an answer to the question "How to find a free TCP port in Windows using C". Any ideas? – Ian Boyd Jan 15 '22 at 02:39
  • "*I need to find a free TCP port - so i can listen on that port*" - you MUST either `bind()` a listening socket, or let an API open it for you, and then you query the API for its used port, if needed. Those are your options. If you can't use `bind()` yourself, and if there is no API to query the server's port, then you will just have to analyze the system's routing tables to see what port is being used AFTER the server begins running. "*we need an answer to the question "How to find a free TCP port"*" - the answer is: YOU CAN'T!! Not for the purpose you intent to use it. – Remy Lebeau Jan 15 '22 at 02:59
  • 1
    "The answer is: YOU CAN'T!!". Excellent; you should put that in the form of an answer. – Ian Boyd Jan 15 '22 at 03:03
  • @IanBoyd What I said is not an answer to this post's original question, it is in answer to a separate comment which really should have been posted as a new question since it involves a completely different scenario than this post's original question. – Remy Lebeau Jan 15 '22 at 03:12
  • At this point i think it would more productive if i started a new question, *"How to find a free TCP Port in windows using 'C'"* where we can then post the answer. – Ian Boyd Jan 15 '22 at 03:20
  • @IanBoyd "*At this point i think it would more productive if i started a new question*"" - that is exactly what I said to do. However, "*How to find a free TCP Port in windows using 'C'*" is not a good question for that post. A more appropriate question would be more like "*How to find a free TCP Port for Windows HTTP Server API to listen on for HTTP requests?*" – Remy Lebeau Jan 15 '22 at 04:27
  • I wouldn't want to mention anything about **why** i need to find a free port. Because then people tend to answer to the (possibly) hypothetical motivation for the question ([not that questions need motivation](https://devblogs.microsoft.com/oldnewthing/20150323-00/?p=44413)), rather than answering the asked question. – Ian Boyd Jan 15 '22 at 04:33
  • @IanBoyd this type of question is very much dependant on WHY you need the port, so DON'T leave out that detail. – Remy Lebeau Jan 15 '22 at 05:44