LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-06-2017, 02:59 PM   #1
Teufel
Member
 
Registered: Apr 2012
Distribution: Gentoo
Posts: 616

Rep: Reputation: 142Reputation: 142
QT: How to get free port number?


Hi!
I have to redirect sopcast (sp-sc-auth) stream to any free port.
How do I get a free port number using Qt?
Thanks
 
Old 04-06-2017, 04:43 PM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,225

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Use Port 0
 
1 members found this post helpful.
Old 04-07-2017, 02:43 AM   #3
Teufel
Member
 
Registered: Apr 2012
Distribution: Gentoo
Posts: 616

Original Poster
Rep: Reputation: 142Reputation: 142
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.
 
Old 04-07-2017, 08:29 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Before calling bind(2), set port number to zero so the kernel will pick up a port-number for you.
 
Old 04-07-2017, 08:50 AM   #5
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by Teufel View Post
I missed some details, sorry.
Yes, I noticed this as well.

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.
 
Old 04-07-2017, 09:24 AM   #6
Teufel
Member
 
Registered: Apr 2012
Distribution: Gentoo
Posts: 616

Original Poster
Rep: Reputation: 142Reputation: 142
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:
Code:
void Sop::connectClicked(){
    QString command = "/usr/bin/sp-sc-auth " + txtUrl->text() + "  3908 8908";
    
    stopped = FALSE;
    process = new QProcess;
    connect (process, SIGNAL(readyReadStandardOutput()), this, SLOT(processOutput())); 
    connect (process, SIGNAL(readyReadStandardError()), this, SLOT(processOutput()));  
    connect (process, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(processRestart(int, QProcess::ExitStatus)));
    
    process->start (command);

}
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.
 
Old 04-07-2017, 09:49 AM   #7
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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.
 
Old 04-07-2017, 11:00 AM   #8
Teufel
Member
 
Registered: Apr 2012
Distribution: Gentoo
Posts: 616

Original Poster
Rep: Reputation: 142Reputation: 142
OK, attempt to use zero allocates free port from kernel pool.
How to allocate port and get allocated port number?
Any Qt example?

Last edited by Teufel; 04-07-2017 at 11:02 AM.
 
Old 04-07-2017, 11:17 AM   #9
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Port number used by server when using dynamic port forwarding in SSH? kreeder Linux - Networking 4 11-21-2011 02:07 PM
USB to serail port adaptor COM port number areftaidi Linux - Software 2 09-25-2007 01:05 AM
Port Number? Dakkar Linux - Newbie 5 11-06-2004 05:50 PM
Ip and port number Alien18 Linux - Networking 1 09-13-2004 09:40 PM
right port number gogo Linux - General 1 05-07-2001 03:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration