Hello, everyone!

I have a program (a tcp server for text messages exchange), and i want it to do the following thing: if a port is already in use - choose automatically a new one (via rand() function) and inform me about that.
I'm testing it on Solaras 5.9 if it matters.

So i do the following:

rc = bind(s, (struct sockaddr *)&sa, sizeof(sa));
   if(rc) {
        port = rand()%5500 + 5000;
        printf("Port already in use, new port is %d", port);
        sa.sin_port = htons(port);
        rc = bind(s, (struct sockaddr *)&sa, sizeof(sa));

But it works in a very strange way. Even the printf() isn't working when i add the sa.sin_port=***** string.

Maybe you can help me to understand what's actually going on?
And, maybe, some ideas on how to do the task better, as if the second time port is already in use it would be a certain crash.

6 Years
Discussion Span
Last Post by Sobakaa

Heh, this forum makes me smarter, really! ;-)
I've solved the issue using while() loop and a correct rand() call.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.