Ver la Versión Completa : OPtener IP de la PC en C++
dmartinezn
24-08-2012, 15:02:28
Saludos
Quisiera saber de que forma podria optener el ip de la PC en la que estoy trabajando en C++ Builder.
Gerson12
24-08-2012, 17:57:24
en la biblioteca <netdb.h> exite una funcion cuya declaracion es la siguiente.
Código:
struct hostent *gethostbyname(char *name);
aqui tienes un ejemplo de como usarla:
Código:
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
struct sockaddr whereto;
struct hostent *hp;
struct sockaddr_in *to;
char *target;
char *hostname;
memset(&whereto, 0, sizeof(struct sockaddr));
to = (struct sockaddr_in *)&whereto;
to->sin_family = AF_INET;
to->sin_addr.s_addr = inet_addr(target);
if (to->sin_addr.s_addr != -1)
hostname = target;
else
{
hp = gethostbyname(target);
if (!hp)
printf("host desconocido%s\n", target);
else
{
to->sin_family = hp->h_addrtype;
memcpy(&(to->sin_addr.s_addr), hp->h_addr, hp->h_length);
hostname = hp->h_name;
printf("gethostbyname ejecutado correctamente\n");
}
}
esto no lo hize yo pero lo busque en san google amigo ^^ espero que te valga si necesitas la fuente donde la saque avisame ya que tengo entendi que no se dejan enlaces o algo asi
dmartinezn
24-08-2012, 18:39:45
Gracias por contestar
Ayer estuve probando ese ejemplo pero me da problemas en:
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
[C++ Error] Unit1.cpp(8): E2209 Unable to open include file 'sys/socket.h'
[C++ Error] Unit1.cpp(9): E2209 Unable to open include file 'netdb.h'
[C++ Error] Unit1.cpp(25): E2450 Undefined structure 'sockaddr'
[C++ Error] Unit1.cpp(25): E2449 Size of 'whereto' is unknown or zero
[C++ Error] Unit1.cpp(25): E2450 Undefined structure 'sockaddr'
[C++ Error] Unit1.cpp(31): E2450 Undefined structure 'sockaddr'
escafandra
24-08-2012, 18:54:46
#include <winsock2.h>
String GetCurrentIP()
{
WSADATA Wsa;
char name[255];
AnsiString ip;
PHOSTENT hostinfo;
if(WSAStartup(MAKEWORD(2,2),&Wsa) == 0 ){
if(gethostname ( name, sizeof(name)) == 0){
if((hostinfo = gethostbyname(name)) != NULL){
ip = inet_ntoa (*(in_addr *)*hostinfo->h_addr_list);
}
}
WSACleanup( );
}
return ip;
}
Ejemplo de uso:
Label1->Caption = GetCurrentIP();
Saludos.
dmartinezn
24-08-2012, 21:15:12
Excelente!!, gracias!.
Una ultima cosa,seria posible detectar otro numero ip de otra tarjeta q de red que este puesta en la misma PC?
escafandra
24-08-2012, 22:00:08
//---------------------------------------------------------------------------
#include <windows.h>
#include <winsock2.h>
#include <Iphlpapi.h>
#include <stdio.h>
#include <conio.h>
#pragma comment(lib, "iphlpapi.lib")
#pragma hdrstop
//---------------------------------------------------------------------------
void GetIPAddress(void)
{
PIP_ADAPTER_INFO AI, pAI;
DWORD AILen;
GetAdaptersInfo(0, &AILen);
pAI = AI = (PIP_ADAPTER_INFO) new BYTE[AILen];
if(!GetAdaptersInfo(pAI, &AILen)){
do{
printf("%s:\n", pAI->Description, pAI->Address[0]);
printf("\tMAC: \t\t%.2X-%.2X-%.2X-%.2X-%.2X-%.2X\n", pAI->Address[0], pAI->Address[1], pAI->Address[2], pAI->Address[3], pAI->Address[4], pAI->Address[5]);
printf("\tIP Address: \t%s\n\n", pAI->IpAddressList.IpAddress.String);
pAI = pAI->Next;
} while(pAI);
}
delete [] AI;
getch();
}
int main()
{
GetIPAddress();
return 0;
}
Lista todas los adaptadores de red (físicos o virtuales) dando su MAC y su IP
Un detalle, para importar la API GetAdaptersInfo de la librería iphlpapi.dll en Builder 5 o lo haces dinámicamente o de forma estática como yo.
iphlpapi.lib la creas con la utilidad IMPLIB que viene con Builder.
Saludos.
vBulletin v3.6.8, Derechos ©2000-2024, Jelsoft Enterprises Ltd.