- 论坛徽章:
- 0
|
#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 |
|