The Port Scanner

Your Goal: Make a port scanner.

The Client:  It should do the following

For every port on euclid.nmu.edu from 1 ... 99 inclusive, you should try and open that port.  Tell the user if it opens or fails to open. If it does open, close it.

Sample output

No: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Yes: 21 22
No: 23 24
Yes: 25
No: 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
Yes: 80
No: 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

Grading

Points

Task

1

Every variable has a comment describing it's use

-2

Code indented wrong

1

Turned in before Monday Jan 21 4:00pm

-1

Turned in after Wednesday Jan 23 at 4:00pm

-1

Every failure to error check.

1

Server prints the IP number of the server.

1

Can check one port.

1

Can check ports 1 .. 99.

1

Output matches the format above EXACTLY.

1

Can read the hostname and port range from the command line.

Hints

My version took 67 lines of code including whitespace.

One can find the values on the command line like this.

One can flush cout without printing a carriage return like this
cout << "HI" << flush;

Some people have found the following code useful

int MakeSocket(char *host, int port) {
        int s;
        int len;
        struct sockaddr_in sa;
        struct hostent *hp;
        struct servent *sp;
        int portnum;
        int ret;
                                                                                                                            
        hp = gethostbyname(host);
        if (hp == 0) {
                perror("HP");
                exit(1);
        }
        bcopy((char *)hp->h_addr, (char *)&sa.sin_addr, hp->h_length);
        sa.sin_family = hp->h_addrtype;
        sa.sin_port = htons(port);
        s = socket(hp->h_addrtype, SOCK_STREAM, 0);
        ret = connect(s, (struct sockaddr *)&sa, sizeof(sa));
        return ret;
}