免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1151 | 回复: 1
打印 上一主题 下一主题

多线程版 查看局域网ip [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-07-19 22:54 |只看该作者 |倒序浏览
#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"
#include "pthread.h"
#include "semaphore.h" //posix


#include "MyQueue.h"


sem_t sem;
sem_t mutex;

MyQueue queue;

int start_ip = 1;
int end_ip = 254;
int time_connect = 10;
int port = 8888;
int nthreads = 255;

void* work(void* arg) {
        struct timeval timeout;
        fd_set wset;
        fd_set rset;
        char straddr[100];
        pthread_detach(pthread_self());
        printf("%d\tthread started...\n", pthread_self());
        for(;;) {
                sem_wait(&sem);
//              printf("%d\t work\n", pthread_self());
                int value;
                if(queue.dequeue(&value) == -1)continue;

                int sockfd = socket(AF_INET, SOCK_STREAM, 0);
                if(sockfd == -1)perror("socket()");

                sprintf(straddr, "192.168.1.%d\0", value);
//              fprintf(stderr, "scan %s\t", straddr);

                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)) = 100) {
                                        timeout.tv_sec  = 0;
                                        timeout.tv_usec = time_connect;
                                } else if(time_connect > 0) {
                                        timeout.tv_sec  = time_connect;
                                        timeout.tv_usec = 0;
                                }
                                FD_ZERO(&wset);
                                FD_SET(sockfd, &wset);
                                rset = wset;
                                int n = select(sockfd+1, &rset, &wset, 0, &timeout);
                                if(n == -1 && errno!= EINTR) {
                                        fprintf(stderr, "ERROR:\tselect() %s(%d)\n", strerror(errno), errno);
                                } else if(n > 0) {

                                        int optval;
                                        int optlen = 4;
                                        if(getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (void*)&optval, (socklen_t*)&optlen)  192.168.1.%d with connect timeout %d at port %d \n", nthreads, start_ip, end_ip, time_connect, port);
        printf("-----------------------------------------------------------------------------------------------------\n");

        for(int i=0; i

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/62281/showart_1086643.html

论坛徽章:
0
2 [报告]
发表于 2008-07-20 11:37 |只看该作者
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP