On Tuesday, 4 February 2014 at 22:31:53 UTC, Dicebot wrote:
On Tuesday, 4 February 2014 at 20:19:14 UTC, TheFlyingFiddle wrote:
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.

```
import std.stdio;
import std.socket;

    void GetIP(){
// A bit of a hack, but we'll create a connection from google to
        // our current ip.
        // Use a well known port (i.e. google) to do this
auto r = getAddress("8.8.8.8",53); // NOTE: This is effetively getAddressInfo
        writeln(r);
        // Create a socket
auto sockfd = new Socket(AddressFamily.INET, SocketType.STREAM);
        // Connect to the google server
        import std.conv;
        const char[] address = r[0].toAddrString().dup;
        ushort port = to!ushort(r[0].toPortString());
        sockfd.connect(new InternetAddress(address,port));
        // Obtain local sockets name and address
        writeln(sockfd.hostName);
        writeln("Our ip address    : ",sockfd.localAddress);
        writeln("the remote address: ",sockfd.remoteAddress);

        // Close our socket
        sockfd.close();
    }
    ```

Reply via email to