diff --git a/net/apps/tcpecho.c b/net/apps/tcpecho.c new file mode 100644 index 0000000000000000000000000000000000000000..4c04bcc81388b38556ea83278a1b2044fb600762 --- /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 0000000000000000000000000000000000000000..b3efb9cea04e9f8b245793931e850d794f11d1c5 --- /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