| |||
| Cómo obtener mi dirección ip con c/c++? Quiero saber si alguien conoce alguna biblioteca, función ó método para obtener la dirección ip desde un programa en c o c++ en linux del equipo que lo ejecute. |
| ||||
| en la biblioteca <netdb.h> exite una funcion cuya declaracion es la siguiente.
Código:
aqui tienes un ejemplo de como usarla:struct hostent *gethostbyname(char *name);
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");
}
}
__________________ http://blog.tolaware.com.ar -> Blog de Java, Ruby y Linux |
| ||||
| de nada amigo
__________________ http://blog.tolaware.com.ar -> Blog de Java, Ruby y Linux |