title: How to find a socket's local port number? (Windows C++)
body: <p>I'm new to Windows networking, and I am trying to find out which PORT number my socket is bound to (C++, Windows 7, Visual Studio 2010 Professional). It is a UDP socket, and from what I understand, using the following initial setup should bind it to a random available port/address:</p>
  
  <pre><code>sockaddr_in local;
  local.sin_family = AF_INET;
  local.sin_addr.s_addr = INADDR_ANY;
  local.sin_port = 0; //randomly selected port
  int result = bind(clientSock, (sockaddr*)&amp;local, sizeof(local));
  //result is always 0
  </code></pre>
  
  <p>As far as using this method, it works for sending data or binding it to a specific port (replacing the 0 with a desired port number). What I need is to bind it randomly, and then find out which port it was bound to afterwards. Is there any way I can do this? It seems that the "local" struct contains "0.0.0.0" as the IP address and "0" as the PORT number.</p>
  
  <p>Thanks for any and all help! I appreciate it.</p>