Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Linux: get socket source port used after connect()

Posted on 2008-06-21
2
Medium Priority
?
3,329 Views
Last Modified: 2010-04-21
Hello, in Linux, how can I get the source port used by a socket after making a call to the connect system call? I want to save the source port used for later use.
Alternatively, how can I find out which source ports are not used, then I could just make a call to bind with that source port.

thanks
0
Comment
Question by:rocketsbay
2 Comments
 
LVL 35

Accepted Solution

by:
Duncan Roe earned 300 total points
ID: 21839437
Use getsockname() and cast the returned sockaddr to sockaddr_in. It then contains the port & IP.
getpeername() gets the remote port & IP.
To find an unused port, you have to call bind() on different ones until you don't get an error. Or you can call bind with a zero port and discover what port you got.
0
 

Author Closing Comment

by:rocketsbay
ID: 31469442
Thanks a lot, it`s exactly what I need.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question