title: C# udp client not receiving reply
body: <p>I have udp server written in C, it works as follow:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;pthread.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;sys/sendfile.h&gt;
  
  typedef struct sockaddr_in sockaddr_in;
  typedef struct sockaddr sockaddr;
  
  void* handle_client_thread(void* client_sock){
      sockaddr_in* client_addr = (sockaddr_in*)client_sock;
      sockaddr_in server_sock_info;
      int desc = socket(AF_INET,SOCK_DGRAM,0);
      bzero(&amp;server_sock_info, sizeof(sockaddr_in));
      server_sock_info.sin_addr.s_addr = INADDR_ANY;
      server_sock_info.sin_family = AF_INET;
      server_sock_info.sin_port = htons(0);
      bind(desc, (sockaddr*)&amp;server_sock_info, sizeof(sockaddr_in));
  
      const int buf_size = 1024;
      char buf[1024] = "msg2";
      sendto(desc,buf,strlen(buf),0, (sockaddr*)client_addr, sizeof(sockaddr_in));
  
      usleep(1000000);
      close(desc);
      free(client_sock);
  }
  
  
  int main(int argc, char** argv) {
      sockaddr_in server_sock_info;
      int desc = socket(AF_INET,SOCK_DGRAM,0);
      bzero(&amp;server_sock_info, sizeof(sockaddr_in));
      server_sock_info.sin_addr.s_addr = INADDR_ANY;
      server_sock_info.sin_family = AF_INET;
      server_sock_info.sin_port = htons(8080);
      bind(desc, (sockaddr*)&amp;server_sock_info, sizeof(sockaddr_in));
  
      const int buf_size = 256;
      char buf[256] = {0};
  
      while(1){
          sockaddr_in* th_data = (sockaddr_in*)malloc(sizeof(sockaddr_in));
          int sz = sizeof(sockaddr_in);
          memset(buf, 0 ,sizeof(buf));
          recvfrom(desc,buf,buf_size,0,(sockaddr*)th_data, &amp;sz);
  
          pthread_attr_t attr;
          pthread_t pubThread;
          pthread_attr_init(&amp;attr);
          pthread_attr_setdetachstate(&amp;attr,PTHREAD_CREATE_DETACHED);
          pthread_create (&amp;pubThread, &amp;attr, handle_client_thread, th_data);
      }
  
      close(desc);
      return EXIT_SUCCESS;
  }
  </code></pre>
  
  <p>I have tested this with client written in C and it is working fine.</p>
  
  <p>Now my C# client works like below:</p>
  
  <pre><code>private async void InitiateMainConnection()
  {
      await Task.Run(() =&gt;
      {
          try {
              string toSend = "ack";
              MainSocket.Send(toSend.ToBytes(),toSend.Length,MainRemoteEndPoint);
              //MainRemoteEndPoint = new IPEndPoint(IPAddress.Any, ((IPEndPoint)MainSocket.Client.LocalEndPoint).Port);
              while (true)
              {
                  byte[] buf = MainSocket.Receive(ref MainRemoteEndPoint);
                  Dispatcher.BeginInvoke(new Action(() =&gt;
                  {
                      this.searchKey.Text = buf.ToString(true);
                  }));
              }
  
          }
          catch(Exception e)
          {
              MessageBox.Show(e.Message);
          }
  
      });
  }
  
  private void connectMain_Click(object sender, RoutedEventArgs e)
  {
      MainSocket = new UdpClient();
  
      // Connect to the remote endpoint.
      try {
          MainRemoteEndPoint = new IPEndPoint(IPAddress.Parse(mainIP.Text), int.Parse(mainPort.Text));
          InitiateMainConnection();
  
          //var c = new UdpClient();
          //Thread.Sleep(5000);
          //c.Send("sadas".ToBytes(), 5, new IPEndPoint(IPAddress.Parse("127.0.0.1"), ((IPEndPoint)MainSocket.Client.LocalEndPoint).Port));
      }
      catch(Exception ex)
      {
          MessageBox.Show(ex.Message);
      }
  }
  </code></pre>
  
  <p>It is not able to receive messages sent from socket created in thread of C server, however if I send message from socket which actually received client data, it is receiving messages correctly.</p>
  
  <p>I have observed packets in Wireshark, packet is arriving in client machine but client code is not receiving it. How do I correct this?</p>