Folgenden C++ Code zum Auslesen der IP für Linux umschreiben



  • Hallo kann mir einer sagen wie das für Linux aussieht?

    #include <winsock2.h>
    #include <iostream>
    
    using namespace std;
    
    main(){
          WORD wVersionRequested;
          WSADATA wsaData;
          char name[255];
          string ip;
          PHOSTENT hostinfo;
          wVersionRequested = MAKEWORD( 2, 0 );
    
          if ( WSAStartup( wVersionRequested, &wsaData ) == 0 )
          {
    
                if( gethostname ( name, sizeof(name)) == 0)
                {
                      if((hostinfo = gethostbyname(name)) != NULL)
                      {
                            ip = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);
                      }
                }
    
                WSACleanup( );
          }
    
          cout << ip;
          Sleep(10000); 
    }
    

    Danke ^^



  • Schmeiss halt den ganzen Windows-only-Kram (WSAStartup, WSACleanup, MAKEWORD) raus, und das sollte so funktionieren. Achso, andere Header brauchst Du wohl noch. Aber dafür gibt's ja man-pages.



  • Danke schon mal aber ich habe von Windowsprogrammierung überhaupt keine Ahnung ich weiß nicht was ich rauswerfen soll ^^



  • Deshalb hab ichs ja aufgeführt.



  • Also so?

    #include <iostream>
    
    using namespace std;
    
    main(){
          WORD wVersionRequested;
          WSADATA wsaData;
          char name[255];
          string ip;
          PHOSTENT hostinfo;
          wVersionRequested = ( 2, 0 );
    
          if (wVersionRequested, &wsaData == 0 )
          {
    
                if( gethostname ( name, sizeof(name)) == 0)
                {
                      if((hostinfo = gethostbyname(name)) != NULL)
                      {
                            ip = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);
                      }
                }
          }
    
          cout << ip;
          Sleep(10000);
    }
    


  • Kann mir das denn einer umschreiben bitte?



  • Linuxpower1 schrieb:

    Kann mir das denn einer umschreiben bitte?

    bitte lesen:

    SG1 schrieb:

    Schmeiss halt den ganzen Windows-only-Kram (WSAStartup, WSACleanup, MAKEWORD) raus, und das sollte so funktionieren. Achso, andere Header brauchst Du wohl noch. Aber dafür gibt's ja man-pages.





  • So wenn ich anstatt WSADATA int nehme dann kompiliert er aber er gibt nichts. Welchen Datentyp kann ich denn hierfür verwenden?

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <netinet/in.h>
    #include <netdb.h>
    #include <sys/types.h>
    #include <arpa/inet.h>
    #include <sys/socket.h>
    #include <sys/un.h>
    #include <iostream>
    
    using namespace std;
    
    main(){
          unsigned short wVersionRequested;
          WSADATA wsaData;
          char name[255];
          string ip;
          hostent *hostinfo;
          wVersionRequested = ( 2, 0 );
    
          if ((wVersionRequested, &wsaData ) == 0 )
          {
    
                if( gethostname ( name, sizeof(name)) == 0)
                {
                      if((hostinfo = gethostbyname(name)) != NULL)
                      {
                            ip = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);
    			cout << ip;
                      }
                }
          }
    
    }
    


  • Hab jetzt soviel rausgeholt bis auf das aber. Jetzt gibt er mir 127.0.0.2 aus und nicht 192.168.2.100 die er im Netzwerk auch hat

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <netinet/in.h>
    #include <netdb.h>
    #include <sys/types.h>
    #include <arpa/inet.h>
    #include <sys/socket.h>
    #include <sys/un.h>
    #include <iostream>
    
    using namespace std;
    
    main(){
          char name[255];
          string ip;
          hostent *hostinfo;
    
          if( gethostname ( name, sizeof(name)) == 0)
          {
              if((hostinfo = gethostbyname(name)) != NULL)
              {
                   ip = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);
    	       cout << ip;
              }
          }   
    }
    


  • cout ist C++, nimm printf
    #include<iostream> ist auch C++
    using namespace std is auch C++
    main ist von typ int und muss 0 zurückgeben wenn alles geklappt hat
    den Datentyp string gibt es in C nicht
    Kompilier das ganze am besten mit nem reinen C-Compiler, damit nich C++-Konstrukte reinrutschen


  • Mod

    linux_c89 schrieb:

    cout ist C++, nimm printf
    #include<iostream> ist auch C++
    using namespace std is auch C++
    main ist von typ int und muss 0 zurückgeben wenn alles geklappt hat
    den Datentyp string gibt es in C nicht
    Kompilier das ganze am besten mit nem reinen C-Compiler, damit nich C++-Konstrukte reinrutschen

    Wie kommst du auf die Idee, dass er C macht? Steht doch sogar im Titel, dass das C++ sein soll.



  • Oh Entschuldigung ich hatte irgendwie was verwechselt
    Dachte er will den C++-Code zu C-Code umschreiben
    sry
    (Wer lesen kann ist halt klar im Vorteil)


Anmelden zum Antworten