LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 09-20-2012, 04:52 AM   #1
yahoosam
Member
 
Registered: Jun 2012
Posts: 79

Rep: Reputation: Disabled
c code to get next free port


i am using
netstat -tulpn | grep Port_no
&
netstat -natp | grep Port_no

to get information about free available ports.is there any function so that i could use in c program to return next free port to the calling function.
 
Old 09-20-2012, 01:22 PM   #2
hean01
LQ Newbie
 
Registered: Aug 2012
Location: Norrkoping, Sweden
Distribution: Fedora, Ubuntu, CentOS, RHEL
Posts: 23

Rep: Reputation: Disabled
You can use bind() to detect if a port is in use or not and you can also
use port 0 and bind will use the next available high port.


Code:
struct sockaddr_in sin;
int socket, port = 22;

socket = socket(AF_INET, SOCK_STREAM, 0));
if(socket == -1)
  return -1;

sin.sin_port = htons(port);
sin.sin_addr.s_addr = 0;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_family = AF_INET;

if (bind(socket, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) == -1) {
  if (errno == EADDRINUSE) 
    printf("Port in use");
  return -1;
}
If port 0 was used and bind selects next free port then use following code to
get socket info and the port that is used.

Code:
struct sockaddr_in sin;
socklen_t len = sizeof(sin);
if (getsockname(sock, (struct sockaddr *)&sin, &len) != -1)
  printf("port number %d\n", ntohs(sin.sin_port));
Regards,

Henrik Andersson
 
1 members found this post helpful.
Old 09-22-2012, 08:14 AM   #3
yahoosam
Member
 
Registered: Jun 2012
Posts: 79

Original Poster
Rep: Reputation: Disabled
Than Q !! vry much sir..
i added some more flavor & can u guess,,,the code is really delicious..
at the beginning i was just storing the result of commands in file &
if(fp==NULL) print"Port is available"

you made it stander way,Thanx again
 
  


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
Source code for free command? smeezekitty Linux - General 2 12-08-2009 01:04 PM
LXer: Linux and Free/Open Source Software: Why Code For Free? Devs Speak! LXer Syndicated Linux News 0 07-29-2009 07:10 PM
LXer: Linux and Free/Open Source Software: Why Code For Free? (part 1) LXer Syndicated Linux News 0 07-24-2009 06:00 PM
free code for SID ztdep Programming 1 04-05-2007 07:00 AM
Why code for free when you could be getting paid? librano General 15 04-01-2006 02:10 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 04:52 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