udpecho.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #include <lwip/api.h>
  2. #define UDP_ECHO_PORT 7
  3. void udpecho_entry(void *parameter)
  4. {
  5. static struct netconn *conn;
  6. static struct netbuf *buf;
  7. static struct ip_addr *addr;
  8. static unsigned short port;
  9. conn = netconn_new(NETCONN_UDP);
  10. netconn_bind(conn, NULL, 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. netconn_send(conn, buf);
  20. /* release buffer */
  21. netbuf_delete(buf);
  22. }
  23. }
  24. #ifdef RT_USING_FINSH
  25. #include <finsh.h>
  26. static rt_thread_t echo_tid = RT_NULL;
  27. void udpecho(rt_uint32_t startup)
  28. {
  29. if (startup && echo_tid == RT_NULL)
  30. {
  31. echo_tid = rt_thread_create("uecho",
  32. udpecho_entry, RT_NULL,
  33. 512, 30, 5);
  34. if (echo_tid != RT_NULL)
  35. rt_thread_startup(echo_tid);
  36. }
  37. else
  38. {
  39. if (echo_tid != RT_NULL)
  40. rt_thread_delete(echo_tid); /* delete thread */
  41. echo_tid = RT_NULL;
  42. }
  43. }
  44. FINSH_FUNCTION_EXPORT(udpecho, startup or stop UDP echo server);
  45. #endif