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

  免費注冊 查看新帖 |

Chinaunix

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

求教:從Web服務(wù)器獲取網(wǎng)頁程序出現(xiàn)的問題 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2011-08-12 11:34 |只看該作者 |倒序瀏覽
  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <netinet/in.h>
  4. #include <netdb.h>
  5. #include <stdio.h>
  6. #include <fcntl.h>
  7. #include <errno.h>
  8. #include <signal.h>

  9. void sig_int(int sig);

  10. int GetHttpHeader(char *buff, char *header);

  11. #define PRINTERROR(s)        \
  12. fprintf(stderr, "\nError at %s, errno = %d\n", s, errno)

  13. int main()
  14. {
  15.         int bytes_all = 0;
  16.         char *host_name = "www.baidu.com";
  17.         int nRet;
  18.         int sock_fd;
  19.         struct sockaddr_in server_addr;
  20.         struct hostent *host_entry;
  21.         char strBuffer[2048] = {0};
  22.         char strHeader[1024] = {0};

  23.         signal(SIGINT, sig_int);

  24.         sock_fd = socket(PF_INET, SOCK_STREAM, 0);
  25.         if(sock_fd == -1)
  26.         {
  27.                 PRINTERROR("socket()");
  28.                 return -1;
  29.         }

  30.         if((host_entry = gethostbyname(host_name)) == NULL)
  31.         {
  32.                 PRINTERROR("gethostbyname()");
  33.         }
  34.         server_addr.sin_port = htons(80);
  35.         server_addr.sin_family = PF_INET;
  36.         server_addr.sin_addr = (*(struct in_addr*)*(host_entry->h_addr_list));

  37.         nRet = connect(sock_fd, (struct sockaddr*)&server_addr, sizeof(struct sockaddr_in));
  38.         if(nRet == -1)
  39.         {
  40.                 PRINTERROR("connect()");
  41.                 close(sock_fd);
  42.                 return -1;
  43.         }

  44.         sprintf(strBuffer, "GET / HTTP/1.1\r\n");
  45.         strcat(strBuffer, "Accept */*\r\n");
  46.         strcat(strBuffer, "Connection: Keep-Alive\r\n");

  47.         nRet = send(sock_fd, strBuffer, strlen(strBuffer), 0);
  48.         if(nRet == -1)
  49.         {
  50.                 PRINTERROR("send()");
  51.                 close(sock_fd);
  52.                 return -1;
  53.         }

  54.         while(1)
  55.         {
  56.                 nRet = recv(sock_fd, strBuffer, sizeof(strBuffer), 0);
  57.                 if(nRet == -1)
  58.                 {
  59.                         PRINTERROR("recv()");
  60.                         break;
  61.                 }

  62.         bytes_all += nRet;
  63.        
  64.         if(0 == GetHttpHeader(strBuffer, strHeader))
  65.         {
  66.                 printf("%s", strHeader);
  67.         }

  68.         if(nRet == 0)
  69.         {
  70.                 fprintf(stderr, "\n %d bytes received.\n", bytes_all);
  71.                 break;
  72.         }

  73.         printf("%s", strBuffer);
  74.         }
  75.         close(sock_fd);

  76.         return 0;
  77. }

  78. void sig_int(int sig)
  79. {
  80.         printf("Ha ha, we get SIGINT!\n");
  81. }

  82. int GetHttpHeader(char *buff, char *header)
  83. {
  84.         char *p, *q;
  85.         int i = 0;

  86.         p = buff;
  87.         q = header;

  88.         if(NULL == p)
  89.                 return -1;
  90.         if(NULL == q)
  91.                 return -1;

  92.         while('\0' != (*p))
  93.         {
  94.                 q[i] = p[i];
  95.                 if((p[i] == 0x0d) && (p[i+1] == 0x0a) && (p[i+2] == 0x0d) && (p[i+3] == 0x0a))
  96.                 {
  97.                         q[i+1] = p[i+1];
  98.                         q[i+2] = p[i+2];
  99.                         q[i+3] = p[i+3];
  100.                         q[i+4] = 0;
  101.                         return 0;
  102.                 }
  103.                 i++;
  104.         }
  105.         return -1;
  106. }
復(fù)制代碼
發(fā)送完HTTP請求后,為什么接受時總是接受0字節(jié)?謝謝

論壇徽章:
0
2 [報告]
發(fā)表于 2011-08-12 13:52 |只看該作者
等著,幫你調(diào)試中。。。

論壇徽章:
0
3 [報告]
發(fā)表于 2011-08-12 14:24 |只看該作者
本帖最后由 jiayanfu 于 2011-08-15 08:37 編輯

指出3方面的原因
其一,在Linux中回車換行使用的符號是“\n”,而不是"\r\n"。
其二,在發(fā)送http請求的時候,應(yīng)當增加host字段。發(fā)送的內(nèi)容更改為
  1.        
  2.         sprintf(strBuffer, "GET / HTTP/1.0\r\n");
  3.         strcat(strBuffer, "Accept */*\n");
  4.         strcat(strBuffer,"Host: www.baidu.com\n");
  5.         strcat(strBuffer, "Connection: Keep-Alive\n");
  6.         strcat(strBuffer, "\n");
  7.        
復(fù)制代碼
其三,在處理recv的時候,應(yīng)該先判斷返回字節(jié)是否為0,然后再去http頭
這樣修改完了以后就可以測試通過了{:2_168:}

論壇徽章:
0
4 [報告]
發(fā)表于 2011-08-12 14:25 |只看該作者
還有,不知道你那能編譯過去不,我這兒還缺少兩個頭文件

  1. #include <unistd.h>
  2. #include <string.h>
復(fù)制代碼
這樣才ok

論壇徽章:
0
5 [報告]
發(fā)表于 2011-08-13 15:43 |只看該作者
回復(fù) 3# jiayanfu


    不對的,我這里還是接受0字節(jié)

論壇徽章:
0
6 [報告]
發(fā)表于 2011-08-13 17:52 |只看該作者
回復(fù) 5# jiakeyouwe


    我的代碼里那個\r\n應(yīng)該是\n!不知道你注意沒有

論壇徽章:
0
7 [報告]
發(fā)表于 2011-08-15 08:18 |只看該作者
HTTP/1.1 200 OK
Expires: -1
Cache-Control: no-store
Cache-Control: must-revalidate
Pragma: no-cache
Connection: close
Content-Type: text/html

<html><script language=javascript>function r(){setTimeout("g()", 2000);}function g(){window.location.href="http://10.10.171.171/webRoot/userAuth/blackUrl.html";}</script><body onLoad="r()"></body></html>HTTP/1.1 200 OK
Expires: -1
Cache-Control: no-store
Cache-Control: must-revalidate
Pragma: no-cache
Connection: close
Content-Type: text/html


354 bytes received.


我執(zhí)行出來的結(jié)果,我昨天在家里試了一下不行,但是在公司就沒有問題!我用瀏覽器通過wireshark看了一下,get命令發(fā)出去以后,服務(wù)器直接返回ok;但是在家里的時候就不行了,瀏覽器和服務(wù)器交互了好幾次,服務(wù)器才返回ok。。。。。而且服務(wù)器在返回ok。。。。。{:2_169:} 具體原因再分析。。。。

論壇徽章:
0
8 [報告]
發(fā)表于 2011-08-16 09:53 |只看該作者
回復(fù) 7# jiayanfu


    當然注意嘍,可是接受0字節(jié),我也是在家里試的

論壇徽章:
0
9 [報告]
發(fā)表于 2011-08-16 09:56 |只看該作者
回復(fù) 7# jiayanfu


    你用的wireshark是瀏覽器自帶的?什么瀏覽器,是linux發(fā)行版帶的嗎?wireshark沒有l(wèi)inux版本的。。。

論壇徽章:
0
10 [報告]
發(fā)表于 2011-08-16 10:00 |只看該作者
回復(fù) 9# jiakeyouwe


    我是在windows下用whireshark做的測試,編程環(huán)境是使用的Linux是虛擬機。但是網(wǎng)絡(luò)環(huán)境配置和windows下是一樣的。。不知道是不是很水。。。{:2_169:}
題外話。。Linux命令行模式下大家通常怎么瀏覽網(wǎng)頁。。。{:2_171:}
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(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