|
|
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <netdb.h>
- #include <pthread.h>
- #include "myproxy.h"
- #include "parse.h"
- void *check_proxy(void *p)
- {
- int sockfd;
- char *ip;
- char *port;
- struct sockaddr_in address;
- char request[256];
- char response[12];
- int ret;
-
- //get the required message in the struct
- ip = ((proxy *)p)->ip;
- port = ((proxy *)p)->port;
-
- /*create the socket*/
- sockfd = socket(AF_INET, SOCK_STREAM, 0);
- if(sockfd == -1)
- {
- fprintf(stderr, "Failed in creating socket!\n");
- }
- /*connect to the server by socket*/
- memset(&address, 0, sizeof(struct sockaddr_in));
- address.sin_family = AF_INET;
- address.sin_addr.s_addr = inet_addr(ip);
- address.sin_port = htons(atoi(port));
- ret = connect(sockfd, (struct sockaddr *)&address, sizeof(struct sockaddr));
- if(ret == -1)
- {
- fprintf(stderr, "Failed in connecting to server!\n");
- }
- /*send the http request*/
- sprintf(request,
- "%s", "GET / HTTP/1.0\nHost:www.google.com\n\n");
- ret = write(sockfd, request, strlen(request));
- if(ret == -1)
- {
- fprintf(stderr, "Failed in writing data to socket!\n");
- }
- /*read the response from server*/
- /*here i donnot check the integrity of the data*/
- ret = read(sockfd, response, sizeof(response));
- if(response[9] == '2' || response[9] == '3')
- {
- fprintf(stdout, "%s:%s OK!\n", ip, port);
- }
- else
- {
- /*debug to display*/
- /*fprintf(stderr, "Proxy Failed!\n");*/
- }
-
- /*close the socket*/
- close(sockfd);
-
- return NULL;
- }
- int
- main()
- {
- int ret;
- int i;
- pthread_t t_id[MAX_PROXY_NUM]; /*thread id*/
-
- ret = parse_proxy();
- if(ret != 0)
- {
- printf("Parsed proxy failed!\n");
- }
-
- /*create a thread to test a proxy*/
- i = 0;
- while(i != proxy_num)
- {
- pthread_create(&(t_id[i]), NULL, &check_proxy, &(parsed_proxy[i]));
- i++;
- }
-
- /*wait the son thread*/
- i = 0;
- //while(i != proxy_num)
- //{
- // pthread_join(t_id[i], (void **) &ret);
- // i++;
- //}
-
- while(1)
- {
- };
- }
复制代码
我用pthread_create创建了大约五十个子线程,然而即便我在MAIN函数里边加入了while(1)循环,每一个线程执行退出后整个程序就退出,而使用pthread_join问题同样。
那么我的问题是,主线程创建了第一个线程后,就做什么去了,还是已经退出了。
(单步执行时,程序的运行结果是正确的,我也检测出正确的代理。)
谢谢大家! |
|