[ENet-discuss] How to get our local IP?

Ruud van Gaal ruud at racer.nl
Fri Sep 28 03:56:40 PDT 2012


Hi Martin,

A simple way might be:

  char name[MAXHOSTNAMELEN];
  struct sockaddr_in addr;

  gethostname(name,sizeof(name));

  struct hostent *hostEntry=0;
  struct in_addr ia;

  // From the Unix network FAQ (www.unixguide.net/network/socketfaq/)
  // First try it as aaa.bbb.ccc.ddd.
  ia.s_addr=inet_addr(name);
  if(ia.s_addr!=INADDR_NONE)
  {
    // Got it...
    addr.sin_addr.s_addr=ia.s_addr;
    return TRUE;
  }
  // Next, try just gethostbyname()
  hostEntry=gethostbyname(name);
  if(hostEntry)
  {
    addr.sin_addr.s_addr=*(int*)hostEntry->h_addr_list[0];
    return TRUE;
  }

  if(!hostEntry)
  {
    qnError("QNAddress:GetByName(%s) failed",name);
    return FALSE;
  }
  // Retrieve address (IPv4)
  addr.sin_addr.s_addr=*(int*)hostEntry->h_addr_list[0];

May require some tuning to your code context.

Cheers,
Ruud

On Wed, Sep 19, 2012 at 4:02 PM, Martin Zemblowski <
martin at bigheadgames.co.uk> wrote:

> Is there any simple way in enet to get local ip of pc running the code (ie
> 192.168.1.10) ?
> Regards
>
> ______________________________**_________________
> ENet-discuss mailing list
> ENet-discuss at cubik.org
> http://lists.cubik.org/**mailman/listinfo/enet-discuss<http://lists.cubik.org/mailman/listinfo/enet-discuss>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cubik.org/pipermail/enet-discuss/attachments/20120928/70f18d72/attachment.html>


More information about the ENet-discuss mailing list