UDP client does not send packets on certains networks


I fight with problems using a UDP client which does not send packets on certain networks.

  1. My goal is:
    I want to run a UDP client that handles connecting, sending and receiving of a UDP data packets to a UDP server in the same local area network. It keeps the connection to the same UDP server the whole lifecycle.
  2. My actions are:
    I designed a base class (see code below)
  3. The result I see is:
    In my home network it works fine. But in my office network, the packets are not sent or at least they do not arrive at my UDP server. When I do the same client task on my PC, it works fine on all networks. So I am sure that both my the networks work fine with UDP.
  4. My expectation & question is:
    Where could be the problem in my implementation and is there a complete example of a UDP client?

Here my client code:

//	File Path       : D:\PSSystemV2\src\UdpClient.cpp
//	Component       : PSSystemV2Component_Esp
//	Configuration   : PSSystemV2
//	Model Element   : UdpClient
//	Rhapsody        : 8.3 (IDF V0.51)

//## auto_generated
#include "UdpClient.h"
//## auto_generated
#include "mgos_config.h"
//## auto_generated
#include "mgos_ro_vars.h"
//## auto_generated
#include "mgos_http_server.h"
//## package PSSystem_V2_Pkg::HwAccess_Pkg

//## class UdpClient


UdpClient::~UdpClient() {

UdpClient::UdpClient(char* sTargetIp, u16 uTargetPort) : 
 m_uTargetPort(uTargetPort) {
    //#[ operation UdpClient(char*,u16)
    strncpy(m_sTargetIp, sTargetIp, MAX_IP_ADDR_STRING_LEN);
    m_bConnectionReady = false;

bool UdpClient::udpClientSendRequest(const void* pData, int iDataLen) {
    //#[ operation udpClientSendRequest(const void*,int)
    int cnt = -1; 
    if ((mgos_wifi_get_status() == MGOS_WIFI_IP_ACQUIRED) && (getIpAddress() != 0)) {
    	// Connect if not done already:	       
    	if (m_pUdpConn == NULL) {		
    	// Send in case of valid connection:
    	if ((m_pUdpConn != NULL) && (m_bConnectionReady)) {
    		cnt = mg_printf(m_pUdpConn, "%.*s", iDataLen, (char*)pData); 
    		LOG(LL, ("udpClientSendRequest: send, connected, cnt=%i", cnt));
    		return true;
    LOG(LL, ("udpClientSendRequest: send, unconnected, cnt=%i", cnt));
    return false;

void UdpClient::connect() {
    //#[ operation connect()
    char url[70];
    snprintf(url, 70, "udp://%s:%u", m_sTargetIp, m_uTargetPort);
    m_pUdpConn = mg_connect(mgos_get_mgr(), url, udpConn_cb, this);
	if (m_pUdpConn != NULL) {
		LOG(LL, ("connect to %s successfully", url));
	} else {
		LOG(LL, ("connect to %s unsuccessfully", url));

void UdpClient::udpConn_cb(struct mg_connection* pConn, int ev, void * ev_data, void * pThis) {
    //#[ operation udpConn_cb(struct mg_connection*,int,void *,void *)
    	switch (ev) {
        case MG_EV_POLL:    // Sent to each connection on each mg_mgr_poll() call 
        case MG_EV_ACCEPT:  // New connection accepted. union socket_address * 
    		LOG(LL, ("MG_EV_ACCEPT (ev=%i)", ev));
        case MG_EV_CONNECT: // connect() succeeded or failed. int *  
    			int status = *(int *)ev_data;
    			if (status == 0) {
    				// Success
    				LOG(LL, ("MG_EV_CONNECT (ev=%i): Success", ev)); 
    				((UdpClient*)pThis)->m_bConnectionReady = true;
    			} else  {
    				// Error
    				LOG(LL, ("MG_EV_CONNECT (ev=%i): No success", ev));
    				((UdpClient*)pThis)->m_bConnectionReady = false;
        case MG_EV_RECV:    // Data has been received. int *num_bytes 
    		LOG(LL, ("MG_EV_RECV (ev=%i, len=%i): '%.*s':%i", ev, *(int*)ev_data, pConn->recv_mbuf.len, pConn->recv_mbuf.buf, pConn->recv_mbuf.size));
            ((UdpClient*)pThis)->udpClientReceiveResponse(pConn->recv_mbuf.buf, pConn->recv_mbuf.len);
        case MG_EV_SEND:    // Data has been written to a socket. int *num_bytes 
    		LOG(LL, ("MG_EV_SEND (ev=%i, len=%i)", ev, *(int*)ev_data));
        case MG_EV_CLOSE:   // Connection is closed. NULL 
    		LOG(LL, ("MG_EV_CLOSE (ev=%i)", ev));
    		((UdpClient*)pThis)->m_bConnectionReady = false;
    		((UdpClient*)pThis)->m_pUdpConn = NULL;
        case MG_EV_TIMER:   // now >= conn->ev_timer_time. double * 
    		LOG(LL, ("MG_EV_TIMER (ev=%i)", ev));
    		LOG(LL, ("default (ev=%i)", ev));

//	End of File D:\PSSystemV2\src\UdpClient.cpp

UDP does not know about networks, IP does.
If something does not work in a specific environment, describe your complete scenario with IP addresses and masks, put a network sniffer and capture network traffic. Are you using broadcast ? (don’t think so). You might have wrong addressing/mask, a firewall near your server, a router not routing your subnet, …
Your code is too long for my lazyness, maybe someone else will follow it and comment.