/* COMP2303 Week 10 ** ** Address Handling Example Code ** Code will be commented in class */ #include #include #include int main(int argc, char* argv[]) { struct in_addr a; in_addr_t ipAddr; unsigned int i, hostRepresentation; if(argc != 2) { fprintf(stderr, "Usage: %s ip-address\n", argv[0]); exit(1); } /* Convert dotted decimal string to network byte order representation */ ipAddr = inet_addr(argv[1]); if(ipAddr == -1) { fprintf(stderr, "Invalid IP address: %s\n", argv[1]); exit(1); } /* Convert to host representation */ hostRepresentation = ntohl(ipAddr); /* Print out both representations as 8 digit hex numbers */ printf("%s: %08x (host order) %08x (network order)\n", argv[1], hostRepresentation, ipAddr); /* UL - unsigned long */ i = (216UL<<24) + (109UL<<16) + (125UL<<8) + 70; /* convert to network byte order */ a.s_addr = htonl(i); /* print out all representations */ printf("%08x (host order) %08x (network order) %s (string)\n", i, a.s_addr, inet_ntoa(a)); return 0; }