Advertisement

Getting my IP address with my server code?

Started by May 17, 2003 06:24 PM
1 comment, last by Sir-Smoke 21 years, 8 months ago
I'd like to set it up so when my server program is executed, it prints the IP address that clients will need to connect. For my purposes, it will be more convenient to just have the server tell you the IP then to use external sources. I havent had much luck with the search feature, but how would I go about doing this? I do recall hearing that you cant find your global IP... which sounds strange to me. I tried this code:
    
hostent* localInfo = gethostbyname(NULL);
in_addr addr;
memcpy(&addr,localInfo->h_addr_list[0],sizeof(in_addr));
cout << inet_ntoa(addr) << endl;
    
but my program CRASHES on the cout part using the inet_ntoa(addr) function (when commented out, program doesnt crash). So I'm not sure what I'm doing wrong. Any help is greatly appreciated [edited by - Sir-Smoke on May 17, 2003 7:25:32 PM]
replace your memcpy with this

memcpy(&addr.sin_addr, localInfo->h_addr, localInfo->h_length);
Advertisement
inet_ntoa is probably failing and returning NULL which will cause a crash when you attempt to send the result to cout.

[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com

This topic is closed to new replies.

Advertisement