ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I missed some details, sorry.
The thing is I starting few instances of my sopcast frontend (written in Qt4) simultaneously. Every instance needs free port to redirect stream. Right now I set a port number manually for every program instance, but I'd like to have it done automatically.
Please post the code you are working with where you have these questions. And consider also dugan's suggestion; however since I have no idea if you are opening TCP or UDP, or acting as a client or server, or if you are talking about a Serial Port, I really can't offer much in the way of better advice than to input 0.
As I wrote above, I built frontend, which starts command-line sopcast tool: sp-sc-auth.
This snippet of code starts sp-sc-auth with appropriate parameters:
it reads stream url from txtUrl and starts process like this:
/usr/bin/sp-sc-auth sop://broker.sopcast.com:3912/123456 3908 8908
There is no any problem.
However when I starting a second instance of my frontend (while the first is running), and trying to connect to some different stream (with different url), sp-sc-auth fails because 3908 and 8908 ports already in use. So I need some different free port for second instance.
The simplest way to get alternative port number is qrand routine. It works, but I'd like to have more sane solution than use random ports.
Thank you for posting the code, it clarifies that this has nothing to do with Qt programming.
I'll let someone who knows how to drive the sc-sp-auth program answer regarding how you obtain any free port, but the suggestion to try zero seems to be a valid assumption.
No real solution on a multi-tasking systems: you check in time t1 that port n1 is free; you start your sub-server; the sub-server tries bind(2) in time t2, and it might find that port n1 is already in use: someone else bound to it between t1 and t2.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.