Hello friends i have written a basic port scanner with options like :

1) The program will prompt for 3 command line arguments viz :
ip/hostname
start_port <mandatory>
end_port <optional>

out of this the end_port is optional if the user provides the
end port than it will scan till that port or else if the end port
is not provided then the scan will go from start_port till 65535.

2) The program open a log file named scanresults.txt at c:\
which contains all the scan results in it so user can check it after
the run.

3) The program shows only those ports, on the screen, which are open it will not display closed ports but they are logged into the file.

after the program run is complete user can check for the file at
c:\scanresults.txt


/****** problem ******/

But the real problem is that suppose when i am running the application as

c:\>port 111.111.111.111 138 139
where 138 is the start port and 139 is the end port
then it is showing both the ports closed.

but in turn if i am running the same application as
c:\>port 111.111.111.111 139 140
where 139 is the start port and 140 is the end port
then it is showing me port 139 open and 140 closed.

inshort if it encounters first port as closed it shows all the following ports closed
but it is not true vice versa.
i have gone through the code many a times but i can't figure it out what is the
problem..

anybody pls go through the code and pls tell me why its happening like this..

i will be very grateful .. thanks millions ..
/************************************************** ******/

the code is as follows :

******************** CODE ******************
#include <stdio.h>
#include <winsock.h>
FILE *fp;
char file_to_open [60];
int sock;
int count = 0;
int start_port;
int end_port = 0;
WSADATA wsaData;
struct hostent *host;
struct sockaddr_in dest;

void OpenFiles()
{
sprintf(file_to_open , "/scanresults.txt");
fp = fopen( file_to_open , "w" );
if( fp == NULL )
{
printf("File Open Error\n");
exit(1);
}
fprintf(fp,"\n************************************ ******************************" );
fprintf(fp,"\n ROOTSCAN LOG");
fprintf(fp,"\n************************************ ******************************\n" );
}

void CloseFiles()
{
fclose( fp );
}

int main(int argc, char *argv[])
{
if(argc < 3)
{
printf("\n\tUsage: %s <host/ip> <start_port> [end_port]\n", argv[0]);
exit(-1);
}

OpenFiles();

//windows specific code here
WSAStartup(MAKEWORD(1, 1), &wsaData);
//end here
start_port = atoi(argv[2]);

if( argc > 3)
end_port = atoi( argv[3] );
else
end_port = 65535;

if((host = gethostbyname(argv[1])) == NULL)
{
printf("Couldn't resolve %s\n", argv[1]);
exit(-1);
}

for(count = start_port; count <= end_port; count++)
{
if((sock = socket(AF_INET, SOCK_STREAM, 0)) == 0)
{
printf("Couldn't make socket!\n");
exit(-1);
}

dest.sin_family = AF_INET;
dest.sin_port = htons(count);
dest.sin_addr = *((struct in_addr *)host->h_addr);

if(connect(sock, (struct sockaddr *)&dest, sizeof(struct sockaddr)) == -1)
{
fprintf( fp ,"Port %5d Closed\n",count);
shutdown(sock, 2);//use this instead of close.
//windows...
WSACleanup();
//endsleep(1);
}
else
{
printf("Port %d \t Open\n", count);
fprintf( fp ,"Port %5d Open\n", count);
shutdown(sock, 2);//use this instead of close.
//windows...
WSACleanup();
//end
}
}//for loop end
CloseFiles();
return(0);
}



pls help waiting...