Merge pull request #3959 from DavidLin1577/patch-6

Update net_test.c
This commit is contained in:
Bernard Xiong 2020-10-18 14:19:57 +08:00 committed by GitHub
commit e6743e8c47
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 238 additions and 203 deletions

View File

@ -20,6 +20,11 @@ void udpecho_entry(void *parameter)
unsigned short port; unsigned short port;
conn = netconn_new(NETCONN_UDP); conn = netconn_new(NETCONN_UDP);
if(conn == NULL)
{
rt_kprintf("no memory error\n");
return;
}
netconn_bind(conn, IP_ADDR_ANY, 7); netconn_bind(conn, IP_ADDR_ANY, 7);
while(1) while(1)
@ -30,7 +35,10 @@ void udpecho_entry(void *parameter)
#else #else
netconn_recv(conn, &buf); netconn_recv(conn, &buf);
#endif #endif
if(buf == NULL)
{
break;
}
addr = netbuf_fromaddr(buf); addr = netbuf_fromaddr(buf);
port = netbuf_fromport(buf); port = netbuf_fromport(buf);
@ -49,6 +57,8 @@ void udpecho_entry(void *parameter)
/* release buffer */ /* release buffer */
netbuf_delete(buf); netbuf_delete(buf);
} }
netconn_delete(conn);
} }
/* /*
* UDP socket echo server * UDP socket echo server
@ -70,7 +80,7 @@ void udpecho_socket_entry(void *parameter)
{ {
/* no memory yet */ /* no memory yet */
rt_kprintf("no memory\n"); rt_kprintf("no memory\n");
goto _exit; return;
} }
/* create a UDP socket */ /* create a UDP socket */
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
@ -123,6 +133,11 @@ void tcpecho_entry(void *parameter)
/* Create a new connection identifier. */ /* Create a new connection identifier. */
conn = netconn_new(NETCONN_TCP); conn = netconn_new(NETCONN_TCP);
if(conn == NULL)
{
rt_kprintf("no memory error\n");
return;
}
/* Bind connection to well known port number 7. */ /* Bind connection to well known port number 7. */
netconn_bind(conn, NULL, TCP_ECHO_PORT); netconn_bind(conn, NULL, TCP_ECHO_PORT);
@ -155,15 +170,20 @@ void tcpecho_entry(void *parameter)
{ {
netbuf_data(buf, &data, &len); netbuf_data(buf, &data, &len);
err = netconn_write(newconn, data, len, NETCONN_COPY); err = netconn_write(newconn, data, len, NETCONN_COPY);
if(err != ERR_OK){} if(err != ERR_OK)
{
break;
} }
while(netbuf_next(buf) >= 0); }while(netbuf_next(buf) >= 0);
netbuf_delete(buf); netbuf_delete(buf);
} }
/* Close connection and discard connection identifier. */ /* Close connection and discard connection identifier. */
netconn_delete(newconn); netconn_delete(newconn);
} }
} }
netconn_delete(conn);
} }
/* /*
@ -183,7 +203,7 @@ void tcpecho_socket_entry(void *parameter)
if (recv_data == RT_NULL) if (recv_data == RT_NULL)
{ {
rt_kprintf("no memory\n"); rt_kprintf("no memory\n");
goto _exit; return;
} }
/* create a TCP socket */ /* create a TCP socket */
@ -233,8 +253,7 @@ void tcpecho_socket_entry(void *parameter)
bytes_received = recv(connected,recv_data, TCP_SOCKET_BUFFER_SIZE, 0); bytes_received = recv(connected,recv_data, TCP_SOCKET_BUFFER_SIZE, 0);
if (bytes_received <= 0) if (bytes_received <= 0)
{ {
rt_kprintf("close client connection, errno: %d\n", rt_kprintf("close client connection, errno: %d\n", rt_get_errno());
rt_get_errno());
/* connection closed. */ /* connection closed. */
lwip_close(connected); lwip_close(connected);
break; break;
@ -251,7 +270,7 @@ _exit:
if (sock != -1) lwip_close(sock); if (sock != -1) lwip_close(sock);
rt_free(recv_data); rt_free(recv_data);
return ; return;
} }
/* /*
@ -265,36 +284,52 @@ void net_test(void)
if (udpecho_tid == RT_NULL) if (udpecho_tid == RT_NULL)
{ {
udpecho_tid = rt_thread_create("uecho", udpecho_tid = rt_thread_create("uecho",
udpecho_entry, RT_NULL, udpecho_entry,
512, RT_THREAD_PRIORITY_MAX/2, 5); RT_NULL,
512,
RT_THREAD_PRIORITY_MAX/2, 5);
if (udpecho_tid != RT_NULL) if (udpecho_tid != RT_NULL)
{
rt_thread_startup(udpecho_tid); rt_thread_startup(udpecho_tid);
} }
}
if (udpecho_socket_tid == RT_NULL) if (udpecho_socket_tid == RT_NULL)
{ {
udpecho_socket_tid = rt_thread_create("uecho_s", udpecho_socket_tid = rt_thread_create("uecho_s",
udpecho_socket_entry, RT_NULL, udpecho_socket_entry,
512, RT_THREAD_PRIORITY_MAX/2 + 1, 5); RT_NULL,
512,
RT_THREAD_PRIORITY_MAX/2 + 1, 5);
if (udpecho_socket_tid != RT_NULL) if (udpecho_socket_tid != RT_NULL)
{
rt_thread_startup(udpecho_socket_tid); rt_thread_startup(udpecho_socket_tid);
} }
}
if (tcpecho_tid == RT_NULL) if (tcpecho_tid == RT_NULL)
{ {
tcpecho_tid = rt_thread_create("techo", tcpecho_tid = rt_thread_create("techo",
tcpecho_entry, RT_NULL, tcpecho_entry,
512, RT_THREAD_PRIORITY_MAX/2 + 2, 5); RT_NULL,
512,
RT_THREAD_PRIORITY_MAX/2 + 2, 5);
if (tcpecho_tid != RT_NULL) if (tcpecho_tid != RT_NULL)
{
rt_thread_startup(tcpecho_tid); rt_thread_startup(tcpecho_tid);
} }
}
if (tcpecho_socket_tid == RT_NULL) if (tcpecho_socket_tid == RT_NULL)
{ {
tcpecho_socket_tid = rt_thread_create("techo_s", tcpecho_socket_tid = rt_thread_create("techo_s",
tcpecho_socket_entry, RT_NULL, tcpecho_socket_entry,
512, RT_THREAD_PRIORITY_MAX/2 + 3, 5); RT_NULL,
512,
RT_THREAD_PRIORITY_MAX/2 + 3, 5);
if (tcpecho_socket_tid != RT_NULL) if (tcpecho_socket_tid != RT_NULL)
{
rt_thread_startup(tcpecho_socket_tid); rt_thread_startup(tcpecho_socket_tid);
} }
} }
FINSH_FUNCTION_EXPORT(net_test, network test); FINSH_FUNCTION_EXPORT(net_test, network test);