[C++] IP Ausgeben

errox

Grünschnabel
Hallo,
ich wollte fragen, ob mir jemand mitteilt wie Ich meine IP Adresse auf einer win32 Konsolenanwendung ausgebe.

Danke für antworten

MfG errox
 
Danke für deine schnelle antwort. Leider, bringt sie mir nicht da die funktion ioctl für Linux ist.

Source:

Code:
#include "stdafx.h"
#include <iostream>
#include <winsock.h>
#include <windows.h>




int main(void)
{
	int sock_fd = socket( AF_INET , SOCK_DGRAM , 0 );
	if( sock_fd == -1 ) { printf("Error: creating socket.\n"); exit(1); }
	struct ifreq ifr;
	strcpy( ifr.ifr_name , interface );
	if( ioctl( sock_fd , SIOCGIFADDR , &ifr ) == -1 ) { printf("problems with ioctl.\n"); exit(1); }
	ip->saddr = inet_addr( inet_ntoa( ((struct sockaddr_in *) (&ifr.ifr_addr))->sin_addr ) );
	close( sock_fd );
}

Fehler:

Warning 1 warning C4244: 'initializing' : conversion from 'SOCKET' to 'int', possible loss of data c:\c++ u c#\c++\projekte\ip\ip\ip.cpp 14
Error 6 error C3861: 'ioctl': identifier not found c:\c++ u c#\c++\projekte\ip\ip\ip.cpp 18
Error 11 error C3861: 'close': identifier not found c:\c++ u c#\c++\projekte\ip\ip\ip.cpp 20
Error 3 error C2228: left of '.ifr_name' must have class/struct/union c:\c++ u c#\c++\projekte\ip\ip\ip.cpp 17
Error 9 error C2228: left of '.ifr_addr' must have class/struct/union c:\c++ u c#\c++\projekte\ip\ip\ip.cpp 19
Error 10 error C2227: left of '->sin_addr' must point to class/struct/union/generic type c:\c++ u c#\c++\projekte\ip\ip\ip.cpp 19
Error 8 error C2227: left of '->saddr' must point to class/struct/union/generic type c:\c++ u c#\c++\projekte\ip\ip\ip.cpp 19
Error 2 error C2079: 'ifr' uses undefined struct 'main::ifreq' c:\c++ u c#\c++\projekte\ip\ip\ip.cpp 16
Error 5 error C2065: 'SIOCGIFADDR' : undeclared identifier c:\c++ u c#\c++\projekte\ip\ip\ip.cpp 18
Error 7 error C2065: 'ip' : undeclared identifier c:\c++ u c#\c++\projekte\ip\ip\ip.cpp 19
Error 4 error C2065: 'interface' : undeclared identifier c:\c++ u c#\c++\projekte\ip\ip\ip.cpp 17

Meine IDE ist VS 2005

Gruß errox
 
Bei meiner suche bin ich auf das hier gestossen, ich habe es schon ausprobiert und nach anfänglichen Schwierigkeiten dann doch zum Laufen gebracht und gedacht ich Poste es mal.



Code:
#include "stdafx.h"
#include <conio.h>
#include <iostream.h>
#include <windows.h> 

#pragma comment(lib,"ws2_32.lib")  // für MSV C++  

int main(int argc, char* argv[])
{
	
	
	
	
	 char hostname[255]; 
	 char *szIPAddress; 
	 WORD wVer; 
	 WSADATA wData; 
	 PHOSTENT hostinfo; 
	 wVer = MAKEWORD( 2, 0 ); 

	 if ( WSAStartup( wVer, &wData ) == 0 ) 
	 { 
	  if( gethostname ( hostname, sizeof(hostname)) == 0) 
	  { 
	   if((hostinfo = gethostbyname(hostname)) != NULL) 
	   { 
		szIPAddress = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list); 
	   } 
	  } 
	 WSACleanup(); 
	 }  

	 cout<<szIPAddress<<endl; 
	 getch();
	 return 0;
}


hoffe das Hilft ^^

hier übrigens noch die Quelle dazu (will mich ja nicht mit fremden Federn schmücken^^)

hier^^

greetz sand13r
 
:(
Error 4 error C2065: 'PHOSTENT' : undeclared identifier c:\c++ u c#\c++\projekte\ip\ip\ip.cpp 17
Error 3 error C2065: 'wData' : undeclared identifier c:\c++ u c#\c++\projekte\ip\ip\ip.cpp 16
Error 1 error C2065: 'WSADATA' : undeclared identifier c:\c++ u c#\c++\projekte\ip\ip\ip.cpp 16
Error 5 error C2146: syntax error : missing ';' before identifier 'hostinfo' c:\c++ u c#\c++\projekte\ip\ip\ip.cpp 17
Error 2 error C2146: syntax error : missing ';' before identifier 'wData' c:\c++ u c#\c++\projekte\ip\ip\ip.cpp 16
Error 10 error C2227: left of '->h_addr_list' must point to class/struct/union/generic type c:\c++ u c#\c++\projekte\ip\ip\ip.cpp 26
Error 9 error C3861: 'gethostbyname': identifier not found c:\c++ u c#\c++\projekte\ip\ip\ip.cpp 24
Error 8 error C3861: 'gethostname': identifier not found c:\c++ u c#\c++\projekte\ip\ip\ip.cpp 22
Error 11 error C3861: 'inet_ntoa': identifier not found c:\c++ u c#\c++\projekte\ip\ip\ip.cpp 26
Error 12 error C3861: 'WSACleanup': identifier not found c:\c++ u c#\c++\projekte\ip\ip\ip.cpp 29
Error 7 error C3861: 'WSAStartup': identifier not found c:\c++ u c#\c++\projekte\ip\ip\ip.cpp 20

Das is doof. Ich will doch blos meine ip auf der console ausgeben ( NUR MEINE IP )
 
Zurück