I fight with problems using a UDP client which does not send packets on certain networks.
- 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. - My actions are:
I designed a base class (see code below) - 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. - 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
#define LL LL_ERROR /* LL_ERROR, LL_WARN, LL_INFO, LL_DEBUG */
UdpClient::~UdpClient() {
}
UdpClient::UdpClient(char* sTargetIp, u16 uTargetPort) :
m_pUdpConn(NULL),
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) {
connect();
}
// 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
break;
case MG_EV_ACCEPT: // New connection accepted. union socket_address *
LOG(LL, ("MG_EV_ACCEPT (ev=%i)", ev));
break;
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;
}
}
break;
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);
break;
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));
break;
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;
break;
case MG_EV_TIMER: // now >= conn->ev_timer_time. double *
LOG(LL, ("MG_EV_TIMER (ev=%i)", ev));
break;
default:
LOG(LL, ("default (ev=%i)", ev));
break;
}
//#]
}
//*********************************************************************
// End of File D:\PSSystemV2\src\UdpClient.cpp
//*********************************************************************