summaryrefslogtreecommitdiff
path: root/components/net/lwip/apps/tcpecho.c
blob: 04ca9ebf3fb274f5e3df4ae681b4a36bf1657da3 (plain)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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. */
		err = netconn_accept(conn, &newconn);
		/* Process the new connection. */
		if(err == ERR_OK)
		{
			struct netbuf *buf;
			void *data;
			u16_t len;

			while(netconn_recv(newconn, &buf) == ERR_OK)
			{
				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