rt-thread/net/apps/udpecho.c

54 lines
1.1 KiB
C

#include <lwip/api.h>
#define UDP_ECHO_PORT 7
void udpecho_entry(void *parameter)
{
static struct netconn *conn;
static struct netbuf *buf;
static struct ip_addr *addr;
static unsigned short port;
conn = netconn_new(NETCONN_UDP);
netconn_bind(conn, NULL, 7);
while(1)
{
/* received data to buffer */
buf = netconn_recv(conn);
addr = netbuf_fromaddr(buf);
port = netbuf_fromport(buf);
/* send the data to buffer */
netconn_connect(conn, addr, port);
netconn_send(conn, buf);
/* release buffer */
netbuf_delete(buf);
}
}
#ifdef RT_USING_FINSH
#include <finsh.h>
static rt_thread_t echo_tid = RT_NULL;
void udpecho(rt_uint32_t startup)
{
if (startup && echo_tid == RT_NULL)
{
echo_tid = rt_thread_create("uecho",
udpecho_entry, RT_NULL,
512, 30, 5);
if (echo_tid != RT_NULL)
rt_thread_startup(echo_tid);
}
else
{
if (echo_tid != RT_NULL)
rt_thread_delete(echo_tid); /* delete thread */
echo_tid = RT_NULL;
}
}
FINSH_FUNCTION_EXPORT(udpecho, startup or stop UDP echo server);
#endif