Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>Q1:I've read from a couple of websites saying that setting "0" for the port tells the OS that you want the next available port, not actually 0. Is this true?</p> </blockquote> <p>Yes, but only after you have used <code>bind()</code> to attach the address to a real socket. At that point, use <code>getsockname()</code> to get the bound address from the socket; the port will be part of that.</p> <blockquote> <p>Q2:I've also read that gethostbyname(gethostname(...)) can give you the machine's ip, but Beej said that these are superseded by getaddrinfo(). So, am I supposed to use getaddrinfo? Or gethostbyname?</p> </blockquote> <p>Use <code>getaddrinfo()</code>; it does all that <code>gethostbyname()</code> did and more, and the interface sucks a lot less. (For example, it's typically thread-safe.)</p> <blockquote> <p>Q3:Is there anything else I'm doing wrong?</p> </blockquote> <p>There's no good defined concept of the server's IP address. Servers can have many due to things like multiple network cards (much more common for servers than desktop systems), and the one that the outside world knows it by might not be one of them (thanks to NAT firewalls). Occasionally, you can know exactly where the messages are coming from before the client connects — the most common option is to know that the client will be on <code>localhost</code> — which is part of the information you set during <code>bind()</code> but that's rare. You <em>can</em> find the address of the client once the connection has happened by using <code>getpeername()</code> but NAT might still make that functionally useless. Such addresses are typically set in the app's config file during deployment.</p> <p>If you only need the info for logging purposes, go right ahead. But beware of using it for anything else, as it doesn't actually tell you that much.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload