2017-03-06 39 views
-2

我建立我自己的功能。使用本網站提供的其他功能將收購本地IP地址一起Get the IP Address of local computer關於在C字符數組的終止字符/ C++

我使用的是16長度的字符數組,這是進程中的返回值,192.168.056.001Íýýýý,它應該像「192.168.056.001」和一個'\ 0',這裏一定是錯誤的,只是無法弄清楚。並且這導致我發送它時出現問題通過創建UDP數據包通過以太網。

char* YourApplication::GetMyIP() { 
    char* IPAdd = new char[16]; 
    if (YourApplication::GenerateIP(MyIP)) { 
     int b1 = MyIP.b1; 
     int hundreds,tens; 
     if (b1 < 100) { 
      IPAdd[0] = '0'; 
      if (b1 < 10) { 
       IPAdd[1] = '0'; 
       IPAdd[2] = b1 + 48; 
      } 
      else { 
       tens = b1/10; 
       IPAdd[1] = tens + 48; 
       IPAdd[2] = b1 % 10 + 48; 
      } 
     } 
     else { 
      hundreds = b1/100; 
      IPAdd[0] = hundreds+48; 
      tens = (b1 - 100*hundreds)/10; 
      IPAdd[1] = tens+48; 
      IPAdd[2] = (b1 - 100 * hundreds - 10 * tens) + 48; 
     } 
     IPAdd[3] = '.'; 
     int b2 = MyIP.b2; 
     if (b2 < 100) { 
      IPAdd[4] = '0'; 
      if (b2 < 10) { 
       IPAdd[5] = '0'; 
       IPAdd[6] = b2 + 48; 
      } 
      else { 
       tens = b2/10; 
       IPAdd[5] = tens + 48; 
       IPAdd[6] = b2 % 10 + 48; 
      } 
     } 
     else { 
      hundreds = b2/100; 
      IPAdd[4] = hundreds + 48; 
      tens = (b2 - 100 * hundreds)/10; 
      IPAdd[5] = tens + 48; 
      IPAdd[6] = (b2 - 100 * hundreds - 10 * tens) + 48; 
     } 
     IPAdd[7] = '.'; 
     int b3 = MyIP.b3; 
     if (b3 < 100) { 
      IPAdd[8] = '0'; 
      if (b3 < 10) { 
       IPAdd[9] = '0'; 
       IPAdd[10] = b3 + 48; 
      } 
      else { 
       tens = b3/10; 
       IPAdd[9] = tens + 48; 
       IPAdd[10] = b3 % 10 + 48; 
      } 
     } 
     else { 
      hundreds = b3/100; 
      IPAdd[8] = hundreds + 48; 
      tens = (b3 - 100 * hundreds)/10; 
      IPAdd[9] = tens + 48; 
      IPAdd[10] = (b3 - 100 * hundreds - 10 * tens) + 48; 
     } 
     IPAdd[11] = '.'; 
     int b4 = MyIP.b4; 
     if (b4 < 100) { 
      IPAdd[12] = '0'; 
      if (b4 < 10) { 
       IPAdd[13] = '0'; 
       IPAdd[14] = b4 + 48; 
      } 
      else { 
       tens = b4/10; 
       IPAdd[13] = tens + 48; 
       IPAdd[14] = b4 % 10 + 48; 
      } 
     } 
     else { 
      hundreds = b4/100; 
      IPAdd[12] = hundreds + 48; 
      tens = (b4 - 100 * hundreds)/10; 
      IPAdd[13] = tens + 48; 
      IPAdd[14] = (b4 - 100 * hundreds - 10 * tens) + 48; 
     } 
    } 
    return IPAdd; 
} 

並且在下面從該網站該函數:

bool YourApplication::GenerateIP(IPv4 & myIP) 
{ 
    char szBuffer[1024]; 

#ifdef WIN32 
    WSADATA wsaData; 
    WORD wVersionRequested = MAKEWORD(2, 0); 
    if (::WSAStartup(wVersionRequested, &wsaData) != 0) 
     return false; 
#endif 


    if (gethostname(szBuffer, sizeof(szBuffer)) == SOCKET_ERROR) 
    { 
#ifdef WIN32 
     WSACleanup(); 
#endif 
     return false; 
    } 

    struct hostent *host = gethostbyname(szBuffer); 
    if (host == NULL) 
    { 
#ifdef WIN32 
     WSACleanup(); 
#endif 
     return false; 
    } 

    //Obtain the computer's IP 
    myIP.b1 = ((struct in_addr *)(host->h_addr))->S_un.S_un_b.s_b1; 
    myIP.b2 = ((struct in_addr *)(host->h_addr))->S_un.S_un_b.s_b2; 
    myIP.b3 = ((struct in_addr *)(host->h_addr))->S_un.S_un_b.s_b3; 
    myIP.b4 = ((struct in_addr *)(host->h_addr))->S_un.S_un_b.s_b4; 

#ifdef WIN32 
    WSACleanup(); 
#endif 
    return true; 
} 
+3

C不是C++。使用調試器或提供[MCVE]。 –

+1

我認爲你有一個關鍵問題'char * IPAdd = new char [16];'應該是'char * IPAdd = new char [16 + 1];'來解釋''\ 0'' – Ingenioushax

+0

我只是想知道我爲每個元素單獨和手動分配,爲什麼會有像「Íýýýý」這樣的字符?由於NDA,我不能提供關於其他部分的細節,而且它也與此功能無關,對此感到抱歉。我只給數組15個元素,第16個留給\ 0。 –

回答

1

使用庫操縱字節串(如的std :: string)將自動地在陣列的末尾添加一個終止字符。但是,由於您自己設置了所有值,因此應該設置IPAddr [15] ='\ 0'。

+0

Thx,我會自己添加'\ 0'。由於我們在這裏使用的是基於代碼的引擎,所以我必須使用char類型的字符串來與其他部分保持一致,並且我還沒有嘗試過用字符串發送過一個包。 –