Port scanner in C

Discussion in 'C' started by shukad33, Jun 9, 2008.

  1. shukad33

    shukad33 New Member

    Joined:
    Jun 9, 2008
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Port scanner in C
    Code:
    #include<stdio.h>
    #include<sys/socket.h>
    #include<sys/types.h>
    #include<stdlib.h>
    #include<netdb.h>
    int main(int argc,char **argv)
    {
    	int i,err,net;
    	struct hostent *host;
    	struct sockaddr_in sa;
    	if(argc!=2)
    	{
    		printf("Error...Usage :%s ip-address",argv[0]);
    		exit(0);
    	}
    	for(i=1;i<20000;i++)
    	{
    		sa.sin_family=AF_INET;
    		sa.sin_port=htons(i);
    		sa.sin_addr.s_addr=inet_addr(argv[1]);
    		net=socket(AF_INET,SOCK_STREAM,0);
    		err=connect(net,(struct sockaddr_in *)&sa,sizeof(sa));
    		if(err>=0)
    		{
    			printf("\n%d is open",i);
    		}
    	}
    	printf("\n");
     }
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Can you explain how do I get the following header files. sys/socket.h
     
  3. madlex

    madlex New Member

    Joined:
    Jun 6, 2008
    Messages:
    12
    Likes Received:
    3
    Trophy Points:
    0
    Occupation:
    Technical Project Manager
    Location:
    Bucharest,RO
    You are opening 20000 connections and you are closing none of them.
    You should close connections in your for.

    Code:
    if(err>=0)
            {
                printf("\n%d is open",i);
            }
    This is wrong. Values over zero may express an error, not a succesful connection.
    You should really interpret this values and the print a correct output.
    (hint: will return 0 on success).

    You didn't specify the platform. For windows platform is necesary to call WSAStartup, to initialize the winsock library, otherwise you will receive errors from each call made to winsock's API.
     
  4. shukad33

    shukad33 New Member

    Joined:
    Jun 9, 2008
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Its a unix platform supported program. Hence it is assumed to be available in UNIX...
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice