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--------- ");
文章評論
總結
以上是生活随笔為你收集整理的linux编写一个简单的端口扫描程序,小弟我在linux下写了个简单的多线程端口扫描程序,运行时出现有关问题,请问一下(2)...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux find 权限不够,超级用户
- 下一篇: 取什么网名好听143个