UDP Socket

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

UDP Socket

1,716 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by mrabbasi61 on Tue Aug 12 05:46:31 MST 2014
hi,
I couldn't find a sample project for UDP socket send and recieve data,
I saw that some people also asked for this before,
any suggestions is appreciated.
Labels (1)
0 Kudos
Reply
4 Replies

1,603 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by mrabbasi61 on Wed Aug 20 04:24:42 MST 2014
I have this code:
struct ip_addr  serverIp, localIp;
udp_recv_fn udp_echo_recv;
IP4_ADDR(&serverIp,192,168,1,188);

u16_t port;
port = 3620;
struct udp_pcb *pcb;
pcb = udp_new();
    udp_bind(pcb, &lpc_netif.ip_addr, port);
    udp_recv(pcb, udp_echo_recv, NULL);
   
    struct pbuf *p;
    char msg[]="Hello Message from LPC4357 \r\n";

//Allocate packet buffer
p = pbuf_alloc(PBUF_TRANSPORT,sizeof(msg),PBUF_RAM);
memcpy (p->payload, msg, sizeof(msg));
udp_connect(pcb, &serverIp, port);
udp_sendto(pcb, p, &serverIp, port);
pbuf_free(p); //De-allocate packet buffer


and it is OK, it sends "Hello Message from LPC4357 \r\n" to the server.

but when put the following line at the end of this part, it doesn't work, seems to me that in this case the p is Null !!!

   while(1){};



0 Kudos
Reply

1,603 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by mrabbasi61 on Fri Aug 15 13:37:48 MST 2014
Thank you for your help.
seems to me that one of the following could be my problem, specially the second one!
but I couldn't find the exact reason yet.

from the page (http://www.lpcware.com/content/faq/lpcxpresso/debugging-hard-fault)
Possible reasons for a Hard Fault include:
[list=1]
  [*]Trying to read or write to an on-chip peripheral that is powered down or not being clocked
  [*]Stack corruption - for example, overwriting the stack with data
  [*]Calling a function pointer with an invalid address
[/list]
0 Kudos
Reply

1,603 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by TheFallGuy on Fri Aug 15 10:40:25 MST 2014
So, you'll need to debug it then:
http://www.lpcware.com/content/faq/lpcxpresso/debugging-hard-fault
0 Kudos
Reply

1,603 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by mrabbasi61 on Fri Aug 15 09:46:09 MST 2014
Thank you all for your kind help :D
finally I decided to follow one of examples that use udp, for example in LWIP_DHCP it uses udp in order to communicate with dhcp server.

I tried to make a UDP connection to a remote IP and Port,
I think that at first I need to create a UDP PCB,
so, in my code I have the following lines, almost in the same sequence as below:

1- struct udp_pcb *my_pcb;
2- struct pbuf *my_buffer;
3- my_pcb=udp_new();
4- udp_bind(my_pcb, IP_ADDR, PORT);
5- udp_connect(my_pcb, IP_ADDR, PORT);
6- udp_sendto(my_pcb, my_buffer, IP_ADDR, PORT);

when I want to debug the code,
as soon as the execution is reached to the line 3 from above, or 4 (when line 3 is commented) ,
I am always taken to the following place:


__attribute__ ((section(".after_vectors")))
void HardFault_Handler(void) {
    while (1) {
    }
}
0 Kudos
Reply