亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

查看局域網(wǎng)內(nèi)所有在線用戶 linux c

系統(tǒng) 1841 0
      
        /*
        
程序可以根據(jù)需要做適當(dāng)?shù)男薷? 使用-t 最好設(shè)置成5秒到10秒左右。否則將可能檢測(cè)不到下面的第一種情況。 1.如果局域網(wǎng)內(nèi)一臺(tái)電腦使用了應(yīng)用層防火墻比如天網(wǎng)之類,會(huì)connect超時(shí)。對(duì)方在線 2.connect success 連接成功,對(duì)方在線 3.connect refused 拒絕連接,對(duì)方在線 4.no route 連續(xù)發(fā)送5個(gè)arp請(qǐng)求(沒有結(jié)果)和一個(gè)dns查詢后(返回nxdomain),內(nèi)核產(chǎn)生no route的錯(cuò)誤。對(duì)方不在線 */ #include " stdio.h " #include " stdlib.h " #include " string.h " #include " unistd.h " #include " sys/types.h " #include " sys/time.h " #include " sys/socket.h " #include " arpa/inet.h " #include " netinet/in.h " #include " netdb.h " #include " fcntl.h " #include " errno.h " void usage() { printf( " use format:\n\t-s startip\n\t-e endip\n\t-t connect_timeout\n\t-p port\n " ); exit( - 1 ); }
int main( int argc, char ** argv)
{
int start_ip = 1 ; int end_ip = 254 ;

     //設(shè)置超時(shí)時(shí)限
int time_connect = 10 ;
     //設(shè)置端口號(hào)
int port = 8888 ; int opt; while ((opt = getopt(argc, argv, " s:e:t:p: " )) != - 1 ) { switch (opt) { case ' s ' :start_ip = atoi(optarg); break ; case ' e ' :end_ip = atoi(optarg); break ; case ' t ' :time_connect = atoi(optarg); break ; case ' p ' :port = atoi(optarg); break ; case ' ? ' :usage(); break ; } } printf( " -----------------------------------------------------------------------------------------------------\n " ); printf( " \t scan will use 192.168.1.%d -> 192.168.1.%d with connect timeout %d in port %d\n " , start_ip, end_ip, time_connect, port); printf( " -----------------------------------------------------------------------------------------------------\n " ); struct timeval timeout; fd_set wset; fd_set rset; char straddr[ 100 ];

     //按照地址1~254進(jìn)行掃描
for ( int i=start_ip; i<end_ip+ 1 ; i++ ) {          //套接字  int sockfd = socket(AF_INET, SOCK_STREAM, 0 ); if (sockfd == - 1 )perror( " socket() " );           sprintf(straddr, " 192.168.1.%d\0 " , i); fprintf(stderr, " scan %s\t " , straddr);
          //設(shè)置地址信息
struct sockaddr_in server_addr; server_addr.sin_family = AF_INET; if (inet_pton(AF_INET, straddr, &(server_addr.sin_addr)) != 1 )perror( " inet_pton() " ); server_addr.sin_port = htons(port); int flags; if ((flags = fcntl(sockfd, F_GETFL, 0 )) < 0 )perror( " fcntl() " ); if (fcntl(sockfd, F_SETFL, flags | O_NONBLOCK)< 0 )perror( " fcntl() " );

         //連接
int result = connect(sockfd, ( struct sockaddr*)(&server_addr), sizeof (server_addr)); if (result == - 1 ) { if (errno == EINPROGRESS) { timeout.tv_sec = 10 ; timeout.tv_usec = 0 ; if (time_connect >= 100 ) { timeout.tv_sec = 0 ; timeout.tv_usec = time_connect; } else if (time_connect > 0 ) { timeout.tv_sec = time_connect; timeout.tv_usec = 0 ; }
                   //select機(jī)制,初始化 FD_ZERO(
& wset);
                   //添加套接字 FD_SET(sockfd,
& wset);
rset
= wset; int n = select (sockfd+ 1 , &rset, &wset, 0 , & timeout); if (n == - 1 && errno!= EINTR) { perror( " select() error " ); } else if (n > 0 ) { int optval; int optlen = 4 ; if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, ( void *)&optval, (socklen_t*)&optlen) < 0 )perror( " getsockopt() " ); if (optval == 0 ) { fprintf(stderr, " success select\n " ); } else { fprintf(stderr, " failed select %d:%s\n " , optval, strerror(optval)); } } else if (n == 0 ) { fprintf(stderr, " connect timeout\n " ); } } else { close(sockfd); perror( " connect() " ); } } else { printf( " connect() success\n " ); close(sockfd); continue ; } close(sockfd); } return 0 ; }

已添加注釋~~~~~~~~~~~~~~~~~~

?轉(zhuǎn)自: http://blog.chinaunix.net/uid-9078996-id-2010266.html ?

查看局域網(wǎng)內(nèi)所有在線用戶 linux c


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 孕妇xxxxxx孕交xxx | 久久久无码精品亚洲日韩按摩 | 精品国产第一国产综合精品gif | 久久99亚洲综合精品首页 | 亚洲人人精品 | 97影院论理手机在线观看 | 亚洲综合在线视频 | 四虎影院永久免费观看 | 九九热精品在线视频 | 久久精品综合 | 色欧美亚洲 | 两性视频网站 | 九九视频网 | 亚洲国产大片 | 黄色毛片一级 | 五月激情婷婷网 | 成年美女| 久久亚洲精品专区蓝色区 | 亚洲日本va中文字幕区 | 麻豆精品一区二区三区免费 | 99热国产这里只有精品99 | 丝袜亚洲精品中文字幕一区 | 香蕉视频网站在线观看 | 视频一区久久 | 性欧美视频 | 久久精品99成人中文字幕880 | 久久精品国产视频在热 | 久久这里只有精品免费看青草 | 天天干夜夜谢 | 国产视频国产 | 欧美精品一区二区三区在线播放 | 黄页网址在线免费观看 | 久久国产精品99国产精 | 国产精品久久久久久搜索 | 四虎永久免费地ww4hu57 | 免费观看欧美一级毛片 | 欧美视频免费在线播放 | 91精品久久国产青草 | 国产一级淫片a视频免费观看 | 婷婷 综合| www.久草视频 |