Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Epoch

Finding IP with WinSock

This topic is 6921 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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...

Share this post


Link to post
Share on other sites
Thanks SiCrane, I suppose I should have looked at the
multiplayer forum before posting the question.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!