亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 2676 | 回復(fù): 8
打印 上一主題 下一主題

socket接收數(shù)據(jù)問題,大家?guī)蛶兔?/a> [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2006-12-26 16:59 |只看該作者 |倒序瀏覽
我正在寫一個分析網(wǎng)頁的程序,碰到點兒問題:

接收數(shù)據(jù):


  1. .....
  2. memset(buf,0,sizeof(buf));
  3. while( (n=read(sock_fd,buf,1024))>0 ){
  4.         printf("have read data [%d]\n\n", n);
  5.         anaylizeBaidu(buf);
  6.         memset(buf,0,sizeof(buf));
  7. }
  8. printf("\n\nok");
  9. ...
復(fù)制代碼


為什么數(shù)據(jù)已經(jīng)全都收完了,程序還沒有打印出"ok",但也沒有打印出"have read data..."。

本來想獲取頭部的content-length,根據(jù)這個判斷大小來結(jié)束,但是www.sina.com.cn等網(wǎng)站沒有返回content-length這一項,而www.baidu.com就有這一項,我改怎么辦?各位大哥大姐幫幫忙,小弟在此先謝過了

論壇徽章:
0
2 [報告]
發(fā)表于 2006-12-26 17:08 |只看該作者
等待在read地方了。

論壇徽章:
1
榮譽版主
日期:2011-11-23 16:44:17
3 [報告]
發(fā)表于 2006-12-26 17:21 |只看該作者
看精華貼里我的那篇關(guān)于CURL的文章.

論壇徽章:
0
4 [報告]
發(fā)表于 2006-12-26 17:35 |只看該作者
你根據(jù)什么判斷數(shù)據(jù)收完了?

論壇徽章:
0
5 [報告]
發(fā)表于 2006-12-26 18:25 |只看該作者
請看下我寫的一個簡單wget,或許有點幫助


  1. /*
  2.         Wget V. 0.7 Download File Form Url
  3.        
  4.         code by W.Z.T        <wzt@xsec.org>
  5.        
  6. */

  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include <sys/socket.h>
  11. #include <sys/types.h>
  12. #include <sys/wait.h>
  13. #include <netinet/in.h>
  14. #include <netdb.h>

  15. #define        MAXLINE         65535
  16. #define SENDDATA        1000

  17. #define PORT                80

  18. struct hostent *host;

  19. char *data_m="Accept: */*\r\n"
  20.              "Accept-Language: zh-cn\r\n"
  21.              "UA-CPU: x86\r\n"
  22.              "Accept-Encoding: gzip, deflate\r\n"
  23.              "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; TencentTraveler ; .NET CLR 1.1.4322)\r\n";
  24.             
  25.             
  26. char *data_e="Connection: Keep-Alive\r\n\r\n";

  27. typedef struct DWONLOAD_FILE{
  28.         char hosts[100];
  29.         char ports[200];
  30.         char file[200];
  31.         int  port;
  32. }DOWN;

  33. void usage(char *pro);
  34. void ver();
  35. DOWN abstract_host(char *src);
  36. void download_file(char *url_t,char *local_file);
  37. int connect_server(int port);

  38. void usage(char *pro)
  39. {
  40.         ver();
  41.         printf("usage : %s <remote file> <local file>\n",pro);
  42.         printf("exp: %s http://tthacker.sitesled.com/backdoor/bindtty.c bindtty.c\n",pro);
  43.         exit(0);
  44. }

  45. void ver()
  46. {
  47.         printf("\nWget Ver 0.6 by W.Z.T, wzt@xsec.org 2006/10/27\n\n");
  48. }

  49. int connect_server(int port)
  50. {
  51.         struct sockaddr_in remote;
  52.         int sock_id;
  53.        
  54.         if((sock_id=socket(AF_INET,SOCK_STREAM,0))<0){
  55.                 perror("sock_id");
  56.                 exit(0);
  57.         }
  58.        
  59.         remote.sin_family=AF_INET;
  60.         remote.sin_port=htons(port);
  61.         remote.sin_addr=*((struct in_addr *)host->h_addr);
  62.        
  63.         if(connect(sock_id,(struct sockaddr *)&remote,sizeof(struct sockaddr))<0){
  64.                 perror("connect");
  65.                 return -1;
  66.         }
  67.        
  68.         printf("\n[+] Connect :%s:%d OK!\r\n",inet_ntoa(remote.sin_addr),ntohs(remote.sin_port));
  69.        
  70.         return sock_id;
  71. }

  72. void download_file(char *url_t,char *local_file)
  73. {
  74.         DOWN file_get;
  75.         FILE *fp;
  76.         char download_url[SENDDATA];
  77.         char recv_data[MAXLINE];
  78.         int sock_fd;
  79.         long get_file_len=0;
  80.         int r_len;
  81.         int http_check;
  82.         char *real_data_pos;
  83.                
  84.         ver();
  85.         file_get=abstract_host(url_t);

  86.         printf("\n[+] Host :%s\n",file_get.hosts);
  87.         printf("[+] Port :%d\n",file_get.port);
  88.         printf("[+] File :%s\n",file_get.file);
  89.        
  90.        
  91.         if((fp=fopen(local_file,"w+"))==NULL){
  92.                 printf("\n[-] Create Local File %s Failed.\n",local_file);
  93.                 exit(1);
  94.         }
  95.        
  96.         if((host=gethostbyname(file_get.hosts))==NULL){
  97.                 herror("gethostbyname");
  98.                 exit(0);
  99.         }

  100. // 提交url
  101.         sprintf(download_url,"GET %s HTTP/1.1\r\n%sHost: %s\r\n%s",file_get.file,data_m,file_get.hosts,data_e);
  102.        
  103. //        printf("%s",download_url);
  104.        
  105.         sock_fd=connect_server(file_get.port);
  106.        
  107.         if(sock_fd==-1){
  108.                 printf("connect error.\n");
  109.                 exit(1);
  110.         }

  111. // 接受并處理第-批數(shù)據(jù)
  112.        
  113.         write(sock_fd,download_url,strlen(download_url));
  114.         r_len=read(sock_fd,recv_data,MAXLINE);
  115.        
  116. //        printf("%s",recv_data);
  117.         http_check=atoi(recv_data+9);
  118.        
  119. //        printf("%d\n",http_check);
  120.         if(http_check == 200 || http_check == 206){
  121.                 printf("\n[+] Download Begin ...\n");
  122.         }
  123.         else{
  124.                 printf("\n[-] Bad request.\n");
  125.                 exit(1);
  126.         }

  127. // 獲得文件大小
  128.        
  129.         get_file_len=atoi(strstr(recv_data,"Content-Length: ")+16);
  130.         printf("\n[+] File Size: %ld\n",get_file_len);

  131. // 尋找真正的文件開始位置
  132.        
  133.         printf("\n[+] Waiting ...\n");
  134.         real_data_pos=strstr(recv_data,"\r\n\r\n")+4;
  135.         fputs(real_data_pos,fp);

  136. // 循環(huán)讀取數(shù)據(jù)
  137.        
  138.         memset(recv_data,0,MAXLINE);
  139.         while((r_len=read(sock_fd,recv_data,MAXLINE))>0){       
  140.                 fputs(recv_data,fp);
  141. //                printf("%s",recv_data);
  142.                 memset(recv_data,0,MAXLINE);
  143.         }
  144.        
  145.         fclose(fp);
  146.         printf("\n[+] Download File OK!\n");       
  147.        
  148. }

  149. DOWN abstract_host(char src[])
  150. {
  151.         DOWN file_download;
  152.         char *http="http://";
  153.         int port;
  154.         int port_flag=0;
  155.         int i,j=0,k,l=0,h;

  156.         i=strlen(http);
  157.         for(;i<strlen(src);i++){
  158.                 if(src[i]==':'){
  159.                         port_flag=1;
  160.                         h=i-1;
  161.                         i++;
  162.                         for(;src[i]!='/';i++){        /* 提取ports */
  163.                                 file_download.ports[j++]=src[i];
  164.                         }
  165.                         file_download.ports[j]='\0';
  166.                 }
  167.                 if(src[i]=='/'){
  168.                         k=i-1;
  169.                         break;
  170.                 }
  171.         }
  172.        
  173. /////////////////////////////////////////////////////////////////////       

  174.         for(;i<strlen(src);i++){        /* 提取file */
  175.                 file_download.file[l++]=src[i];
  176.         }
  177.         file_download.file[l]='\0';

  178. ////////////////////////////////////////////////////////////////////

  179.         i=strlen(http);     /* 提取主機名 */
  180.         j=0;
  181.         k=((port_flag==1)?h:k);
  182.        
  183.         for(;i<=k;i++)
  184.                 file_download.hosts[j++]=src[i];
  185.         file_download.hosts[j]='\0';
  186.        
  187. ////////////////////////////////////////////////////////////////////

  188.         if(port_flag==0)   /* 提取port */
  189.                 file_download.port=PORT;       
  190.         else
  191.                 file_download.port=atoi(file_download.ports);
  192.        
  193. ////////////////////////////////////////////////////////////////////       
  194.        
  195.         return file_download;
  196. }

  197. int main(int argc,char **argv)
  198. {
  199.         if(argc<3){
  200.                 usage(argv[0]);
  201.         }
  202.        
  203.         download_file(argv[1],argv[2]);
  204.                
  205.         return 0;
  206. }
復(fù)制代碼

論壇徽章:
0
6 [報告]
發(fā)表于 2006-12-27 06:16 |只看該作者

論壇徽章:
0
7 [報告]
發(fā)表于 2006-12-27 10:05 |只看該作者

呵呵,HTTP協(xié)議問題

等待原因就是socket阻塞,等待讀取...
對于HTTP協(xié)議,你可以去看看RFC文檔,估計是做爬蟲吧
1>請求頭可以發(fā)送 connection:close,處理完畢http服務(wù)器會關(guān)閉TCP連接,也就是說你read到結(jié)束的時候會有0返回
2> 對于長連接:keep-alive,這樣的話如果沒有content-length 也會有chunk傳輸編碼,都能正確找到結(jié)尾
3> 如果是做爬蟲,抓取效率很大程度上取決于抓取策略...


原帖由 青野志狼 于 2006-12-26 16:59 發(fā)表
我正在寫一個分析網(wǎng)頁的程序,碰到點兒問題:

接收數(shù)據(jù):

[code]
.....
memset(buf,0,sizeof(buf));
while( (n=read(sock_fd,buf,1024))>0 ){
        printf("have read data [%d]\n\n", n);
        a ...

論壇徽章:
0
8 [報告]
發(fā)表于 2007-01-09 16:04 |只看該作者
多謝大家的回復(fù),做的確實是個蜘蛛。
To W.Z.T:
     看了你的代碼,在read時那個連接是自動關(guān)閉的。

To sevendays:
    根據(jù)chunk如何分析是否結(jié)束?

找了一篇相關(guān)的文章:
http://hack.bluebing.cn/Html/wgpd/wljs/1253920060918143521.html

論壇徽章:
0
9 [報告]
發(fā)表于 2007-01-09 18:13 |只看該作者
呵呵,去找RFC文檔啊
chunk編碼最后是0\r\n\r\n(或者0\n\n),具體看RFC吧,自己變看文檔多找?guī)讉網(wǎng)站測試一下就清楚了
或者下載個 httpwatch來看看也行

原帖由 青野志狼 于 2007-1-9 16:04 發(fā)表
多謝大家的回復(fù),做的確實是個蜘蛛。
To W.Z.T:
     看了你的代碼,在read時那個連接是自動關(guān)閉的。

To sevendays:
    根據(jù)chunk如何分析是否結(jié)束?

找了一篇相關(guān)的文章:
[url]http://hack.bluebing ...
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP