/*=================================================================== | | Portcheck version 1 | Written by Jonathan Harm (110983@lycos.com) | http://change.to/opensource | ========================================================================*/ #include #include #include #include #include int main(viod) { int sock; unsigned int portnum; //portnum can be short or int, but has to be unsigned char servIP[16], *servIP_p; //the array is 16 because that is the maximum length an IP number can be(including the dots) struct sockaddr_in serveraddr; servIP_p = servIP; //initialize the pointer printf("\nWhat IP do you want to connect to?\n"); scanf("%s", servIP); printf("\n\nWhat port?\n"); scanf("%u", &portnum); if( (sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { fprintf(stderr, "errer creating socket\n"); exit(1); } memset(&serveraddr, 0, sizeof(struct sockaddr_in)); //zero out the struct serveraddr.sin_family = AF_INET; serveraddr.sin_port = htons(portnum); //make sure the portnumber is in networking byte order serveraddr.sin_addr.s_addr = inet_addr(servIP_p); //convert dotted quad to networking byte order binary number if( connect(sock, (struct sockaddr *) &serveraddr, sizeof(serveraddr)) < 0) { fprintf(stderr, "\nerrer connecting\n"); exit(1); } else { printf("\nConnected successfully!\n"); } return 0; }