What would be the correct way to get the port number for a free port, using C in Linux?
I've tried to create() and bind() a socket without specifying the port, and that seems to work in some cases (using gcc, and some Linux dist).
When compiling with the Intel Icc64 compiler I suddenly had to do a "dummy send" with the created socket to get a port != 0.
Then, when using that technique on mashines running Slackware 13, it doesn't work. I just get the port to be 0.
So, is there a "correct" way to do this?
I'm trying to find out free port numbers that clients can use to receive messages from a server, and I would like to retreive them before actually sending anything between server/client.
What's create()? The irony of including the "e" ...
Your question is not clear to me - do you wish to discern which port is available on the local box (server) so that you can bind() and listen() on that particular port, *or* do you wish to know on the client which port can be used for bi-directional communication to the server (this port will be chosen automatically if you specify 0 for sin_port, and it will be used for *outgoing* packets unless you are going to listen() on that port).
Probably best to post what you have written so far.
I think that it stopped working on a particular compiler is indicative of problems in your code. An important but uninitialized variable perhaps, left to have an undefined value and ending up being a number of different things depending on compiler, system, and circumstances.