The principle is very simple, just as the reply is also said to be based on the Connect () function return value: Then the teacher said, this is full connection, cannot scan UDP port bar. Well, I forgot what I said later. In this way, the reply will be finished. Actually didn't ask what, hehe. So easy. And then stick to my code, in fact, according to the online information modification, copyright do not find me. function is everyone's: dd, sticky code ING.
1#include <sys/types.h>2#include <sys/socket.h>3#include <netinet/inch.h>4#include <time.h>5#include <stdio.h>6 7 #defineSP 18 #defineEP 10009 Ten voidmsg () One { Aprintf"Usage:scanner [IP address]\n"); - } - the intMainintargcChar*argv[]) - { - floatCosttime; - clock_t start, end; + - intp; + intSOCKFD;//returns the file descriptor of a socket A structsockaddr_in my_addr; at intRenum;//defines the return value of the Connect () function - if(2!=argc) { - msg (); - return 0; - } - in //Scan Address Initialization -my_addr.sin_family =af_inet; toMY_ADDR.SIN_ADDR.S_ADDR =htonl (inaddr_any); +Inet_pton (Af_inet, argv[1], &my_addr.sin_addr); -Start =clock (); the * for(p = SP; p < EP; p++) $ {Panax NotoginsengMy_addr.sin_port =htons (p); -SOCKFD = socket (Af_inet,sock_stream,0); theRenum = Connect (SOCKFD, (structSOCKADDR *) &my_addr,sizeof(structsockaddr)); + if(Renum = =0) Aprintf"%d ports Open! \ n", p); the Close (SOCKFD); + } - $End =clock (); $Costtime = (float) (End-start)/10000; -printf"Scan Time:%f sec \ n", costtime); - the return(0); -}
Simple TCP full-connection scan under MACOSX