udpecho.c 1.1 KB
Newer Older
B
bernard.xiong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#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