http://jjgirl.yeah.net (2001-04-19 13:30:17)
本站点代码本人都在Linux上作过调试,如果在其他Unix系统上有问题
请与本人联系
[email protected]
本人已加中文注释,如果仍然有问题有给我留言或来信。
/* -*-C-*- tcpprobe.c */
/* tcpprobe - report on which tcp ports accept connections */
/* IO ERROR,
[email protected], Sep 15, 1995 */
#include
#include
#include
#include
#include
#include
/*below seven lines added by jjgirl 4.26.2000 */
#include
#include
#include
#include
#include
#include
#include
/*jjgirl:以上为头文件,jjgirl加了几个头文件才编译通过! */
int main(int argc, char **argv)
{
//int probeport = 0;
struct hostent *host;
int err, i, net;
struct sockaddr_in sa;
if (argc != 2) {
printf(""Usage: %s hostname
"", argv[0]);
exit(1);
}
/*jjgirl:以上为检验参数! */
/*jjgirl:以下对1—1024端口开始扫描,你也可以把1024该为65536加大扫描范围。 */
for (i = 1; i < 1024; i++) {
strncpy((char *)&sa, """", sizeof sa);
sa.sin_family = AF_INET;
if (isdigit(*argv[1]))
sa.sin_addr.s_addr = inet_addr(argv[1]);
else if ((host = gethostbyname(argv[1])) != 0)
strncpy((char *)&sa.sin_addr, (char *)host->h_addr, sizeof sa.sin_addr);
else {
herror(argv[1]);
exit(2);
}
sa.sin_port = htons(i);
/*jjgirl:以上为填上sockaddr_in结构的内容,下面的connect要用它! */
/*下面开始socketle 了*/
net = socket(AF_INET, SOCK_STREAM, 0);
if (net < 0) {
perror(""
socket"");
exit(2);
}
/*jjgirl:开始connect! */
err = connect(net, (struct sockaddr *) &sa, sizeof sa);
if (err < 0) {
printf(""%s %-5d %s
"", argv[1], i, strerror(errno));
fflush(stdout);
} else {
printf(""%s %-5d accepted.
"", argv[1], i);
/*jjgirl:成功打印*/
if (shutdown(net, 2) < 0) { /*不允许接受和发送,但接口仍打开!*/
perror(""
shutdown"");
exit(2);
}
}
close(net);
}//jjgirl:循环结束!
printf(""
"");
fflush(stdout);
return (0);
}