Click to See Complete Forum and Search --> : Getting Server IP using WinSock


Risifo
June 11th, 2002, 04:40 PM
I just started trying to learn WinSock, heh, just dove right in after reading creating a non-blocking server from gamedev.net, and I use that to write a simply non-blocking server. You can connect to it, and if you press certain buttons it will react. But one thing I want to do, is to get the server IP so that I can set up admin access for me without entering a username and password(Which I also want to set up, but don't know how to output from client, and input strings). First I want to find server ip.

I tried
char *ServerIP = inet_ntoa(Server_Address.sin.addr);

but it outputs 0.0.0.0

Wierd... please help

Valen
June 13th, 2002, 10:22 AM
Try this function.

void GetNameAndIP(char *name, char* address)
{
IN_ADDR addr;
HOSTENT* phe;

if (gethostname(name, strlen(name)) == SOCKET_ERROR)
{
strcpy(name,"Unknown");
strcpy(address,"Unknown");
}

else
{
phe = gethostbyname(name);

if (phe == 0)
{
strcpy(address,"Unknown");
}

else
{
memcpy(&addr, phe->h_addr_list[0], sizeof(IN_ADDR));
strcpy(address,inet_ntoa(addr));
}
}
return;
}

NigelQ
June 13th, 2002, 10:44 AM
Or this one (http://www.codeguru.com/forum/showthread.php?s=&postid=537118#post537118)

Hope this helps,

- Nigel