Jump to content
Sign in to follow this  
linuxuser

Network Programming:Simple portscan

Recommended Posts

Writing a C++ socket program that scans given range of port number and tells u which ones are available is not hard at all.

The following code does just that. It takes 3 arguments. First one is host ip address, second one is lower range and third one is higher range of port #.

 

Eg--> Nameof Exe( portscan.exe) 127.0.0.1 0 80

 

 

#include<stdio.h>

#include<winsock2.h>

 

int main( int argc, char* argv[] ){

if( argc != 4 ){

printf("Invalid arguments");

return 0;

}

 

SOCKET s;

struct sockaddr_in address;

 

s = socket( AF_INET, SOCK_STREAM, 0 );

address.sin_family = AF_INET;

address.sin_addr.s_addr = inet_addr((char *)argv[1]);//INADDR_ANY;

 

int port = 0;

for( port=atoi( argv[2]); port<= atoi( argv[3] ); port++ ){

address.sin_port = htons( port );

if( connect(s, (struct sockaddr*)&address, sizeof( address )) == 0 ){

printf("Port:");

printf("%d", port );

printf(" open\n" );

closesocket( s );

s = socket( AF_INET, SOCK_STREAM, 0 );

}

}

closesocket( s );

return 1;

}

Share this post


Link to post
Share on other sites

yaa i can check your code too but I lost my C compiler ... umm anyway I have copied down in my box and will check that out .. thanx for that

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.