cancel
Showing results for 
Search instead for 
Did you mean: 

Problem With UDP Broadcast receive

GETANS
Associate II

I am using the STM32F107VCT6 controller. use STMcubeIDE.

I write the code of the UDP Server. but when I change the IP, subnet, and Gateway using TCP and reset the device. I do not get Broadcast info or the udp_recv function not working.LWIP.PNG

void udpServer_init(void)
{

 
// UDP Control Block structure
 
struct udp_pcb *upcb;
//   struct udp_pcb *upcb;
   err_t err;
 
   /* 1. Create a new UDP control block  */
   upcb = udp_new();
   if(upcb == NULL){
   HAL_UART_Transmit(&huart1, "fail\n", 5, 5);
   }
 
   /* 2. Bind the upcb to the local port */
 
 
   err = udp_bind(upcb, IP_ADDR_ANY , 1225);  // 7 is the server UDP port
 
 
   /* 3. Set a receive callback for the upcb */
   if(err == ERR_OK)
   {
   HAL_UART_Transmit(&huart1, "ENTER IN UDP bind\n", 20,20);
   udp_recv(upcb, udp_receive_callback, NULL);
   }
   else
   {
   HAL_UART_Transmit(&huart1, "ENTER IN UDP remo\n", 20,20);
   udp_remove(upcb);
   }
}
 
// udp_receive_callback will be called, when the client sends some data to the server
/* 4. Process the datagram packet and send a reply to client. */
 
void udp_receive_callback(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *addr, u16_t port)
{
HAL_UART_Transmit(&huart1, "ENTER IN UDP rec\n", 20,20);
struct pbuf *txBuf;
 
/* Get the IP of the Client */
char *remoteIP = ipaddr_ntoa(addr);
 
 
char buf[100];
memset (buf, '\0', 100);
char load[100];
memset (load, '\0', 100);
 
strcpy(load,p->payload);
uint8_t len = 0;
 
if(load[0]=='I'&& load[1]=='N'&&load[2]=='F'&& load[3]=='O')
{
HAL_UART_Transmit(&huart1, "ENTER IN UDP rec1\n", 20,20);
 
 
len = sprintf(buf,"IP = %u.%u.%u.%u \n SUBNET = %u.%u.%u.%u \n GETWAY = %u.%u.%u.%u \n",(char*)IP_ADDRESS[0],(char*)IP_ADDRESS[1],(char*)IP_ADDRESS[2],(char*)IP_ADDRESS[3]
,(char*)NETMASK_ADDRESS[0],(char*)NETMASK_ADDRESS[1],(char*)NETMASK_ADDRESS[2],(char*)NETMASK_ADDRESS[3]
,(char*)GATEWAY_ADDRESS[0],(char*)GATEWAY_ADDRESS[1],(char*)GATEWAY_ADDRESS[2],(char*)GATEWAY_ADDRESS[3]);
 
}
 
else
{
len = sprintf(buf,"Invalid Command");
}
 
/* allocate pbuf from RAM*/
txBuf = pbuf_alloc(PBUF_TRANSPORT,len, PBUF_RAM);
 
/* copy the data into the buffer  */
pbuf_take(txBuf, buf, len);
 
/* Connect to the remote client */
udp_connect(upcb, addr, port);
 
/* Send a Reply to the Client */
udp_send(upcb, txBuf);
// udp_sendto(upcb, txBuf, addr, port);
 
/* free the UDP connection, so we can accept new clients */
udp_disconnect(upcb);
 
/* Free the p_tx buffer */
pbuf_free(txBuf);
 
/* Free the p buffer */
pbuf_free(p);
 
}
7 REPLIES 7
Bob S
Principal

> err = udp_bind(upcb, IP_ADDR_ANY , 1225); // 7 is the server UDP port

Which is it - port 1225 or port 7?

What IP/mask/gateway values worked?  What are the IP address/mask/gateway settings of the device sending the UDP broadcast?

IP : 192.168.4.111

Subnetmask : 255.255.255.0

Gateway : 192.168.4.1

and port : 1225

Maybe I mis-understood your original post because you re-posted the same IP address as in your first post.  Did this code EVER work?  To me it sounded like it worked, and then you changed the IP address and it stopped working.

If it DID work and now does not - provide BOTH the original IP address info and new IP address info

What is the IP address of the device that is sending the UDP broadcasts?

What does your program print on the serial port?

Do you call MX_LwIP_Process() somewhere in your main loop?

 

First, I Set the Value of IP: 192.168.4.111, SUBNET: 255.255.255.0, GATEWAY: 192.168.4.1.

and Using TCP, I Change the IP:10.10.10.11, SUBNET: 255.0.0.0, GATEWAY: 10.10.10.1. I change the value in LWIP. C file (Call MX_LWIP_Init() Function). then I Call UDP function. First Bind the UDP, then wait for receive DATA. But not get any Receive data.

For Data Sending I Use Docklight Application and for UDP connect I set The Docklight IP (example UDP:255.255.255.255:1225).

OK - you answered ONE of my questions.

(1) So your code receives UDP broadcasts with IP = 192.168.4.111 and does not receive UDP broadcasts with IP = 10.10.10.11, is that correct?

(2) What is the IP address of the device SENDING the UDP packets (I presume this is you PC)?

(3) What network equipment is between the SENDER and your STM32 board?  An ethernet switch?  A router/firewall?  The 'company intranet"?

(4) What does your program output on its serial/console port?

1.So your code receives UDP broadcasts with IP = 192.168.4.111 and does not receive UDP broadcasts with IP = 10.10.10.11, is that correct?

Ans - Yes. Does not receive UDP Broadcast with 10.10.10.11.

2.  What is the IP address of the device SENDING the UDP packets (I presume this is your PC)?

Ans - Yes, it is my PC. and The IP address is range same as 192.168.4.121.

3. What network equipment is between the SENDER and your STM32 board?  An ethernet switch?  A router/firewall?  The 'company intranet"?

Ans - The Network equipment is between the Sender and STM32 Board is Ethernet Switch.

4. What does your program output on its serial/console port?

Ans - I just check on serial port receive data.

Can't explain it.  Grasping at straws - make sure the interface is up on the STM32 with 10.10.10.11 IP address.  Don't know why it wouldn't, but worth checking.  Run wireshark on your PC and make sure the packet actually gets sent.  Again, I don't know why it wouldn't, but worth checking.