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

@ -14,41 +14,51 @@
rt_thread_t udpecho_tid = RT_NULL; rt_thread_t udpecho_tid = RT_NULL;
void udpecho_entry(void *parameter) void udpecho_entry(void *parameter)
{ {
struct netconn *conn; struct netconn *conn;
struct netbuf *buf; struct netbuf *buf;
struct ip_addr *addr; struct ip_addr *addr;
unsigned short port; unsigned short port;
conn = netconn_new(NETCONN_UDP); conn = netconn_new(NETCONN_UDP);
netconn_bind(conn, IP_ADDR_ANY, 7); if(conn == NULL)
{
rt_kprintf("no memory error\n");
return;
}
netconn_bind(conn, IP_ADDR_ANY, 7);
while(1) while(1)
{ {
/* received data to buffer */ /* received data to buffer */
#if LWIP_VERSION_MINOR==3U #if LWIP_VERSION_MINOR==3U
buf = netconn_recv(conn); buf = netconn_recv(conn);
#else #else
netconn_recv(conn, &buf); netconn_recv(conn, &buf);
#endif #endif
if(buf == NULL)
addr = netbuf_fromaddr(buf); {
port = netbuf_fromport(buf); break;
}
addr = netbuf_fromaddr(buf);
port = netbuf_fromport(buf);
/* send the data to buffer */ /* send the data to buffer */
netconn_connect(conn, addr, port); netconn_connect(conn, addr, port);
/* reset address, and send to client */ /* reset address, and send to client */
#if LWIP_VERSION_MINOR==3U #if LWIP_VERSION_MINOR==3U
buf->addr = RT_NULL; buf->addr = RT_NULL;
#else #else
buf->addr = *IP_ADDR_ANY; buf->addr = *IP_ADDR_ANY;
#endif #endif
netconn_send(conn, buf); netconn_send(conn, buf);
/* release buffer */ /* release buffer */
netbuf_delete(buf); netbuf_delete(buf);
} }
netconn_delete(conn);
} }
/* /*
* UDP socket echo server * UDP socket echo server
@ -58,57 +68,57 @@ void udpecho_entry(void *parameter)
rt_thread_t udpecho_socket_tid = RT_NULL; rt_thread_t udpecho_socket_tid = RT_NULL;
void udpecho_socket_entry(void *parameter) void udpecho_socket_entry(void *parameter)
{ {
int sock; int sock;
int bytes_read; int bytes_read;
char *recv_data; char *recv_data;
rt_uint32_t addr_len; rt_uint32_t addr_len;
struct sockaddr_in server_addr, client_addr; struct sockaddr_in server_addr, client_addr;
/* allocate the data buffer */ /* allocate the data buffer */
recv_data = rt_malloc(UDP_SOCKET_BUFFER_SIZE); recv_data = rt_malloc(UDP_SOCKET_BUFFER_SIZE);
if (recv_data == RT_NULL) if (recv_data == RT_NULL)
{ {
/* 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)
{ {
rt_kprintf("create socket error\n"); rt_kprintf("create socket error\n");
goto _exit; goto _exit;
} }
/* initialize server address */ /* initialize server address */
server_addr.sin_family = AF_INET; server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(UDP_SOCKET_ECHO_PORT); server_addr.sin_port = htons(UDP_SOCKET_ECHO_PORT);
server_addr.sin_addr.s_addr = INADDR_ANY; server_addr.sin_addr.s_addr = INADDR_ANY;
rt_memset(&(server_addr.sin_zero),0, sizeof(server_addr.sin_zero)); rt_memset(&(server_addr.sin_zero),0, sizeof(server_addr.sin_zero));
/* bind socket to server address */ /* bind socket to server address */
if (bind(sock,(struct sockaddr *)&server_addr, if (bind(sock,(struct sockaddr *)&server_addr,
sizeof(struct sockaddr)) == -1) sizeof(struct sockaddr)) == -1)
{ {
/* bind failed */ /* bind failed */
rt_kprintf("bind error\n"); rt_kprintf("bind error\n");
goto _exit; goto _exit;
} }
addr_len = sizeof(struct sockaddr); addr_len = sizeof(struct sockaddr);
while (1) while (1)
{ {
/* try to receive from UDP socket */ /* try to receive from UDP socket */
bytes_read = recvfrom(sock, recv_data, UDP_SOCKET_BUFFER_SIZE, 0, bytes_read = recvfrom(sock, recv_data, UDP_SOCKET_BUFFER_SIZE, 0,
(struct sockaddr *)&client_addr, &addr_len); (struct sockaddr *)&client_addr, &addr_len);
/* send back */ /* send back */
sendto(sock, recv_data, bytes_read, 0, sendto(sock, recv_data, bytes_read, 0,
(struct sockaddr *)&client_addr, addr_len); (struct sockaddr *)&client_addr, addr_len);
} }
_exit: _exit:
rt_free(recv_data); rt_free(recv_data);
return; return;
} }
/* /*
@ -118,52 +128,62 @@ _exit:
rt_thread_t tcpecho_tid = RT_NULL; rt_thread_t tcpecho_tid = RT_NULL;
void tcpecho_entry(void *parameter) void tcpecho_entry(void *parameter)
{ {
struct netconn *conn, *newconn; struct netconn *conn, *newconn;
err_t err; err_t err;
/* 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);
/* Tell connection to go into listening mode. */ /* Tell connection to go into listening mode. */
netconn_listen(conn); netconn_listen(conn);
while(1) while(1)
{ {
/* Grab new connection. */ /* Grab new connection. */
#if LWIP_VERSION_MINOR==3U #if LWIP_VERSION_MINOR==3U
newconn = netconn_accept(conn); newconn = netconn_accept(conn);
if(newconn != NULL) if(newconn != NULL)
#else #else
err = netconn_accept(conn, &newconn); err = netconn_accept(conn, &newconn);
if(err == ERR_OK) if(err == ERR_OK)
#endif #endif
/* Process the new connection. */ /* Process the new connection. */
{ {
struct netbuf *buf; struct netbuf *buf;
void *data; void *data;
u16_t len; u16_t len;
#if LWIP_VERSION_MINOR==3U #if LWIP_VERSION_MINOR==3U
while((buf = netconn_recv(newconn)) != NULL) while((buf = netconn_recv(newconn)) != NULL)
#else #else
while((err = netconn_recv(newconn, &buf)) == ERR_OK) while((err = netconn_recv(newconn, &buf)) == ERR_OK)
#endif #endif
{ {
do do
{ {
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)
} {
while(netbuf_next(buf) >= 0); break;
netbuf_delete(buf); }
} }while(netbuf_next(buf) >= 0);
/* Close connection and discard connection identifier. */
netconn_delete(newconn); netbuf_delete(buf);
} }
} /* Close connection and discard connection identifier. */
netconn_delete(newconn);
}
}
netconn_delete(conn);
} }
/* /*
@ -174,84 +194,83 @@ void tcpecho_entry(void *parameter)
rt_thread_t tcpecho_socket_tid = RT_NULL; rt_thread_t tcpecho_socket_tid = RT_NULL;
void tcpecho_socket_entry(void *parameter) void tcpecho_socket_entry(void *parameter)
{ {
char *recv_data; char *recv_data;
rt_uint32_t sin_size; rt_uint32_t sin_size;
int sock = -1, connected, bytes_received; int sock = -1, connected, bytes_received;
struct sockaddr_in server_addr, client_addr; struct sockaddr_in server_addr, client_addr;
recv_data = rt_malloc(TCP_SOCKET_BUFFER_SIZE); recv_data = rt_malloc(TCP_SOCKET_BUFFER_SIZE);
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 */
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{ {
rt_kprintf("create socket error\n"); rt_kprintf("create socket error\n");
goto _exit; goto _exit;
} }
/* initialize server address */ /* initialize server address */
server_addr.sin_family = AF_INET; server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(TCP_SOCKET_ECHO_PORT); server_addr.sin_port = htons(TCP_SOCKET_ECHO_PORT);
server_addr.sin_addr.s_addr = INADDR_ANY; server_addr.sin_addr.s_addr = INADDR_ANY;
rt_memset(&(server_addr.sin_zero),8, sizeof(server_addr.sin_zero)); rt_memset(&(server_addr.sin_zero),8, sizeof(server_addr.sin_zero));
/* bind to server address */ /* bind to server address */
if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
{ {
rt_kprintf("bind address failed\n"); rt_kprintf("bind address failed\n");
goto _exit; goto _exit;
} }
/* listen */ /* listen */
if (listen(sock, 5) == -1) if (listen(sock, 5) == -1)
{ {
rt_kprintf("listen error\n"); rt_kprintf("listen error\n");
goto _exit; goto _exit;
} }
sin_size = sizeof(struct sockaddr_in); sin_size = sizeof(struct sockaddr_in);
while(1) while(1)
{ {
/* accept client connected */ /* accept client connected */
connected = accept(sock, (struct sockaddr *)&client_addr, &sin_size); connected = accept(sock, (struct sockaddr *)&client_addr, &sin_size);
if (connected > 0) if (connected > 0)
{ {
int timeout; int timeout;
/* set timeout option */ /* set timeout option */
timeout = 5000; /* 5second */ timeout = 5000; /* 5second */
setsockopt(connected, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); setsockopt(connected, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
/* handle this client */ /* handle this client */
while (1) while (1)
{ {
/* receive data from this connection */ /* receive data from this connection */
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; }
}
/* send data to client */ /* send data to client */
send(connected, recv_data, bytes_received, 0); send(connected, recv_data, bytes_received, 0);
} }
} }
} }
_exit: _exit:
/* close socket */ /* close socket */
if (sock != -1) lwip_close(sock); if (sock != -1) lwip_close(sock);
rt_free(recv_data); rt_free(recv_data);
return ; return;
} }
/* /*
@ -261,40 +280,56 @@ _exit:
/* network test utilities entry */ /* network test utilities entry */
void net_test(void) void net_test(void)
{ {
/* start UDP echo server */ /* start UDP echo server */
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,
if (udpecho_tid != RT_NULL) 512,
rt_thread_startup(udpecho_tid); RT_THREAD_PRIORITY_MAX/2, 5);
} if (udpecho_tid != RT_NULL)
if (udpecho_socket_tid == RT_NULL) {
{ rt_thread_startup(udpecho_tid);
udpecho_socket_tid = rt_thread_create("uecho_s", }
udpecho_socket_entry, RT_NULL, }
512, RT_THREAD_PRIORITY_MAX/2 + 1, 5);
if (udpecho_socket_tid != RT_NULL)
rt_thread_startup(udpecho_socket_tid);
}
if (tcpecho_tid == RT_NULL) if (udpecho_socket_tid == RT_NULL)
{ {
tcpecho_tid = rt_thread_create("techo", udpecho_socket_tid = rt_thread_create("uecho_s",
tcpecho_entry, RT_NULL, udpecho_socket_entry,
512, RT_THREAD_PRIORITY_MAX/2 + 2, 5); RT_NULL,
if (tcpecho_tid != RT_NULL) 512,
rt_thread_startup(tcpecho_tid); RT_THREAD_PRIORITY_MAX/2 + 1, 5);
} if (udpecho_socket_tid != RT_NULL)
if (tcpecho_socket_tid == RT_NULL) {
{ rt_thread_startup(udpecho_socket_tid);
tcpecho_socket_tid = rt_thread_create("techo_s", }
tcpecho_socket_entry, RT_NULL, }
512, RT_THREAD_PRIORITY_MAX/2 + 3, 5);
if (tcpecho_socket_tid != RT_NULL) if (tcpecho_tid == RT_NULL)
rt_thread_startup(tcpecho_socket_tid); {
} tcpecho_tid = rt_thread_create("techo",
tcpecho_entry,
RT_NULL,
512,
RT_THREAD_PRIORITY_MAX/2 + 2, 5);
if (tcpecho_tid != RT_NULL)
{
rt_thread_startup(tcpecho_tid);
}
}
if (tcpecho_socket_tid == RT_NULL)
{
tcpecho_socket_tid = rt_thread_create("techo_s",
tcpecho_socket_entry,
RT_NULL,
512,
RT_THREAD_PRIORITY_MAX/2 + 3, 5);
if (tcpecho_socket_tid != RT_NULL)
{
rt_thread_startup(tcpecho_socket_tid);
}
} }
FINSH_FUNCTION_EXPORT(net_test, network test); FINSH_FUNCTION_EXPORT(net_test, network test);