http

  1. #include <stdlib.h> 
  2. #include <string.h> 
  3. #include <arpa/inet.h> 
  4. #include <sys/stat.h> 
  5. #include <sys/types.h> 
  6. #include <sys/socket.h> 
  7. #include <netdb.h> 
  8. #include <unistd.h> 
  9. #include <pthread.h>  
  10. #include<stdio.h>  
  11. #include<errno.h> 
  12.  
  13.  
  14.  
  15. #define BUFSIZE 1024 
  16. #define DestIp "10.1.10.253"    //"www.baidu.com" 
  17. #define DestPort 80 
  18. #define Req "GET /index.html HTTP/1.1\r\nHost: 211.94.144.100\r\nConnection: Close\r\n\r\n" 
  19. #define ReqLen sizeof(Req) 
  20.  
  21.  
  22.  
  23. unsigned long dns(const char* host_name) 
  24.     struct hostent* host = gethostbyname(host_name); 
  25.     struct in_addr addr; 
  26.     char ** pp; 
  27.  
  28.     pp = host->h_addr_list; 
  29.     if (*pp!=NULL) 
  30.     { 
  31.         addr.s_addr = *((unsigned int *)*pp); 
  32.         printf("address is %s\n",inet_ntoa(addr)); 
  33.         pp++; 
  34.  
  35.         return addr.s_addr; 
  36.     } 
  37.     return 0; 
  38. void thread_FUN() 
  39. while(1) 
  40. mainFUn(); 
  41. int mainFUn() 
  42.     ssize_t i; 
  43.     int nRequestLen; 
  44.  
  45.     char strResponse[BUFSIZE]={0}; 
  46.     char strRequest[BUFSIZE]={0}; 
  47.  
  48.  
  49.     int sockfd, numbytes; 
  50.     struct sockaddr_in dest_addr; /* connector's address information */ 
  51.  
  52.     dns(DestIp); 
  53.  
  54.     if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) 
  55.     { 
  56.         perror("socket"); 
  57.         exit(1); 
  58.     } 
  59.  
  60.     dest_addr.sin_family = AF_INET; /* host byte order */ 
  61.     dest_addr.sin_port = htons(DestPort); /* short, network byte order */ 
  62.     dest_addr.sin_addr.s_addr = inet_addr(DestIp); 
  63.  
  64.     /* Create and setup the connection */ 
  65.     if (connect(sockfd, (struct sockaddr *)&dest_addr,sizeof(struct sockaddr)) == -1) 
  66.     { 
  67.         perror("connect:s"); 
  68.         //exit(1); 
  69.     } 
  70.     /* Send the request */ 
  71.      
  72. strncpy(strRequest, Req,ReqLen); 
  73.     nRequestLen = ReqLen; 
  74.     if (write(sockfd,strRequest,nRequestLen) == -1) 
  75.     { 
  76.         perror("write"); 
  77.         exit(1); 
  78.     } 
  79.  
  80.     /* Read in the response  
  81.     while (1) 
  82.     { 
  83.         i = read(sockfd,strResponse,BUFSIZE-1); 
  84.         if (0 >= i) 
  85.         { 
  86.             break; 
  87.         } 
  88.         strResponse[i]='\0'; 
  89.   
  90.  
  91.     }*/ 
  92.  
  93.     /* Close the connection */ 
  94.     close(sockfd); 
  95. int main()  
  96.     {  
  97.      pthread_t ts[4024];  
  98.      int i; 
  99.     /*創建線程*/  
  100.   for(i=0;i<4024;i++) 
  101.      pthread_create(&(ts[i]),NULL,(void *)thread_FUN,NULL);  
  102.      /*等待線程退出*/  
  103.      pthread_join(ts[0],NULL);  
  104.      pthread_join(ts[1],NULL);  
  105.      return 0;  
  106.     }  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章