title: Implementing Multi-threading concurrent server by C++ (UDP case)
body: <p>I used "Create-new-thread method" to deal with new clients and met problem in UDP.
  First I used main thread to create one TCP thread to deal with new accept()
  and create one UDP thread to deal with new recvfrom(). (TCP cases are OK)
  Once the first recvfrom() callback, i try to pass the client to the new UDP thread and keep the current one to deal with next new UDP client
  It should be work by passing the address and new-created datagram socket to the new thread to continue do recvfrom(), right ?</p>
  
  <p>During experiment, I found that the OLD UDP thread keeps receiving datagram from the client while the new thread just doing while loop and recvfrom() turns to non-blocking manner and returns -1 as a result.</p>
  
  <p>Therefore what is the problem of my code....thanks~
  Do I understand something wrongly..?</p>
  
  <p>PassToUDPThread is the typeof struct i made for data passing to new thread:</p>
  
  <pre><code>typedef struct {
     sockaddr_in ReceiverSocket;
     SOCKET sendSocket;
     char* recvbuf;
     long packet_size;
  
  }PassToUDPThread;
  </code></pre>
  
  <p>Here is my code for the FIRST UDP thread function (dealing with first recvfrom() ONLY) :</p>
  
  <pre><code>sockaddr_in *ReceiverSocket = new sockaddr_in;
  
  ReceiverSocket-&gt;sin_family = AF_INET;
  if (strcmp(recv_hostname, "INADDR_ANY") != 0)
  inet_pton(AF_INET, ip_addr_char, &amp;ReceiverSocket-&gt;sin_addr.s_addr);
  else ReceiverSocket-&gt;sin_addr.s_addr = INADDR_ANY;
  ReceiverSocket-&gt;sin_port = htons((u_short)recv_port);
  
  //*** Create the socket
  SOCKET s;
  
  s = socket(AF_INET, SOCK_DGRAM, 0);
  printf("Receiver: Socket Created - UDP connection\n");
  bind(s, (struct sockaddr *)ReceiverSocket, sizeof(struct sockaddr_in));
  
  do
  {
     int fromlen = (int)sizeof(struct sockaddr_in);
     retVal = recvfrom(s, recvbuf, packet_size, 0, (struct sockaddr*)ReceiverSocket, &amp;fromlen);
     std::cout &lt;&lt; "UDP recv by MAIN "&lt;&lt;std::endl;
     if (retVal == SOCKET_ERROR) {
        std::cout &lt;&lt; "UDP Socket error : " + WSAGetLastError()&lt;&lt;std::endl;
        return 0;
     }
     else {
         SOCKET sendSocket = socket(AF_INET, SOCK_DGRAM, 0);
         PassToUDPThread _p;
         PassToUDPThread *p = &amp;_p;
         p-&gt;sendSocket = sendSocket;
         p-&gt;ReceiverSocket = *ReceiverSocket;
         p-&gt;recvbuf = recvbuf;
         p-&gt;packet_size = packet_size;
  
         thrd_t UDPthread;
         if (thrd_create(&amp;UDPthread, ThreadUDPReceiver, (void*)p) == thrd_success) {
             std::cout&lt;&lt;"success create new UDP thread"&lt;&lt;std::endl;
                              }
  
  
  
  
     }
     } while (retVal&gt;0);
       closesocket(s);
       UDPReceiver(arg);
       return 1;
  }
  </code></pre>
  
  <p>Here is the code for the function on NEW-CREATED UDP thread:</p>
  
  <pre><code> int ThreadUDPReceiver(void *arg) {
  
      int retVal;
      SOCKET s = ((PassToUDPThread*)arg)-&gt;sendSocket;
      char* recvbuf = ((PassToUDPThread*)arg)-&gt;recvbuf;
      long packet_size = ((PassToUDPThread*)arg)-&gt;packet_size;
      sockaddr_in *ReceiverSocket = new sockaddr_in;
      memcpy(ReceiverSocket, &amp;(((PassToUDPThread*)arg)-&gt;ReceiverSocket), sizeof sockaddr_in);
  
      bind(s, (struct sockaddr *)ReceiverSocket, sizeof(struct sockaddr_in));
  
      do {
         int fromlen = (int)sizeof(struct sockaddr_in);
         retVal = recvfrom(s, recvbuf, packet_size, 0, (struct sockaddr *)ReceiverSocket, &amp;fromlen);
         std::cout &lt;&lt; "UDP recv by thread "&lt;&lt; std::endl;
      } while (true);
  
      return 0;
  
      }
  </code></pre>