0
en_7123's Avatar, Join Date: Feb 2010
Ambitious contributor
Any other suggestions ????
0
karthigayan's Avatar, Join Date: Feb 2010
Go4Expert Member
Sorry , I forgot to explain the reason for removing the close(new) .I removed because the connect will always return 0 for success and -1 for error. So when the successful connect the value of new will be 0,and you close the 0 ( it means stdin ).So only those problem were happen .(connect will not return the file descriptor refer : man connect ) .


Then I add the statement
Code:
 close(net);
Actually I misplaced this statement .I explained it below .

Last edited by karthigayan; 10Mar2010 at 18:14..
0
karthigayan's Avatar, Join Date: Feb 2010
Go4Expert Member
There is big mistake in your port scanner function ,because of that only it doesn't work for the next attempt . I too missed to debug it in the previous code.

Actually you tried to establish socket connection 20000 times.Some got succeed . For some time you opened some number of socket descriptors ,that exceeds the system limits.So it got fail the later iteration .You need to close the socket descriptor after checking the connect ion.

Here is the debugger port scanner function , now the problems will not occur ,try this.

Code:
 void simple_port_scanner()
{
        char ip[17];

        int i,net,new;

        struct sockaddr_in sa;



        printf("Enter the ip : ");
        scanf("%s",ip);
        sa.sin_addr.s_addr=inet_addr(ip);
        for(i=1;i<20000;i++)
        {
                sa.sin_family=AF_INET;
                sa.sin_port=htons(i);
                net=socket(AF_INET,SOCK_STREAM,0);

                if((new=connect(net,(struct sockaddr *)&sa,sizeof(sa)))>=0)
                {
                        printf("%d is open:%d\n",i);
                }
                close(net); //Close the socket other wise it will exceed system limits
        }
        printf("now exit scanner\n");

}
Verify this output with your previous scanner outputs . You will see some big different .

cheers

Last edited by karthigayan; 10Mar2010 at 18:25..
0
en_7123's Avatar, Join Date: Feb 2010
Ambitious contributor
Oh finally I thinks that's the problem.Thanks