Ferramentas do usuário

Ferramentas do site


gethostbyname

Diferenças

Aqui você vê as diferenças entre duas revisões dessa página.


gethostbyname [2023/09/12 16:14] (atual) – criada - edição externa 127.0.0.1
Linha 1: Linha 1:
 +====== Função gethostbyname ======
 +
 +Sintaxe:
 +
 +''struct [[hostent]] * gethostbyname(const [[tipos_dados|char]] * name);''
 +
 +----
 +
 +
 +A função **gethostbyname** retorna a partir de um nome passado o endereço [[IP]] associado ao nome. A função realizar o papel de um [[DNS]]. Ela retorna um ponteiro para uma estrutura ou NULL em caso de erro.
 +
 +Veja o exemplo:
 +
 +<code c>
 +#include <stdio.h>
 +#include <stdlib.h>
 +#include <errno.h>
 +#include <netdb.h>
 +#include <sys/types.h>
 +#include <netinet/in.h>
 +#include <arpa/inet.h>
 +
 +int main(int argc, char *argv[])
 +{
 +   struct hostent *h;
 +
 +   if (argc != 2)
 +   {
 +      printf("Deve-se passar nome da maquina");
 +      exit(1);
 +   }
 +
 +   if ((h=gethostbyname(argv[1])) == NULL)
 +   {
 +      perror("gethostbyname:");
 +      exit(1);
 +   }
 +
 +   printf("Nome do Host: %s\n", h->h_name);
 +   printf("Endereco IP : %s\n", inet_ntoa(*((struct in_addr *)h->h_addr)));
 +
 +   return 0;
 +}
 +</code>
 +
 + --- //[[marcos@laureano.eti.br|Marcos Laureano]] 2008/04/25 06:36//