From 2de41d04ad5981b936af5340a79ef314ac5621dd Mon Sep 17 00:00:00 2001 From: "bernard.xiong" Date: Sun, 11 Oct 2009 08:37:58 +0000 Subject: [PATCH] add upd and tcp echo server. git-svn-id: https://rt-thread.googlecode.com/svn/trunk@90 bbd45198-f89e-11dd-88c7-29a3b14d5316 --- net/apps/tcpecho.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++ net/apps/udpecho.c | 53 ++++++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 net/apps/tcpecho.c create mode 100644 net/apps/udpecho.c diff --git a/net/apps/tcpecho.c b/net/apps/tcpecho.c new file mode 100644 index 0000000000..4c04bcc813 --- /dev/null +++ b/net/apps/tcpecho.c @@ -0,0 +1,68 @@ +#include + +#define TCP_ECHO_PORT 7 + +void tcpecho_entry(void *parameter) +{ + struct netconn *conn, *newconn; + err_t err; + + /* Create a new connection identifier. */ + conn = netconn_new(NETCONN_TCP); + + /* Bind connection to well known port number 7. */ + netconn_bind(conn, NULL, TCP_ECHO_PORT); + + /* Tell connection to go into listening mode. */ + netconn_listen(conn); + + while(1) + { + /* Grab new connection. */ + newconn = netconn_accept(conn); + /* Process the new connection. */ + if(newconn != NULL) + { + struct netbuf *buf; + void *data; + u16_t len; + + while((buf = netconn_recv(newconn)) != NULL) + { + do + { + netbuf_data(buf, &data, &len); + err = netconn_write(newconn, data, len, NETCONN_COPY); + if(err != ERR_OK){} + } + while(netbuf_next(buf) >= 0); + netbuf_delete(buf); + } + /* Close connection and discard connection identifier. */ + netconn_delete(newconn); + } + } +} + +#ifdef RT_USING_FINSH +#include +static rt_thread_t echo_tid = RT_NULL; +void tcpecho(rt_uint32_t startup) +{ + if (startup && echo_tid == RT_NULL) + { + echo_tid = rt_thread_create("echo", + tcpecho_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(tcpecho, startup or stop TCP echo server); +#endif diff --git a/net/apps/udpecho.c b/net/apps/udpecho.c new file mode 100644 index 0000000000..b3efb9cea0 --- /dev/null +++ b/net/apps/udpecho.c @@ -0,0 +1,53 @@ +#include + +#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 +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