Linux下Socket写http协议下载文件代码以及说明

来源:互联网 发布:惠州网络推广方案 编辑:程序博客网 时间:2024/05/22 03:17


看代码:

          #include <string>   
  1. #include <iostream>   
  2. #include <stdio.h>   
  3. #include <winsock.h>   
  4. #include <conio.h>   
  5.   
  6.   
  7. using namespace std;   
  8.   
  9. #pragma comment("ws2_32.lib")   
  10.   
  11. string host2ip(const string& strdomain);   
  12. void save2file(char *buffer,const string& filename);   
  13. int getlength(const string& sheader);   
  14. bool getstatus(const string& sheader);   
  15. int getleftlength(const string& sheader);   
  16. void sethttphead(const string& file,const string& server);   
  17.   
  18.   
  19. int port = 80;   
  20. int error=0;   
  21. string serveraddr;   
  22. int i = 0;   
  23. const int BUFFER_SIZE = 1024;   
  24. char buffer[BUFFER_SIZE] = {0};   
  25. char    msg[BUFFER_SIZE];   
  26. FILE *f;   
  27. const string file = "/BT/200801/3/117906.torrent";   
  28. const string url = "www.supbt.com";   
  29. const string filename = "117906.torrent";   
  30.   
  31.   
  32. int main(void)   
  33. {   
  34.         int      r;   
  35.         WSADATA  wsa;   
  36.         SOCKET   sock;   
  37.         struct   sockaddr_in sin;   
  38.         f = fopen(filename.c_str(),"ab");   
  39.         sethttphead(file,url);   
  40.         if(WSAStartup(MAKEWORD(2,2),&wsa))   
  41.         {   
  42.                 printf("WSAStartup Error: %d/n",WSAGetLastError());   
  43.                 getche();   
  44.                 return -1;   
  45.         }   
  46.   
  47.         if((sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==INVALID_SOCKET)   
  48.         {   
  49.                 printf("Socket Error: %d/n",WSAGetLastError());   
  50.                 getche();   
  51.                 return -1;   
  52.         }   
  53.      
  54.         sin.sin_family=AF_INET;   
  55.         sin.sin_port=htons(port);   
  56.         serveraddr = host2ip(url);   
  57.   
  58.         cout << serveraddr << endl;   
  59.         sin.sin_addr.s_addr=inet_addr(serveraddr.c_str());   
  60.      
  61.   
  62.         if(connect(sock,(struct sockaddr *)&sin,sizeof(sin))==SOCKET_ERROR)   
  63.         {   
  64.                 printf("Connect Error: %d/n",WSAGetLastError());   
  65.                 printf("请检测网络连接!/n");   
  66.                 getche();   
  67.                 return -1;   
  68.         }   
  69.   
  70.      
  71.         if((r=send(sock,msg,sizeof(msg),0))==SOCKET_ERROR)   
  72.         {   
  73.                 printf("Send Error: %d/n",WSAGetLastError());   
  74.                 getche();   
  75.                 return -1;   
  76.         }   
  77.   
  78.         memset(buffer,0,BUFFER_SIZE);   
  79.         if((r=recv(sock,buffer,BUFFER_SIZE,0))==SOCKET_ERROR)   
  80.         {   
  81.                 printf("Recv Error: %d/n",WSAGetLastError());   
  82.                 closesocket(sock);   
  83.                 return -1;   
  84.         }   
  85.   
  86.         string header = string(buffer);   
  87.         int ilength = getlength(header);   
  88.         int leftlength = ilength;   
  89.         int icount;   
  90.         if(getstatus(header)){   
  91.                 //current is r   
  92.                 char *q = buffer;   
  93.                 char *p = strstr(buffer, "/r/n/r/n");   
  94.   
  95.                 p += 4;   
  96.                 //注意这里得 r - (p-q) 非常关键得问题   
  97.                 fwrite(p,r - (p - q),1,f);   
  98.                 leftlength -= r - (p - q);   
  99.                 int i=0;   
  100.                 icount = r - (p - q);   
  101.                 while(true)   
  102.                 {   
  103.                         memset(buffer, 0, BUFFER_SIZE);   
  104.                         r = recv(sock, buffer, BUFFER_SIZE, 0);   
  105.   
  106.                         if (r < 0) {   
  107.                                 closesocket(sock);   
  108.                                 return 2;   
  109.                         }   
  110.                         if (r == 0) {   
  111.                                 break;   
  112.                         }   
  113.                         int tmp;   
  114.                         tmp = leftlength;    //209  100 100 92   
  115.                         leftlength -= r;   
  116.                         if(leftlength > 0){   
  117.                                 fwrite(buffer,r,1,f);   
  118.                                 icount += r;   
  119.                         }   
  120.                         else if(leftlength == 0){   
  121.                                 //   
  122.                                 break;   
  123.                         }   
  124.                         else{   
  125.                                 icount += tmp;   
  126.                                 fwrite(buffer,tmp,1,f);   
  127.                                 break;   
  128.                         }   
  129.                         cout << i++ << " r is "<< r << endl;   
  130.   
  131.                 }   
  132.         }else{   
  133.                 cout << "http protocal error" << endl;   
  134.         }   
  135.         cout << icount << endl;   
  136.         fclose(f);   
  137.         closesocket(sock);   
  138.         WSACleanup();   
  139.         getche();   
  140.         return 0;   
  141. }   
  142. /*  
  143. 构造HTTP头  
  144. */  
  145. void sethttphead(const string& file,const string& server)   
  146. {   
  147.         strcat(msg,"GET ");   
  148.         strcat(msg,file.c_str());   
  149.   
  150.         strcat(msg," HTTP/1.1");   
  151.         strcat(msg,"/r/n");   
  152.   
  153.         strcat(msg,"Accept:*/*");   
  154.         strcat(msg,"/r/n");   
  155.   
  156.         //strcat(msg,"Accept-Language: zh-cn");   
  157.         //strcat(msg,"/r/n");   
  158.   
  159.         //strcat(msg,"Accept-Encoding: gzip, deflate");   
  160.         //strcat(msg,"/r/n");   
  161.   
  162.         strcat(msg,"User-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");   
  163.         strcat(msg,"/r/n");   
  164.   
  165.         strcat(msg,"Host:");   
  166.         strcat(msg,server.c_str());   
  167.         strcat(msg,"/r/n");   
  168.   
  169.         strcat(msg,"Connection: Keep-Alive");   
  170.         strcat(msg,"/r/n");   
  171.         strcat(msg,"/r/n");   
  172. }   
  173. //---------------------------------------------------------------------------   
  174.   
  175. /*   
  176. 将domain 转换为IP地址   
  177. */   
  178. string host2ip(const string& strdomain)   
  179. {   
  180.         SOCKADDR_IN   saddr;   
  181.         WORD   wVersionRequested;      
  182.         WSADATA   wsaData;      
  183.         hostent   *pHostIp;   
  184.         int   nErrorNum;      
  185.         wVersionRequested   =   MAKEWORD(2,   2);      
  186.         nErrorNum   =   WSAStartup(wVersionRequested,   &wsaData);   
  187.         if(nErrorNum   !=   0)   
  188.         {   
  189.                 return   "";   
  190.         }   
  191.         saddr.sin_family   =   AF_INET;   
  192.         pHostIp   =   gethostbyname(strdomain.c_str());   
  193.         if(pHostIp   ==   NULL)   
  194.         {   
  195.                 return   "0.0.0.0";   
  196.         }   
  197.         WSACleanup();   
  198.         return   (string)inet_ntoa(*(LPIN_ADDR)*(pHostIp->h_addr_list));   
  199. }   
  200. /*  
  201. 该方法为测试用法,并为应用到项目中  
  202. */  
  203. void save2file(char *buffer,const string& filename)   
  204. {   
  205.         FILE *f;   
  206.         f = fopen(filename.c_str(),"a+");   
  207.         fwrite(buffer,strlen(buffer),1,f);   
  208.         fclose(f);   
  209. }   
  210. /*  
  211. 该方法获取待下载文件大小  
  212. */  
  213. int getlength(const string& sheader){   
  214.         char * cont_len_pos;   
  215.         unsigned long cont_length;   
  216.         const char* header = sheader.c_str();   
  217.   
  218.         cont_len_pos =(char *)strstr(header,"Content-Length:");   
  219.         cont_len_pos = (char *)strchr(cont_len_pos,' ');   
  220.         cont_len_pos++;   
  221.         sscanf(cont_len_pos, "%ld", &cont_length);   
  222.   
  223.         return cont_length;   
  224. }   
  225. /*  
  226. 该方法判断是否Get成功,不成功就取消下载文件  
  227. */  
  228. bool getstatus(const string& sheader){   
  229.         string start = "HTTP/1.1 200 OK";   
  230.         if(sheader.find(start,0) != string::npos){   
  231.                 return true;   
  232.         }   
  233.         else{   
  234.                 return false;   
  235.         }   
  236. }   
  237. /*  
  238. 该方法并没有用到,开始考虑多了  
  239. */  
  240. int getleftlength(const string& sheader){   
  241.         int nreturnl = 0;   
  242.         int ilength = sheader.length();   
  243.         string _send = "/r/n/r/n";   
  244.         int istart = sheader.find_last_of(_send);   
  245.         if(istart != string::npos){   
  246.                 nreturnl = ilength - istart -_send.length();   
  247.         }   
  248.         return nreturnl;   
  249. }   
原创粉丝点击