2017-03-17 45 views
1

我有一個接收模塊「表達」 POST請求的NodeJS服務器,我需要用C++來發送POST請求,我的客戶連接()發送()函數返回數字> 0,但我的服務器沒有收到任何東西,並且沒有錯誤。 這是我的服務器代碼和我的C++代碼,請幫幫我,謝謝大家(我的服務器運行良好,因爲當我用JS發送請求時,它可以接收請求很好。)C++ POST請求的NodeJS服務器發生故障(基於套接字)

服務器代碼:

app.post('/fps', function(req, res){ 
    var fpsFilename = "./native/Data/FpsFile.txt"; 
    console.log('execute the imagequality program'); 

    var exec = require('child_process').exec; 
    console.log('in fps post'); 
    exec('./native/fps ' + fpsFilename, function(err, data, stderr) { 
    if(data.length > 1) { 
     res.json({fps : data}); 
    } else { 
     console.log('fps file trans error'); 
    } 
    if(err) { 
     console.info('stderr from fps:'+stderr); 
    } 
    }); 
}); 

我的C++代碼:

#define IPSTR "127.0.0.1" 
#define PORT 4001 
#define BUFSIZE 4096 
int MyBasicServerConnector::TestConnect() 
{ 
     int sockfd,ret,i ,h; 
     struct sockaddr_in servaddr; 
     char str1[4096], str2[2048], buf[BUFSIZE], *str; 
     socklen_t len; 
     fd_set t_set1; 
     struct timeval tv; 
     int isConneted = 0; 

     if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) 
      { 

        printf("---------------------socket error!\n"); 
        exit(0); 
      } 

     bzero(&servaddr,sizeof(servaddr)); 
     servaddr.sin_family = AF_INET; 
     servaddr.sin_port = htons(PORT); 

     if (inet_pton(AF_INET, IPSTR, &servaddr.sin_addr) <= 0) 
      { 

       printf("---------------------inet_pton error!\n"); 
       exit(0); 

      } 

      if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0){  
        printf("---------------------connect error!\n"); 
        exit(0); 
      } 
      else 
      { 
      isConneted = 1; 
      std::cout <<"---------------------connect success"<<endl; 
      } 




       memset(str2, 0, 2048); 
       //strcat(str2, "name = x\n"); 
       //strcat(str2, "code = x"); 
       //str=(char *)malloc(128); 
       len = strlen(str2); 
       //sprintf(str, "%d", len); 

       memset(str1, 0, 4096); 
       strcat(str1, "POST /fps HTTP/1.1 \r\n"); 
       strcat(str1, "Host: 127.0.0.1:4001\r\n"); 
       strcat(str1, "Connection: keep-alive\r\n"); 
       strcat(str1, "Content-Length: 7\r\n"); 
       strcat(str1, "Accept: */*\r\n"); 
       strcat(str1, "Origin: http://127.0.0.1:4001\r\n"); 
       strcat(str1, "X-Requested-With: XMLHttpRequest\r\n"); 
       strcat(str1, "User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36\r\n"); 
       strcat(str1, "Content-Type: application/x-www-form-urlencoded; charset=UTF-8\r\n"); 
       strcat(str1, "Referer: http://127.0.0.1:4001/\r\n"); 
       strcat(str1, "Accept-Encoding: gzip, deflate, br\r\n"); 
       strcat(str1, "Accept-Language: en-US,en;q=0.8\r\n"); 


     if (isConneted > 0) 
     { 
      if(send(sockfd,str1 ,strlen(str1),MSG_NOSIGNAL)>0) 
      { 
       cout<<"ok\n"; 
      } 
      else 
      { 
       cout<<"xx\n"; 
      } 
     } 
     close (sockfd); 
     return 0; 
     } 
+0

任何人都可以幫助我解決這個問題..?這是非常緊急的,非常感謝! – LeoHirasawa

+0

是否比較了C++發送和使用wireshark發送的JS? – Jonas

回答

0

我建議你實際上通過閱讀standard研究如何HTTP的工作。您的帖子中有幾處錯誤。最明顯的是:

  • 應該在請求的第一行HTTP/1.1後沒有空間
  • 你聲明的7內容長度,但不發送任何主體數據
  • 沒有頭尾分隔符(空行\r\n