附加到char *的std :: string

时间:2015-03-25 12:22:29

标签: c++ string

我在windows 8并使用cygwing++。我写了一个我的Connection类的成员函数,它维护一个连接并提供一些操作来处理它。其中一个在下面:

 const char* Connection::recieve_all_data()
 {
      using namespace utils;
      if(socket_ == INVALID_SOCKET)
      {
             throw std::invalid_argument("Socket was not initialized\n");
      }

      std::string result = std::string();
      char *buf = new char[BUFFER_SIZE];
      while(true)
      {
               int iResult = recv(socket_, buf, utils::BUFFER_SIZE, 0);
               if(iResult == BUFFER_SIZE)
               {
                       result.append(buf);
                       std::cout << buf << std::endl << std::endl; //1, LOOK AT THIS LINE
               }
               else if (iResult < BUFFER_SIZE)
               {
                       char *final_buffer = new char[iResult + 1];
                       strncpy(final_buffer, buf, iResult);
                       final_buffer[iResult] = '\0';
                       result.append(final_buffer);
                       break;
               }
               else if(iResult == WSAENOTCONN)
               {
                       throw std::runtime_error("Socket was not connected");
               }
      }
      return result.c_str();
}

我尝试测试该功能以从google.com接收一些数据。事情是,如果没有注释行//1,我会收到以下内容:

HTTP/1.0 400 Bad Request
Content-Type: text/html; charset=UTF-8
Content-Length: 1419
Date: Wed, 25 Mar 2015 12:12:51 GMT
Server: GFE/2.0

<!DOCTYPE html>
<html lang=en>
  <meta charset=utf-8>
  <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
  <title>Error 400 (Bad Request)!!1</title>
  <style>
    *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/errors/logo_sm_2.png) no-repeat}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/errors/logo_sm_2_hr.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/errors/logo_sm_2_hr.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/errors/logo_sm_2_hr.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:55px;width:150px}
  </style>
  <a href=//www.google.com/><span id=logo aria-label=Google></span></a>
  <p><b>400.</b> <ins>That’s an error.</ins>
  <p>Your client has issued a malformed or illegal request.  <ins>That’s all we know.</ins>

但如果我评论第//1行,则收到的回复不满:

HTTP/1.0 400 Bad Request
Content-Type: text/html; charset=UTF-8
Content-Length: 1419
Date: Wed, 25 Mar 2015 12:12:07 GMT
Server: GFE/2.0

<!DOCTYPE html>
<html lang=en>
  <meta charset=utf-8>
  <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
  <title>Error 400 (Bad Request)!!1</title>
  <style>
    *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/errors/logo_sm_2.png) no-repeat}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/errors/logo_sm_2_hr.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/errors/logo_sm_2_hr.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/errors/logo_sm_2_hr.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:55px;width:150px}
  </style>
  <a href=

有什么关系?我真的不知道这种行为。第//1行怎么会影响result?这只是一个输出。也许这是一个cygwin问题?

0 个答案:

没有答案