OK, I found the problem.
I was trying to scan port 1025.
First I put this above the connect() call to check that we were trying the correct address and port.
printf("## Checking ip %s\n", inet_ntoa(tmp->sock.sin_addr));
printf("## Port number is %d\n", ntohs(tmp->sock.sin_port));
printf("## Reversed Port number is %d\n", tmp->sock.sin_port);
Obviously, something is wrong with the port number. It seems to be reversed.
## Checking ip 192.168.0.21
## Port number is 260
## Reversed Port number is 1025
So in the add_to_sock_stuff function I put:
printf("## port number is now %d\n", port);
So we can see the port number being passed to add_to_stock_stuff is already reversed. We then reverse it again:
## port number is now 260
which gives us the wrong value.
socks->sock.sin_port = htons(port);
Essentially, we are passing the port number to htons twice which is equivalent to not calling htons at all.
You can trace back from add_to_stock_stuff to find the first time that ntohs is called on the port number. One of the ntohs calls must be removed.
On a different issue, you could make the interface a little simpler. You should give a sample command line in your usage instructions. Also, maybe if only the start ip address and port number is specified, you could just scan that one computer.
Let me know how you get on.
P.S Run 'netstat -a' to find open ports on your computer. No need to install telnet.