LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 10-09-2008, 09:44 PM   #1
tnjones
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
ranton
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
ta0kira
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.
ta0kira
 
Old 10-12-2008, 09:55 AM   #4
tnjones
LQ Newbie
 
Registered: Aug 2008
Posts: 27

Original Poster
Rep: Reputation: 15
ranton,
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:
Code:
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);
       close(soc);  
    
   }
   else
   printf("Port %d is open\n",port);
  
   close(soc);  
  }
  
    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!!!
 
  


Reply


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:03 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration