qq_35056292
y4ung
2017-10-12 02:19

C语言多线程传参数时,线程里的参数值一直没变?

1
  • c
  • 线程
  • 参数
  • 多线程

我想用C实现一个多线程的端口扫描,对每个ip都创建100个线程;
在main里for循环中创建线程,打印出来的参数(arg->min_port,arg->max_port)是没问题的,
但是在线程函数里打印出传进去的参数时,min_port与max_port都是一样的,请问该怎么解决? 谢谢


#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <string.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <pthread.h>

#define THREAD_NUM 100
#define MAX_PORT 65535
#define SEG_LEN 649


struct port_seg
{
    char *dest;
    int min_port;
    int max_port;
};

typedef struct port_seg port_segment;

void * scan(void * arg)
{
    struct port_seg * parg;
    int min_port;
    int max_port;
    int sockfd;
    struct sockaddr_in server;
    int length;
    int ret;    
    struct servent* result; 

    parg = (struct port_seg *)arg;

    min_port = parg->min_port;
    max_port = parg->max_port;

    printf("ip:%s\n",parg->dest);
    printf("min_port:%d\n",min_port);   
    printf("max:%d\n",max_port);

    if (-1==(sockfd=socket(AF_INET,SOCK_STREAM,0)))
    {
        perror("generating socket error\n");
        exit(1);
    }   

    memset(&server,0,sizeof(struct sockaddr_in));
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = inet_addr(parg->dest);


    length = sizeof(struct sockaddr);   


    for(int i=min_port;i<=max_port;i++)
    {       
        server.sin_port = htons(i);
        if(0>(ret=connect(sockfd,(struct aockaddr*)&server,length)))
        {
            //perror("connect err\n");
            continue;
        }
        else
        {   
            if(NULL == (result = getservbyport(server.sin_port,"tcp")))
            {   
                continue;
            }
            else
            {   
                printf(">>> ip:%s, name:%s, s_port:%d\n", parg->dest, result->s_name,ntohs(result->s_port));
            }

        }

        close(sockfd);  
    }   

    close(sockfd);
    exit(0);
}


int main(int argc, char** argv)
{
    pthread_t * thread;
    thread = (pthread_t *)malloc(THREAD_NUM * sizeof(pthread_t));

    if(argc<1)
    {
        printf("./portscanner ip1, ip2, ……");
        exit(1);
    }

    for(int i=1; i<argc; i++)
    { // each group
        for(int j=0;j<THREAD_NUM;j++)
        { 
//          printf("j:%d  ",j);
            port_segment port;
            port.dest = argv[i];
            port.min_port = j*SEG_LEN+1;

            if(j == THREAD_NUM - 1)
            {
                port.max_port = MAX_PORT;
            }
            else
            {
                port.max_port = port.min_port + SEG_LEN -1;
            }

//          printf("min:%d\n",port.min_port);
//          printf("max:%d\n",port.max_port);                       

            if ( pthread_create(&thread[j], NULL, scan, (void *)&port) != 0 )
                    {
                        perror( "pthread_create failed\n" );
                        free(thread);
                        exit(1);
            }

        }
    }


    exit(0);
}


  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答