Advertisement

Finding IP with WinSock

Started by May 14, 2000 12:12 AM
1 comment, last by Epoch 24 years, 7 months ago
I''m using WinSock for my communications in my chat program/game and I was wondering how do you find what your IP is with WinSock. I used the GetSockName function (using MFC but there''s an equivelant in non-MFC anyway.) but it only gives me a "0.0.0.0" message, and it only works when i actually manually put in the IP Address in the Connect function. ( i find out the IP address with the dos program ipconfig.exe that comes with windows) Any suggestions? Thanks
A post by Shadow in the multiplayer forum
quote:
TO Get your IP (or Ip''s in case you have more than one interface) do use this function;
[Each time you use it it gives the next ip, of the next interface]
firs to this,
char localHost[256];
gethostname(localHost,256);
then use it like
GetIpFrom(localhost);
and it returns a char* with the ip (then you can convert the ip to any other format you want)


char *GetIpFrom(char *hostname)
{
hostent *host;
struct in_addr local;
long hs;
static index = 0;


host = gethostbyname(hostname);

if(!host->h_addr_list[index]) return "None";
memcpy(&hs,host->h_addr_list[index],4);

local.S_un.S_addr = hs;

index++;

return inet_ntoa(local);
}



Hope that was what you were looking for...
Advertisement
Thanks SiCrane, I suppose I should have looked at the
multiplayer forum before posting the question.

This topic is closed to new replies.

Advertisement