linux编写一个简单的端口扫描程序,小弟我在linux下写了个简单的多线程端口扫描程序,运行时出现有关问题,请问一下(2)...

当前位置:我的异常网» Linux/Unix » 小弟我在linux下写了个简单的多线程端口扫描程序,

小弟我在linux下写了个简单的多线程端口扫描程序,运行时出现有关问题,请问一下(2)

www.myexceptions.net  网友分享于:2013-02-26  浏览:23次

usleep(1000);

printf( "\nThread   ending....\n ");

while(g_nThreadNum> 0)

{

usleep(1);

printf( "   =|=\t%d   threads   \r ",g_nThreadNum);

}

printf( "threadnum   =   %d ",   g_nThreadNum);

printf( "\nThread   ended!\n ");

}

int   main(int   argc,   char   **argv)

{

int   i,   j,   nStartPort,   nEndPort,nThread,   ret,   index;

int   nPort[1000];

pthread_t   tid;

/*   当前的线程数量   */

g_nThreadNum   =   0;

host   =   argv[1];

nThread   =   atoi(argv[3]);

sscanf(argv[2],   "%d-%d ",   &nStartPort,   &nEndPort);

printf( "nStartPort   =   %d\n ",   nStartPort);

printf( "nEndPort   =   %d\n ",   nEndPort);

/*   最大线程数   */

g_nMaxThread   =   (nThread   > =   1000)?   1000   :   nThread;

timeout.tv_usec   =   0;

if   (g_nMaxThread   >   500)

timeout.tv_sec   =   2;

else

timeout.tv_sec   =   1;

printf( "MaxThread   =   %d\n ",   g_nMaxThread);

printf( "ThreadNum   =   %d\n ",   g_nThreadNum);

index   =   0;

for   (i   =   nStartPort;   i   <=   nEndPort;   i++,   index++)

nPort[index]   =   i;

for   (j   =   0;   j   <   index;   j++)

{

g_nThreadNum++;

/*   若超过最大的线程数,程序休眠等待线程的释放   */

while   (g_nThreadNum   > =   g_nMaxThread)

{

usleep(10);

}

ret   =   pthread_create(&tid,   NULL,   (void   *)thread_scan_port,   &nPort[j]);

if   (0   !=   ret)

{

perror( "\npthread_create ");

exit(1);

}

}

//printf( "ThreadNum   =   %d\n ",   g_nThreadNum);

WaitThreadEnd();

return   0;

}

编译:   gcc   -lpthread   PortScanner.c   -o   PortScanner

运行:./PortScanner   192.168.2.1   1-1000   500

注:192.168.2.1   要扫描的地址   1-1000   端口范围   500   指定可以产生的最大线程数

当端口和线程比较大时运行就出问题了

------解决方案--------------------

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define MAX_PORT 4000

#define MAX_THREAD 100

/* basename(argv[0]). netBSD,linux and gnu libc all define it. */

extern char *__progname;

/* globals */

int port_num,thread_num=100;

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

int main(int argc,char **argv)

{

void *thread_main(void *);

pthread_t tid;

#ifndef HAVE__PROGNAME

__progname = argv[0];

#endif

printf( "----kf_701 scan tool--------- ");

文章评论

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值