Re: How do I get my own IP address?
- Posted by jimcbrown (admin) Sep 21, 2009
- 2301 views
Untested concept code:
include std/pretty.e include std/net/dns.e constant COMPUTERNAME = getenv("COMPUTERNAME") sequence ips = host_by_name(COMPUTERNAME) for i = 1 to length(ips[3]) do if match("192.", ips[3][i]) = 1 then -- filter elsif match("10.", ips[3][i]) = 1 then -- filter -- and so on end if end for
Again, how does printing this out, or not print it out, tell winxp which nic to talk to the outside world with?
useless
I did not print it out. Once you have filtered the ips, you can pick what is left and then tell linux which ip to use to connect from in the connect(2) call via the struct sockaddr.
On xp the function and structs are different, but winsock has the same feature. So once you've filtered out the local ips, you will have the one external ip that you will pass to the OS to connect from or bind to.
However, if the computer has multiple external ips, it gets tricky. You either have to pick one at random, or pick the first in the list, or pick last in the list. If using bind(2), it may be possible to bind using all ips addresses by looping through each one and bind()ing separately. Then you will have to listen on multiple sockets for a client connection.