Browse Source

add upd and tcp echo server.

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@90 bbd45198-f89e-11dd-88c7-29a3b14d5316
bernard.xiong 15 years ago
parent
commit
2de41d04ad
2 changed files with 121 additions and 0 deletions
  1. 68 0
      net/apps/tcpecho.c
  2. 53 0
      net/apps/udpecho.c

+ 68 - 0
net/apps/tcpecho.c

@@ -0,0 +1,68 @@
+#include <lwip/api.h>
+
+#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 <finsh.h>
+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

+ 53 - 0
net/apps/udpecho.c

@@ -0,0 +1,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