title: LWIP - how to get UDP source port of received packet?
body: <p><em>SOLVED</em> <strong>how to identify <em>source port</em> of sender who sent the packet you received now:</strong></p>
  <p>Use example &quot;udp_server&quot;...</p>
  <p>...and anywhere after line: <code>int len = recvfrom(sock, rx_buffer, sizeof(rx_buffer) - 1, 0, (struct sockaddr *)&amp;source_addr, &amp;socklen); </code></p>
  <p>...add line <code>u16_t client_port = htons(((struct sockaddr_in *)&amp;source_addr)-&gt;sin_port);</code></p>
  <p>IP address of sender is in existing line <code>inet_ntoa_r(((struct sockaddr_in *)&amp;source_addr)-&gt;sin_addr, addr_str, sizeof(addr_str) - 1);</code></p>
  <p>So:</p>
  <blockquote>
  <p>char client_IP[16]; //address of client who send request</p>
  <p>u16_t client_port = 0; //port of client who send request</p>
  <p>ESP_LOGI(TAG,
  &quot;Received %d bytes from %s:%d&quot;, len, client_IP, client_port);</p>
  </blockquote>
  <p>will give you:</p>
  <blockquote>
  <p>UDP Rx: Received 28 bytes from 192.168.0.110:55124</p>
  </blockquote>
  <p>When UDP packet is received, now you know <em><strong>ip:port</strong></em> to reply to.</p>