1
0

udpecho.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #include <lwip/api.h>
  2. #define UDP_ECHO_PORT 7
  3. void udpecho_entry(void *parameter)
  4. {
  5. struct netconn *conn;
  6. struct netbuf *buf;
  7. struct ip_addr *addr;
  8. unsigned short port;
  9. conn = netconn_new(NETCONN_UDP);
  10. netconn_bind(conn, IP_ADDR_ANY, 7);
  11. while(1)
  12. {
  13. /* received data to buffer */
  14. buf = netconn_recv(conn);
  15. addr = netbuf_fromaddr(buf);
  16. port = netbuf_fromport(buf);
  17. /* send the data to buffer */
  18. netconn_connect(conn, addr, port);
  19. /* reset address, and send to client */
  20. buf->addr = RT_NULL;
  21. netconn_send(conn, buf);
  22. /* release buffer */
  23. netbuf_delete(buf);
  24. }
  25. }
  26. #ifdef RT_USING_FINSH
  27. #include <finsh.h>
  28. static rt_thread_t echo_tid = RT_NULL;
  29. void udpecho(rt_uint32_t startup)
  30. {
  31. if (startup && echo_tid == RT_NULL)
  32. {
  33. echo_tid = rt_thread_create("uecho",
  34. udpecho_entry, RT_NULL,
  35. 512, 30, 5);
  36. if (echo_tid != RT_NULL)
  37. rt_thread_startup(echo_tid);
  38. }
  39. else
  40. {
  41. if (echo_tid != RT_NULL)
  42. rt_thread_delete(echo_tid); /* delete thread */
  43. echo_tid = RT_NULL;
  44. }
  45. }
  46. FINSH_FUNCTION_EXPORT(udpecho, startup or stop UDP echo server);
  47. #endif