Hi I'm new to socket programming and i wrote a very simple port scanner.I'm having problem with the code
----------------------------------------------------------------------------------------------------------
Code:
#include<stdio.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<stdlib.h>
#include<netdb.h>
int main()
{
char ip[32];
int i,err,net;
struct sockaddr_in sa;
net=socket(AF_INET,SOCK_STREAM,0);
sa.sin_family=AF_INET;
printf("enter the ip");
scanf("%s",ip);
sa.sin_addr.s_addr=inet_addr(ip);
for(i=1;i<20000;i++)
{
sa.sin_port=htons(i);
if((connect(net,(struct sockaddr *)&sa,sizeof(sa)))>=0)
{
printf("\n%d is open",i);
}
}
printf("\n");
return ;
}
-------------------------------------------------------------------------------------------------------
the output for this is :
enter the ip
127.0.0.1
22 is open
Now here is another version of the above
------------------------------------------------------------------------------------------------------
Code:
#include<stdio.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<stdlib.h>
#include<netdb.h>
int main()
{
char addr[32];
int i,net;
struct sockaddr_in sa;
printf("enter the ip");
scanf("%s",addr);
sa.sin_addr.s_addr=inet_addr(addr);
for(i=1;i<20000;i++)
{
sa.sin_family=AF_INET;
sa.sin_port=htons(i);
net=socket(AF_INET,SOCK_STREAM,0);
if((connect(net,(struct sockaddr *)&sa,sizeof(sa)))>=0)
{
printf("\n%d is open",i);
}
}
return ;
}
--------------------------------------------------------------------------------------------------------
The output for his is:
enter the ip
127.0.0.1
22 is open
25 is open
111 is open
631 is open
1 now only difference in the above two program is that I'm calling the socket call and sa.sin_family=AF_INET in for loop in second program .Why is the first not working.
2.Also my second question is i have assigned
char ADDR[32];
.How many space do i need to assign in char for storing the ip.
3


K and the last query I have is that when i run the second program giving ip of computer on my lan it works fine but when i try it against say some site (say if I enter ip of google ) it doesnt work and no output is given.