Ver Mensaje Individual
  #1 (permalink)  
Antiguo 04/02/2005, 02:35
ermanolillo
 
Fecha de Ingreso: febrero-2005
Mensajes: 3
Antigüedad: 19 años, 3 meses
Puntos: 0
Problema con la función gethostbyname

Hola:

tengo el siguiente trozo de código:

int Servidor_Crea_Conexion_Inet(int puerto)
{
int sock;
int res;
struct sockaddr_in si;
struct hostent *hp;
char maquina[20];

sock=-1;

sock=socket(AF_INET,SOCK_STREAM,0);

if (sock!= -1)
{
gethostname(maquina, sizeof (maquina));
hp=gethostbyname(maquina);
if (hp==NULL)
printf ("Error %d en funcion gethostbyname\n",errno);
else
{
memset(&si,0,sizeof(struct sockaddr_in));
si.sin_family=AF_INET;
si.sin_port=htons(puerto);
memmove(&si.sin_addr, hp->h_addr,hp->h_length);
res=bind(sock,(struct sockaddr *)&si, sizeof(si));
if (res!= -1)
{
res=listen(sock,5);
if (res==-1)
sock=-1;
}
else
sock=-1;
}
}
else
printf ("Error %d en la funcion socket\n",errno);

return sock;
}

Esta función me da siempre que la ejecuto un puntero a null en la función gethostbyname, y no me crea el socket de conexión.
¿Alguien puede ayudarme?.

Gracias.