|
作者: quest [quest] 版主 | 登录 |
[CODE] /*该程序用到了pthread库,编译时记得加上,不然无法通过. *还有很多东西,本来想做的,没时间了...:((其实是懒) */ #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdarg.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #include <pthread.h> //多线程库 int port; int sockfd, net, j = 0; struct sockaddr_in sa; struct hostent *host; char *hostname; pthread_mutex_t mutex; void scan (void *tID); int main (int argc, char *argv[]) { int iCount, i; int iResult; char c; pthread_t tScan[100]; if (argc < 2) { printf ("Usage:%s hostname\n", argv[0]); exit (1); } strncpy ((char *) &sa, "", sizeof sa); sa.sin_family = AF_INET; if (isdigit (*argv[1])) { sa.sin_addr.s_addr = inet_addr (argv[1]); hostname = 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); } pthread_mutex_init (&mutex, NULL); iCount = atoi (argv[2]); printf ("%d\n", iCount); port = 0; for (i = 0; i < iCount; i++) { iResult = pthread_create (&tScan[i], NULL, (void *) &scan, (void *) i); } for (i = 0; i < iCount; i++) pthread_join(tScan[i], NULL); if (j == 0) printf ("You hava no port to open the System maybe safe!\n"); else printf ("There are %d port opened!\n", j); printf ("\r"); fflush (stdout); return (0); } int getport () { return (++port > 65535 ? -1 : port); } void scan (void *tID) { int iPort; int iTID; iTID = (int) tID; iPort = 0; while (iPort != -1) { pthread_mutex_lock (&mutex); iPort = getport (); pthread_mutex_unlock (&mutex); if (iPort == -1) return; sa.sin_port = htons (iPort); sockfd = socket (AF_INET, SOCK_STREAM, 0); if (sockfd < 0) { perror ("\nsocket"); } net = connect (sockfd, (struct sockaddr *) &sa, sizeof sa); if (net < 0) { } else { j++; printf ("thread %d get port %d\n", iTID, iPort); printf ("%s %d accepted.\n", hostname, iPort); } close (sockfd); } } [此贴被 TomyChen(quest) 在 07月16日08时12分 编辑过] |
地主 发表时间: 04/09 15:57 |
回复: group [group] 论坛用户 | 登录 |
看了一段代码,结果发现是被注释掉了的,我晕!~~ |
B1层 发表时间: 04/10 00:26 |
回复: quest [quest] 版主 | 登录 |
被注释的是原来没加多线程的时候用的,用了多线程就 注释掉咯 |
B2层 发表时间: 04/10 08:24 |
回复: TomyChen [quest] 版主 | 登录 |
提一下这个帖,虽然代码写得很差...:) |
B3层 发表时间: 07/16 08:40 |
回复: 286 [unique] 版主 | 登录 |
不是一般的差 |
B4层 发表时间: 07/16 10:35 |
回复: ricky [ricky] 版主 | 登录 |
呵呵,鼓励一下吗 |
B5层 发表时间: 07/17 21:34 |
回复: collimator [collimator] 论坛用户 | 登录 |
不太明白,能解释一下是什么意思吗??? |
B6层 发表时间: 07/20 08:48 |
|
20CN网络安全小组版权所有
Copyright © 2000-2010 20CN Security Group. All Rights Reserved.
论坛程序编写:NetDemon
粤ICP备05087286号