title: C++ socket option reuse port
body: <p>I have a problem with a c++ socket.<br>
  I'm using CAsyncSocket from MFC that i want to join a multicast group.<br>
  Also I need to have multiple listener on this group and here is where i get in trouble.<br>
  I found some examples on the web but it doesn't seems to work.<br>
  Here is my code:  </p>
  
  <pre><code>//create socket on port 17233   
  BOOL bRet = Create(17233,SOCK_DGRAM, FD_READ);
  
  //set reuse socket option  
  BOOL bMultipleApps = TRUE;
  bRet = SetSockOpt(SO_REUSEADDR, (void*)&amp;bMultipleApps, sizeof(BOOL), SOL_SOCKET);
  
  //join multicast group
  ip_mreq m_mrMReq;           // Contains IP and interface of the host group
  m_mrMReq.imr_multiaddr.s_addr = inet_addr((LPCSTR)"224.30.0.1");    /* group addr */ 
  m_mrMReq.imr_interface.s_addr = htons(INADDR_ANY);      /* use default */
  
  int uRes =setsockopt(m_hSocket, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char FAR *)&amp;m_mrMReq, sizeof(m_mrMReq));
  </code></pre>
  
  <p>There are no errors when i run this.<br>
  But when i try to run another instance of the app it fails to create a new socket on that port because the port is in use.</p>
  
  <p>I have done this in C# and it worked fine like this:</p>
  
  <pre><code>IPEndPoint ipep = new IPEndPoint(IPAddress.Any, port);
  s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
  s.Bind(ipep);
  IPAddress ip = IPAddress.Parse(mcastGroup);
  s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ip, IPAddress.Any));
  s.SetSocketOption(SocketOptionLevel.IP,SocketOptionName.MulticastTimeToLive, int.Parse("1"));
  </code></pre>
  
  <p>So if any body sees a problem with my code or have some tips i will gladly appreciated.  </p>
  
  <p><strong>EDIT 1:</strong><br>
  Is CAsyncSocket a TCP socket?</p>
  
  <p><strong>EDIT 2:</strong>
  After reading <a href="https://stackoverflow.com/questions/1694144/can-two-application-listen-to-the-same-port">Can two applications listen to the same port?</a><br>
  I think i made a confusion. I need a Multicast UDP port that can be access by multiple application using SO_REUSEADDR   </p>
  
  <p><strong>Edit for Clarification:</strong>  </p>
  
  <pre><code>BOOL bRet = Create(17233,SOCK_DGRAM, FD_READ)
  </code></pre>
  
  <p>Creates an UDP socket and bind's to to port 17223.<br>
  For <code>SetSockOpt(SO_REUSEADDR, (void*)&amp;bMultipleApps, sizeof(BOOL), SOL_SOCKET);</code> to work you need to set it before binding  as @Hasturkun said.<br>
  The final working code looks like this:  </p>
  
  <pre><code>    BOOL bRet = Socket(SOCK_DGRAM, FD_READ);
      if(bRet != TRUE)
      {
          UINT uErr = GetLastError();
          std::cout&lt;&lt;"Error:"&lt;&lt;uErr&lt;&lt;std::endl;
          return FALSE;
      }else{
          std::cout&lt;&lt;"Create sock: OK"&lt;&lt;std::endl;
      }
  
      //add reuse
      BOOL bMultipleApps = TRUE;      /* allow reuse of local port if needed */
      SetSockOpt(SO_REUSEADDR, (void*)&amp;bMultipleApps, sizeof(BOOL), SOL_SOCKET);
  
      //bind
      bRet = Bind(17233, NULL);
      if(bRet != TRUE)
      {
          UINT uErr = GetLastError();
          std::cout&lt;&lt;"Error(BIND):"&lt;&lt;uErr&lt;&lt;std::endl;
      }else{
          std::cout&lt;&lt;"BIND sock: OK"&lt;&lt;std::endl;
      }
  </code></pre>
  
  <p>Thanks,<br>
  Gabriel</p>
  
  <p>;</p>