Forum > Networking and Web Programming
[SOLVED] gethostbyname(gethostname, host) returns random host.addr on MacOs
Key-Real:
I'm trying to get my local IP Address under MacOS.
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uses netdb;var host : THostEntry;begin gethostbyname(gethostname, host); writeln(HostAddrToStr(host.addr));end.
this writes a random ip address :(
what to do?
loaded:
The reason is written here.
https://wiki.freepascal.org/Netdb
If you want to know the LAN or WAN addresses of your computer. You need to use a different method.
I remember an example I wrote on the subject a long time ago. If I can find it I will send it.
Key-Real:
yes, cnetdb (I use it under linux) .... but it is not there for Mac
loaded:
--- Quote from: Key-Real on November 10, 2023, 01:44:43 pm ---yes, cnetdb (I use it under linux) .... but it is not there for Mac
--- End quote ---
Yes you are right. I was wrong here. ;D
In my solution it was valid for windows. In this case, I think the advice of someone experienced with MacOs will be useful.
Remy Lebeau:
--- Quote from: Key-Real on November 10, 2023, 12:30:26 pm ---I'm trying to get my local IP Address under MacOS.
...
this writes a random ip address :(
--- End quote ---
Well, for one thing, you are not checking the return value of gethostbyname() for failure before using the data. For another thing, this is not the best way to get local IPs anyway (even on Windows). Use getifaddrs() instead (or GetAdapterAddresses() on Windows).
Navigation
[0] Message Index
[#] Next page