How can i find my LAN IP Address using std.socket?
mshah.475 at gmail.com
Wed Feb 22 05:38:05 UTC 2023
On Tuesday, 4 February 2014 at 22:31:53 UTC, Dicebot wrote:
> On Tuesday, 4 February 2014 at 20:19:14 UTC, TheFlyingFiddle
>> I'm setting up a simple local network enabling me to connect
>> phones to the computer through the local wi-fi. The simplest
>> way i could think of to make this work without relying on an
>> external server was to simply broadcast the ip and port to all
>> machines in the network.(Btw by server i mean my / my project
>> groups windows boxes).
>> So well the problem is that i need a way for the phones to
>> find running servers on the LAN.
> I think it is close to impossible to do in portable way. Most
> reliable approach is to get list of all configured network
> interfaces via posix functions (or via `system` call as least
> resort), filter out "lo" and broadcast message for every such
> interface. I think you can also filter only wireless interfaces
> that way relatively easily too.
Apologies that I am bumping a post that is 9 years old, but I
recently had to do this and thought this may help beginners. In a
way it's a hack as suggested from the second post, that you can
connect to a known ip address (e.g. google) from a socket and
then see the endpoints with the local and remote addresses.
// A bit of a hack, but we'll create a connection from
// our current ip.
// Use a well known port (i.e. google) to do this
auto r = getAddress("126.96.36.199",53); // NOTE: This is
// Create a socket
auto sockfd = new Socket(AddressFamily.INET,
// Connect to the google server
const char address = r.toAddrString().dup;
ushort port = to!ushort(r.toPortString());
// Obtain local sockets name and address
writeln("Our ip address : ",sockfd.localAddress);
writeln("the remote address: ",sockfd.remoteAddress);
// Close our socket
More information about the Digitalmars-d-learn