title: POSIX UDP socket not binding to correct IP
body: <p>I'm in the process of writing a project for college involving writing a chat client and server using POSIX sockets and C++.</p>
  
  <p>The clients are supposed to converse with each other using P2P, such as each client has his own open UDP socket through which he sends and recieves messages from/to other clients.</p>
  
  <p>My problem is 2-fold:</p>
  
  <ol>
  <li>My UDPSocket class constructor seems to be ignoring the port number completely, binding to port 65535 regardless of the parameter.</li>
  <li>The port is binding to IP 255.255.255.255 rather than my own IP (10.0.0.3), or at least that's what i get when I call getpeername.</li>
  </ol>
  
  <p>To the best of my knowledge passing INADDR_ANY should bind to my local address, and passing port number 0 should make the OS choose a free port, what am I doing wrong?</p>
  
  <p>This is the constructor of my UDPSocket class:</p>
  
  <pre><code>UDPSocket::UDPSocket(int port){
  socket_fd = socket (AF_INET, SOCK_DGRAM, 0);
  
  // clear the s_in struct
  bzero((char *) &amp;in, sizeof(in));  /* They say you must do this    */
  
  //sets the sin address
  in.sin_family = (short)AF_INET;
  in.sin_addr.s_addr = htonl(INADDR_ANY);    /* WILDCARD */
  in.sin_port = htons((u_short)port);
  
  fsize = sizeof(from);
  
  //bind the socket on the specified address
  if(bind(socket_fd, (struct sockaddr *)&amp;in, sizeof(in))&lt;0){
      perror ("Error naming channel");
  }
  }
  </code></pre>
  
  <p>This is the initialization:</p>
  
  <pre><code>m_Socket = new UDPSocket(0);
  </code></pre>
  
  <p>And this is the method I use to retrieve the binded address: (UDPSocket inherits Socket)</p>
  
  <pre><code>std::string Socket::GetSocketAddress()
  {
      struct sockaddr_in  addr;
      int len = sizeof(addr);
      getpeername(socket_fd, (struct sockaddr*)&amp;addr, (socklen_t*)&amp;len);
  
      char ipAddressBuffer[50];
      memset(ipAddressBuffer, 0, sizeof(ipAddressBuffer));
      sprintf(ipAddressBuffer, "%s:%d", inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
  
      return ipAddressBuffer;
  }
  </code></pre>
  
  <p>Any help would be greatly appreciated,
  Avi.</p>