6

I wrote an server application in erlang and a client in C#. They communicate through 3 TCP ports. Port numbers are hardcoded. Now I'd like to do this dynamically. This is my first time doing network programming, so please pardon my inability to use proper terminology :-D

What I would like to do is make a supervisor which would accept a TCP connection from a client on a previously known port (say, 10000, or whatever), then find 3 free ports, start a server application on those 3 ports and tell the client those port numbers so client can connect to the server.

My particular problem is: how do I find 3 ports which are not in use? (clarification: which module:fun() to use to find a free port?)

My general problem is: I'm sure this kind of stuff (one server allocating ports and redirecting clients) is quite common network programming problem and there should be a bunch of (erlang-specific or general) resources about this, but I just don't have the terminology to google it out.

3 Answers 3

14

According to the Erlang documentation here, if the Port argument to the gen_tcp:listen/2 function is 0, then the OS will assign any available port to the socket. The latter can then be retreived using inet:port/1 .

You can therefore do something like this :

{ok, Listen} = gen_tcp:listen(0, [Options]),
Port = inet:port(Listen).
2
  • Yes, that makes sense. Then I just communicate those back to the supervisor who relay those to the client. Nice, thanks!
    – dijxtra
    Jun 23, 2011 at 1:42
  • 5
    Elixir version: {:ok, listen} = :gen_tcp.listen(0, []); {:ok, port} = :inet.port(listen); port
    – Henrik N
    Mar 3, 2017 at 21:58
3

just in case you didn't know that - you dont have to allocate new ports for each client, it's perfectly fine to have all clients to connect to same ports

UPDATE:

if there is a reason to allocate new ports for incoming clients then it's far beyond your first "introduction to network programming" program.

separate ports could mean you want to completely isolate environments of different groups of clients. it's comparable to providing completely different IP addresses to connect to. if you want to write a simple ping-pong program - you don't need it. and i honestly believe you will never need to use such solution in your whole life - that's how incredibly rarely it is.

regarding cpu/ports overhead - allocating ports and starting a server that listens to that port is already far bigger overhead than accepting clients on same port.

8
  • Yeah, but supervisor will spawn a brand new server for each client. But, you are right, I could use only 3 ports in this situation also... Hm. What's better: to have each server allocate 3 distinct ports (port usage overhead), or to have only 3 global ports and then have a dedicated server redirect traffic from each client to it's respective server (CPU overhead)?
    – dijxtra
    Jun 23, 2011 at 10:09
  • i have a feeling you misunderstood a tutorial about network programming at some point. i'll update my answer.
    – keymone
    Jun 23, 2011 at 11:26
  • Yes, you are right, I would like to isolate groups of clients. I'm doing a simulator server. Currently my app is one instance of a simulator talking to one client, and I'd like to write a server which assigns a brand new simulator for each client, so several clients can run several distinct isolated simulators (only thing shared would be the entry point where the client asks for a new simualtor). If one simulator goes belly up, I don't want it to take other simulators underwater. That's why I was thinking separate port triplet for each client.
    – dijxtra
    Jun 23, 2011 at 11:40
  • what stops you from running simulator for each accepted client on one port?
    – keymone
    Jun 23, 2011 at 11:45
  • Again, isolation. There are 3 things a client can do with a simulator: GET a parameter, SET a parameter and do an ACTION. GET gen_server is just simply relaying state variables from server to client and should be quite stable, but SET and ACTION servers have a potential for crashing since they do some real action. So I'd like to isolate each server on it's own port, so if one of those 3 gen_servers crashes, it doesn't take the other two with him. Am I doing this right or am I taking this isolation thing to far? (I'm an erlang / network programming noob, so have mercy :-D)
    – dijxtra
    Jun 23, 2011 at 12:10
1

You need to avoid commonly known ports, ftp, http, smtp etc, But I don't think there is any master list of which ports other software uses that you should avoid. I think your best bet is to come up with a range of ports you want to use. Check at runtime if anybody else answers ( is using the ports ) on the numbers you choose dynamically, if not issue it to the client.

2
  • "Check at runtime if anybody else answers ( is using the ports ) on the numbers you choose dynamically" - well, yes. But I don't know how to do that :-D That's exactly what I asked. I simply cannot google it out. So, how to I find out if anybody is using a port?
    – dijxtra
    Jun 23, 2011 at 1:15
  • 2
    There actually is a master list of ports used by software: iana.org/assignments/port-numbers Jul 1, 2011 at 22:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.