title: Calling socket::connect, socket::bind, socket::listen without using getaddrinfo( ) function before it
body: <p>In all the example including <a href="http://www.beej.us/guide/bgnet/output/html/singlepage/bgnet.html#getaddrinfoman" rel="nofollow noreferrer">Beej's Guide</a>, the IP address is provided in dot notation and then it's fed to <code>::getaddrinfo()</code>. <a href="https://stackoverflow.com/questions/23572645/c-socket-programming-with-urlport-url-in-getaddrinfo">This post</a> doesn't answer my question.</p>
  
  <p>After which the <code>addrinfo</code> struct is used for socket related functions (e.g. <code>connect()</code>, <code>bind()</code>, <code>listen()</code>). For example:</p>
  
  <pre><code>struct addrinfo hints, *res;
  // ... create socket etc.
  connect(sockfd, res-&gt;ai_addr, res-&gt;ai_addrlen);
  </code></pre>
  
  <p><a href="http://www.beej.us/guide/bgnet/output/html/singlepage/bgnet.html#connect" rel="nofollow noreferrer">Example</a><br>
  The variable <code>ai_addr</code> is of type <code>sockaddr</code> which can be <strong>safely</strong> typecasted to <code>sockaddr_storage</code>, <code>sockaddr_in</code> and <code>sockaddr_in6</code>.</p>
  
  <p><strong>Question</strong>:  </p>
  
  <p>If I typecast <code>sockaddr</code> to <code>sockaddr_in</code> (or <code>sockaddr_in6</code>) </p>
  
  <pre><code>sockaddr_in&amp; ipv4 = (sockaddr_in&amp;)(sockaddr_variable);
  </code></pre>
  
  <p>and feed below info:</p>
  
  <ul>
  <li>ipv4.sin_family = AF_INET</li>
  <li>ipv4.sin_addr = [IP Address in net byte order]</li>
  <li>ipv4.sin_port = [Port number in net byte order]</li>
  </ul>
  
  <p><strong>Can I call the <code>connect()</code> method directly using above info?</strong></p>
  
  <pre><code>connect(sockfd, &amp;ipv4, sizeof(ipv4));
  </code></pre>
  
  <p>With my program it doesn't appear to work. Am I missing something, or is there a better way?</p>
  
  <p>The motivation behind is that, if we have the information of IPAddress, Port etc. in socket readable format then why to go through the cycle of <code>getaddrinfo()</code></p>