Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 10-09-2008, 09:44 PM   #1
LQ Newbie
Registered: Aug 2008
Posts: 27

Rep: Reputation: 15
How to use poll() with non-blocking sockets

I have this project where I have to implement a simple port scanner in C++ for Linux. The program should take as input a target IP address, beginning port number, and ending port number. After recieving the above info, the program should display a list of open ports. I finally got it working but it takes a very long time to search a wide range of ports(100 to 1000). I was told that the run time would greatly decrease if I had several non -blocking sockets and apply poll() or use multi-threading(which is dificult so I heard). With this in mind, I am trying to find out how to open several non -blocking sockets and apply poll().However, I am confused about how to do this. Currently, after a user enters ip and the start and end ports I open a socket inside a for loop(goes from the start port to end port). Any advice/example would greatly be appreciated.
Old 10-10-2008, 12:48 PM   #2
LQ Newbie
Registered: May 2008
Posts: 2

Rep: Reputation: 0
You could do this with poll, but personally, I don't know why you would not use select() instead and just add all of the sockets to the fd_sets for read and except.
Old 10-10-2008, 01:36 PM   #3
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
info libc can tell you pretty much everything you need to know about your project. You just have to take the time to look through it and experiment with the code.
Old 10-12-2008, 09:55 AM   #4
LQ Newbie
Registered: Aug 2008
Posts: 27

Original Poster
Rep: Reputation: 15
Thanks for the input! However, this is my first time working with sockets and isn't quite familiar with select(). By any chance can you give me input on adding select() to my program. Below is the code where I am opening a socket and checking if it is open/close:
for (port=start; port<=end; port++)

soc = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); //created the tcp socket

    fcntl(soc,F_SETFL, O_NONBLOCK);
    memset( &servaddr, 0, sizeof(servaddr));
   servaddr.sin_family = AF_INET;
   servaddr.sin_port = htons(port); //set the portno

   hostaddr = gethostbyname( argv[1] ); //get the ip 1st argument
   memcpy(&servaddr.sin_addr, hostaddr->h_addr, hostaddr->h_length);
   conres = connect(sd, (struct sockaddr*)&servaddr, sizeof(servaddr));
   if (conres == -1)
   printf("Port %d is closed\n", port);
   printf("Port %d is open\n",port);
    return 0;
As noted at this time I am only opening one socket, where will I open more sockets. Should I open the sockets before the for loop. Any advice on this will be greatly appreciated!!!


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Program to forward tcp sockets to unix domain sockets mikepol Linux - Networking 0 09-27-2007 09:49 AM
Poll: (Without the poll) - How is Linux used in your workplace? SlowCoder General 13 09-11-2007 11:03 PM
Problem w/ sockets, fdopen, and blocking. 95se Programming 3 11-17-2005 12:38 PM
Non blocking sockets in Perl ivanatora Programming 3 08-19-2005 02:14 AM
Set/Unset Non-Blocking sockets? zer0python Programming 2 01-04-2004 01:31 AM

All times are GMT -5. The time now is 02:08 PM.

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