提交 382e19cc 编写于 作者: C ChungHsuanChen 提交者: guo

improve and add comments in examples/network/ including tcpserver.c

tcpclient.c udpserver.c and udpclient.c
Formatting
Co-authored-by: mysterywolf's avatarMan, Jianting (Meco) <920369182@qq.com>
上级 4db9cfbe
/* /*
* Copyright (c) 2006-2021, RT-Thread Development Team * Copyright (c) 2006-2022, RT-Thread Development Team
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
* *
* Change Logs: * Change Logs:
* Date Author Notes * Date Author Notes
* 2022-01-24 ChungHsuan improve code comments
*/ */
#include <rtthread.h> #include <rtthread.h>
...@@ -16,7 +17,7 @@ ...@@ -16,7 +17,7 @@
#include <sys/time.h> #include <sys/time.h>
#include <sys/select.h> #include <sys/select.h>
#endif #endif
#include <sys/socket.h> /* 使用BSD socket,需要包含socket.h头文件 */ #include <sys/socket.h> /* socket.h header file is needed when using BSD socket */ /* 使用BSD socket,需要包含socket.h头文件 */
#include "netdb.h" #include "netdb.h"
#define DEBUG_TCP_CLIENT #define DEBUG_TCP_CLIENT
...@@ -35,8 +36,11 @@ static int started = 0; ...@@ -35,8 +36,11 @@ static int started = 0;
static int is_running = 0; static int is_running = 0;
static char url[256]; static char url[256];
static int port = 8080; static int port = 8080;
static const char send_data[] = "This is TCP Client from RT-Thread."; /* 发送用到的数据 */ static const char send_data[] = "This is TCP Client from RT-Thread."; /* The message be sent */ /* 发送用到的数据 */
/**
* @brief This function is for creating a tcp client on RT-Thread
*/
static void tcpclient(void *arg) static void tcpclient(void *arg)
{ {
int ret; int ret;
...@@ -48,7 +52,7 @@ static void tcpclient(void *arg) ...@@ -48,7 +52,7 @@ static void tcpclient(void *arg)
struct timeval timeout; struct timeval timeout;
fd_set readset; fd_set readset;
/* Get host address by parameter url(Domain name resolution if input domain) */
/* 通过函数入口参数url获得host地址(如果是域名,会做域名解析) */ /* 通过函数入口参数url获得host地址(如果是域名,会做域名解析) */
host = gethostbyname(url); host = gethostbyname(url);
if (host == RT_NULL) if (host == RT_NULL)
...@@ -56,7 +60,7 @@ static void tcpclient(void *arg) ...@@ -56,7 +60,7 @@ static void tcpclient(void *arg)
LOG_E("Get host by name failed!"); LOG_E("Get host by name failed!");
return; return;
} }
/* Allocate space for recv_data */
/* 分配用于存放接收数据的缓冲 */ /* 分配用于存放接收数据的缓冲 */
recv_data = rt_malloc(BUFSZ); recv_data = rt_malloc(BUFSZ);
if (recv_data == RT_NULL) if (recv_data == RT_NULL)
...@@ -64,24 +68,26 @@ static void tcpclient(void *arg) ...@@ -64,24 +68,26 @@ static void tcpclient(void *arg)
LOG_E("No memory"); LOG_E("No memory");
return; return;
} }
/* Create a socket and set it to SOCK_STREAM(TCP) */
/* 创建一个socket,类型是SOCKET_STREAM,TCP类型 */ /* 创建一个socket,类型是SOCKET_STREAM,TCP类型 */
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{ {
/* Failed on creatinf socket */
/* 创建socket失败 */ /* 创建socket失败 */
LOG_E("Create socket error"); LOG_E("Create socket error");
goto __exit; goto __exit;
} }
/* Initialize server side address */
/* 初始化预连接的服务端地址 */ /* 初始化预连接的服务端地址 */
server_addr.sin_family = AF_INET; server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port); server_addr.sin_port = htons(port);
server_addr.sin_addr = *((struct in_addr *)host->h_addr); server_addr.sin_addr = *((struct in_addr *)host->h_addr);
rt_memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero)); rt_memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero));
/* Connect to server */
/* 连接到服务端 */ /* 连接到服务端 */
if (connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) if (connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
{ {
/*Failed on connecting to server*/
/* 连接失败 */ /* 连接失败 */
LOG_E("Connect fail!"); LOG_E("Connect fail!");
goto __exit; goto __exit;
...@@ -101,49 +107,56 @@ static void tcpclient(void *arg) ...@@ -101,49 +107,56 @@ static void tcpclient(void *arg)
/* Wait for read */ /* Wait for read */
if (select(sock + 1, &readset, RT_NULL, RT_NULL, &timeout) == 0) if (select(sock + 1, &readset, RT_NULL, RT_NULL, &timeout) == 0)
continue; continue;
/* Receive the maximum size 1024 bytes from socket */
/* 从sock连接中接收最大BUFSZ - 1字节数据 */ /* 从sock连接中接收最大BUFSZ - 1字节数据 */
bytes_received = recv(sock, recv_data, BUFSZ - 1, 0); bytes_received = recv(sock, recv_data, BUFSZ - 1, 0);
if (bytes_received < 0) if (bytes_received < 0)
{ {
/* Receive failed and close the connection */
/* 接收失败,关闭这个连接 */ /* 接收失败,关闭这个连接 */
LOG_E("Received error, close the socket."); LOG_E("Received error, close the socket.");
goto __exit; goto __exit;
} }
else if (bytes_received == 0) else if (bytes_received == 0)
{ {
/* Print warning message when recv function return 0 */
/* 打印recv函数返回值为0的警告信息 */ /* 打印recv函数返回值为0的警告信息 */
LOG_W("Received warning, recv function return 0."); LOG_W("Received warning, recv function return 0.");
continue; continue;
} }
else else
{ {
/* Receive data sucessfully and append '\0' at the end of message */
/* 有接收到数据,把末端清零 */ /* 有接收到数据,把末端清零 */
recv_data[bytes_received] = '\0'; recv_data[bytes_received] = '\0';
if (rt_strcmp(recv_data, "q") == 0 || rt_strcmp(recv_data, "Q") == 0) if (rt_strcmp(recv_data, "q") == 0 || rt_strcmp(recv_data, "Q") == 0)
{ {
/* If the first letter is 'q' or 'Q', close the connection */
/* 如果是首字母是q或Q,关闭这个连接 */ /* 如果是首字母是q或Q,关闭这个连接 */
LOG_I("Got a 'q' or 'Q', close the socket."); LOG_I("Got a 'q' or 'Q', close the socket.");
goto __exit; goto __exit;
} }
else else
{ {
/* Show the message in terminal */
/* 在控制终端显示收到的数据 */ /* 在控制终端显示收到的数据 */
LOG_D("Received data = %s", recv_data); LOG_D("Received data = %s", recv_data);
} }
} }
/* Send message to connected socket */
/* 发送数据到sock连接 */ /* 发送数据到sock连接 */
ret = send(sock, send_data, rt_strlen(send_data), 0); ret = send(sock, send_data, rt_strlen(send_data), 0);
if (ret < 0) if (ret < 0)
{ {
/* Send failed, close the connection */
/* 发送失败,关闭这个连接 */ /* 发送失败,关闭这个连接 */
LOG_I("send error, close the socket."); LOG_I("send error, close the socket.");
goto __exit; goto __exit;
} }
else if (ret == 0) else if (ret == 0)
{ {
/* Print warning message when send function return 0 */
/* 打印send函数返回值为0的警告信息 */ /* 打印send函数返回值为0的警告信息 */
LOG_W("Send warning, send function return 0."); LOG_W("Send warning, send function return 0.");
} }
...@@ -165,6 +178,9 @@ __exit: ...@@ -165,6 +178,9 @@ __exit:
return; return;
} }
/**
* @brief The usage description of tcp client on rt-Thread
*/
static void usage(void) static void usage(void)
{ {
rt_kprintf("Usage: tcpclient -h <host> -p <port>\n"); rt_kprintf("Usage: tcpclient -h <host> -p <port>\n");
...@@ -178,6 +194,9 @@ static void usage(void) ...@@ -178,6 +194,9 @@ static void usage(void)
rt_kprintf(" --help Print help information\n"); rt_kprintf(" --help Print help information\n");
} }
/**
* @brief This function is for testing tcp client on rt-Thread
*/
static void tcpclient_test(int argc, char** argv) static void tcpclient_test(int argc, char** argv)
{ {
rt_thread_t tid; rt_thread_t tid;
......
/* /*
* Copyright (c) 2006-2021, RT-Thread Development Team * Copyright (c) 2006-2022, RT-Thread Development Team
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
* *
* Change Logs: * Change Logs:
* Date Author Notes * Date Author Notes
* 2022-01-24 ChungHsuan improve code comments
*/ */
#include <rtthread.h> #include <rtthread.h>
...@@ -16,7 +17,7 @@ ...@@ -16,7 +17,7 @@
#include <sys/time.h> #include <sys/time.h>
#include <sys/select.h> #include <sys/select.h>
#endif #endif
#include <sys/socket.h> /* 使用BSD socket,需要包含socket.h头文件 */ #include <sys/socket.h> /* socket.h header file is needed when using BSD socket */ /* 使用BSD socket,需要包含socket.h头文件 */
#include "netdb.h" #include "netdb.h"
#define DEBUG_TCP_SERVER #define DEBUG_TCP_SERVER
...@@ -34,12 +35,15 @@ ...@@ -34,12 +35,15 @@
static int started = 0; static int started = 0;
static int is_running = 0; static int is_running = 0;
static int port = 5000; static int port = 5000;
static const char send_data[] = "This is TCP Server from RT-Thread."; /* 发送用到的数据 */ static const char send_data[] = "This is TCP Server from RT-Thread."; /* The message be sent */ /* 发送用到的数据 */
/**
* @brief This function is for creating a tcp server on RT-Thread
*/
static void tcpserv(void *arg) static void tcpserv(void *arg)
{ {
int ret; int ret;
char *recv_data; /* 用于接收的指针,后面会做一次动态分配以请求可用内存 */ char *recv_data; /* recv_data is a pointer used to receive data */ /* 用于接收的指针,后面会做一次动态分配以请求可用内存 */
int sock, connected, bytes_received; int sock, connected, bytes_received;
struct sockaddr_in server_addr, client_addr; struct sockaddr_in server_addr, client_addr;
...@@ -47,33 +51,33 @@ static void tcpserv(void *arg) ...@@ -47,33 +51,33 @@ static void tcpserv(void *arg)
fd_set readset, readset_c; fd_set readset, readset_c;
socklen_t sin_size = sizeof(struct sockaddr_in); socklen_t sin_size = sizeof(struct sockaddr_in);
recv_data = rt_malloc(BUFSZ + 1); /* 分配接收用的数据缓冲 */ recv_data = rt_malloc(BUFSZ + 1);/* Allocate space for recv_data */ /* 分配接收用的数据缓冲 */
if (recv_data == RT_NULL) if (recv_data == RT_NULL)
{ {
LOG_E("No memory"); LOG_E("No memory");
return; return;
} }
/* Before making use of socket, socket should be created first and set the socket created to SOCK_STREAM(TCP) */
/* 一个socket在使用前,需要预先创建出来,指定SOCK_STREAM为TCP的socket */ /* 一个socket在使用前,需要预先创建出来,指定SOCK_STREAM为TCP的socket */
if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
{ {
LOG_E("Create socket error"); LOG_E("Create socket error");
goto __exit; goto __exit;
} }
/* Initialize server side address */
/* 初始化服务端地址 */ /* 初始化服务端地址 */
server_addr.sin_family = AF_INET; server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port); /* 服务端工作的端口 */ server_addr.sin_port = htons(port); /*Server side port number*//* 服务端工作的端口 */
server_addr.sin_addr.s_addr = INADDR_ANY; server_addr.sin_addr.s_addr = INADDR_ANY;
rt_memset(&(server_addr.sin_zero), 0x0, sizeof(server_addr.sin_zero)); rt_memset(&(server_addr.sin_zero), 0x0, sizeof(server_addr.sin_zero));
/* Bind socket to server side address */
/* 绑定socket到服务端地址 */ /* 绑定socket到服务端地址 */
if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
{ {
LOG_E("Unable to bind"); LOG_E("Unable to bind");
goto __exit; goto __exit;
} }
/* Listen on socket */
/* 在socket上进行监听 */ /* 在socket上进行监听 */
if (listen(sock, 10) == -1) if (listen(sock, 10) == -1)
{ {
...@@ -99,20 +103,21 @@ static void tcpserv(void *arg) ...@@ -99,20 +103,21 @@ static void tcpserv(void *arg)
/* Wait for read or write */ /* Wait for read or write */
if (select(sock + 1, &readset, RT_NULL, RT_NULL, &timeout) == 0) if (select(sock + 1, &readset, RT_NULL, RT_NULL, &timeout) == 0)
continue; continue;
/* Accept a request from client and the function is blocking */
/* 接受一个客户端连接socket的请求,这个函数调用是阻塞式的 */ /* 接受一个客户端连接socket的请求,这个函数调用是阻塞式的 */
connected = accept(sock, (struct sockaddr *)&client_addr, &sin_size); connected = accept(sock, (struct sockaddr *)&client_addr, &sin_size);
/* Return the socket connected sucessfully */
/* 返回的是连接成功的socket */ /* 返回的是连接成功的socket */
if (connected < 0) if (connected < 0)
{ {
LOG_E("accept connection failed! errno = %d", errno); LOG_E("accept connection failed! errno = %d", errno);
continue; continue;
} }
/* Accept the message which points by client address */
/* 接受返回的client_addr指向了客户端的地址信息 */ /* 接受返回的client_addr指向了客户端的地址信息 */
LOG_I("I got a connection from (%s , %d)\n", LOG_I("I got a connection from (%s , %d)\n",
inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port)); inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
/* Handle method of client connection */
/* 客户端连接的处理 */ /* 客户端连接的处理 */
while (is_running) while (is_running)
{ {
...@@ -122,7 +127,7 @@ static void tcpserv(void *arg) ...@@ -122,7 +127,7 @@ static void tcpserv(void *arg)
/* Wait for read or write */ /* Wait for read or write */
if (select(connected + 1, &readset_c, RT_NULL, RT_NULL, &timeout) == 0) if (select(connected + 1, &readset_c, RT_NULL, RT_NULL, &timeout) == 0)
continue; continue;
/* Receive message from connected socket. Buffer size is 1024 bytes,but it's not guranteed to receive size exactly 1024 */
/* 从connected socket中接收数据,接收buffer是1024大小,但并不一定能够收到1024大小的数据 */ /* 从connected socket中接收数据,接收buffer是1024大小,但并不一定能够收到1024大小的数据 */
bytes_received = recv(connected, recv_data, BUFSZ, 0); bytes_received = recv(connected, recv_data, BUFSZ, 0);
if (bytes_received < 0) if (bytes_received < 0)
...@@ -134,16 +139,18 @@ static void tcpserv(void *arg) ...@@ -134,16 +139,18 @@ static void tcpserv(void *arg)
} }
else if (bytes_received == 0) else if (bytes_received == 0)
{ {
/* Print warning message when recv function return 0 */
/* 打印recv函数返回值为0的警告信息 */ /* 打印recv函数返回值为0的警告信息 */
LOG_W("Received warning, recv function return 0."); LOG_W("Received warning, recv function return 0.");
continue; continue;
} }
else else
{ { /* Receive data sucessfully and append '\0' at the end of message */
/* 有接收到数据,把末端清零 */ /* 有接收到数据,把末端清零 */
recv_data[bytes_received] = '\0'; recv_data[bytes_received] = '\0';
if (strcmp(recv_data, "q") == 0 || strcmp(recv_data, "Q") == 0) if (strcmp(recv_data, "q") == 0 || strcmp(recv_data, "Q") == 0)
{ {
/* If the first letter is 'q' or 'Q', close the connection */
/* 如果是首字母是q或Q,关闭这个连接 */ /* 如果是首字母是q或Q,关闭这个连接 */
LOG_I("Got a 'q' or 'Q', close the connect."); LOG_I("Got a 'q' or 'Q', close the connect.");
closesocket(connected); closesocket(connected);
...@@ -152,6 +159,7 @@ static void tcpserv(void *arg) ...@@ -152,6 +159,7 @@ static void tcpserv(void *arg)
} }
else if (strcmp(recv_data, "exit") == 0) else if (strcmp(recv_data, "exit") == 0)
{ {
/* If the message received is 'exit', close the whole server side. */
/* 如果接收的是exit,则关闭整个服务端 */ /* 如果接收的是exit,则关闭整个服务端 */
closesocket(connected); closesocket(connected);
connected = -1; connected = -1;
...@@ -159,11 +167,12 @@ static void tcpserv(void *arg) ...@@ -159,11 +167,12 @@ static void tcpserv(void *arg)
} }
else else
{ {
/* Show the message in terminal */
/* 在控制终端显示收到的数据 */ /* 在控制终端显示收到的数据 */
LOG_D("Received data = %s", recv_data); LOG_D("Received data = %s", recv_data);
} }
} }
/* Send message to connected socket */
/* 发送数据到connected socket */ /* 发送数据到connected socket */
ret = send(connected, send_data, rt_strlen(send_data), 0); ret = send(connected, send_data, rt_strlen(send_data), 0);
if (ret < 0) if (ret < 0)
...@@ -175,6 +184,7 @@ static void tcpserv(void *arg) ...@@ -175,6 +184,7 @@ static void tcpserv(void *arg)
} }
else if (ret == 0) else if (ret == 0)
{ {
/* Print warning message when send function return 0 */
/* 打印send函数返回值为0的警告信息 */ /* 打印send函数返回值为0的警告信息 */
LOG_W("Send warning, send function return 0."); LOG_W("Send warning, send function return 0.");
} }
...@@ -202,6 +212,9 @@ __exit: ...@@ -202,6 +212,9 @@ __exit:
return; return;
} }
/**
* @brief The usage description of tcp server on rt-Thread
*/
static void usage(void) static void usage(void)
{ {
rt_kprintf("Usage: tcpserver -p <port>\n"); rt_kprintf("Usage: tcpserver -p <port>\n");
...@@ -214,6 +227,9 @@ static void usage(void) ...@@ -214,6 +227,9 @@ static void usage(void)
rt_kprintf(" --help Print help information\n"); rt_kprintf(" --help Print help information\n");
} }
/**
* @brief This function is for testing tcp server on rt-Thread
*/
static void tcpserver_test(int argc, char** argv) static void tcpserver_test(int argc, char** argv)
{ {
rt_thread_t tid; rt_thread_t tid;
......
/* /*
* Copyright (c) 2006-2021, RT-Thread Development Team * Copyright (c) 2006-2022, RT-Thread Development Team
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
* *
* Change Logs: * Change Logs:
* Date Author Notes * Date Author Notes
* 2022-01-24 ChungHsuan improve code comments
*/ */
#include <rtthread.h> #include <rtthread.h>
#include <sys/socket.h> /* 使用BSD socket,需要包含sockets.h头文件 */ #include <sys/socket.h> /* socket.h header file is needed when using BSD socket */ /* 使用BSD socket,需要包含sockets.h头文件 */
#include "netdb.h" #include "netdb.h"
#define DEBUG_UDP_CLIENT #define DEBUG_UDP_CLIENT
...@@ -27,14 +28,17 @@ static int is_running = 0; ...@@ -27,14 +28,17 @@ static int is_running = 0;
static char url[256]; static char url[256];
static int port = 8080; static int port = 8080;
static int count = 10; static int count = 10;
const char send_data[] = "This is UDP Client from RT-Thread.\n"; /* 发送用到的数据 */ const char send_data[] = "This is UDP Client from RT-Thread.\n";/* The message be sent */ /* 发送用到的数据 */
/**
* @brief This function is for creating a udp client on RT-Thread
*/
static void udpclient(void *arg) static void udpclient(void *arg)
{ {
int sock; int sock;
struct hostent *host; struct hostent *host;
struct sockaddr_in server_addr; struct sockaddr_in server_addr;
/* Get host address by parameter URL (domain name resolution if input domain) */
/* 通过函数入口参数url获得host地址(如果是域名,会做域名解析) */ /* 通过函数入口参数url获得host地址(如果是域名,会做域名解析) */
host = (struct hostent *) gethostbyname(url); host = (struct hostent *) gethostbyname(url);
if (host == RT_NULL) if (host == RT_NULL)
...@@ -42,14 +46,15 @@ static void udpclient(void *arg) ...@@ -42,14 +46,15 @@ static void udpclient(void *arg)
LOG_E("Get host by name failed!"); LOG_E("Get host by name failed!");
return; return;
} }
/* Create a socket and set it to SOCK_DGRAM(UDP) */
/* 创建一个socket,类型是SOCK_DGRAM,UDP类型 */ /* 创建一个socket,类型是SOCK_DGRAM,UDP类型 */
if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
{ {
/* Failed on creatinf socket */
LOG_E("Create socket error"); LOG_E("Create socket error");
return; return;
} }
/* Initialize server side address */
/* 初始化预连接的服务端地址 */ /* 初始化预连接的服务端地址 */
server_addr.sin_family = AF_INET; server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port); server_addr.sin_port = htons(port);
...@@ -58,17 +63,18 @@ static void udpclient(void *arg) ...@@ -58,17 +63,18 @@ static void udpclient(void *arg)
started = 1; started = 1;
is_running = 1; is_running = 1;
/* The total sending number(count) */
/* 总计发送count次数据 */ /* 总计发送count次数据 */
while (count && is_running) while (count && is_running)
{ {
/* Send message to server side */
/* 发送数据到服务远端 */ /* 发送数据到服务远端 */
sendto(sock, send_data, rt_strlen(send_data), 0, sendto(sock, send_data, rt_strlen(send_data), 0,
(struct sockaddr *)&server_addr, sizeof(struct sockaddr)); (struct sockaddr *)&server_addr, sizeof(struct sockaddr));
/* Thread sllep for 1 second */
/* 线程休眠一段时间 */ /* 线程休眠一段时间 */
rt_thread_mdelay(1000); rt_thread_mdelay(1000);
/* count decrease 1 */
/* 计数值减一 */ /* 计数值减一 */
count --; count --;
} }
...@@ -77,7 +83,7 @@ static void udpclient(void *arg) ...@@ -77,7 +83,7 @@ static void udpclient(void *arg)
{ {
LOG_I("UDP client send data finished!"); LOG_I("UDP client send data finished!");
} }
/* Close the socket */
/* 关闭这个socket */ /* 关闭这个socket */
if (sock >= 0) if (sock >= 0)
{ {
...@@ -88,6 +94,9 @@ static void udpclient(void *arg) ...@@ -88,6 +94,9 @@ static void udpclient(void *arg)
is_running = 0; is_running = 0;
} }
/**
* @brief The usage description of udp client on rt-Thread
*/
static void usage(void) static void usage(void)
{ {
rt_kprintf("Usage: udpclient -h <host> -p <port> [--cnt] [count]\n"); rt_kprintf("Usage: udpclient -h <host> -p <port> [--cnt] [count]\n");
...@@ -102,6 +111,9 @@ static void usage(void) ...@@ -102,6 +111,9 @@ static void usage(void)
rt_kprintf(" --help Print help information\n"); rt_kprintf(" --help Print help information\n");
} }
/**
* @brief This function is for testing udp client on rt-Thread
*/
static void udpclient_test(int argc, char** argv) static void udpclient_test(int argc, char** argv)
{ {
rt_thread_t tid; rt_thread_t tid;
......
/* /*
* Copyright (c) 2006-2021, RT-Thread Development Team * Copyright (c) 2006-2022, RT-Thread Development Team
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
* *
* Change Logs: * Change Logs:
* Date Author Notes * Date Author Notes
* 2022-01-24 ChungHsuan improve code comments
*/ */
#include <rtthread.h> #include <rtthread.h>
...@@ -16,7 +17,7 @@ ...@@ -16,7 +17,7 @@
#include <sys/time.h> #include <sys/time.h>
#include <sys/select.h> #include <sys/select.h>
#endif #endif
#include <sys/socket.h> /* 使用BSD socket,需要包含socket.h头文件 */ #include <sys/socket.h> /* socket.h header file is needed when using BSD socket */ /* 使用BSD socket,需要包含socket.h头文件 */
#include "netdb.h" #include "netdb.h"
#define DEBUG_UDP_SERVER #define DEBUG_UDP_SERVER
...@@ -35,6 +36,9 @@ static int started = 0; ...@@ -35,6 +36,9 @@ static int started = 0;
static int is_running = 0; static int is_running = 0;
static int port = 5000; static int port = 5000;
/**
* @brief This function is for creating a udp server on RT-Thread
*/
static void udpserv(void *paramemter) static void udpserv(void *paramemter)
{ {
int sock; int sock;
...@@ -45,7 +49,7 @@ static void udpserv(void *paramemter) ...@@ -45,7 +49,7 @@ static void udpserv(void *paramemter)
struct timeval timeout; struct timeval timeout;
fd_set readset; fd_set readset;
/* Allocate space for recv_data */
/* 分配接收用的数据缓冲 */ /* 分配接收用的数据缓冲 */
recv_data = rt_malloc(BUFSZ); recv_data = rt_malloc(BUFSZ);
if (recv_data == RT_NULL) if (recv_data == RT_NULL)
...@@ -53,20 +57,20 @@ static void udpserv(void *paramemter) ...@@ -53,20 +57,20 @@ static void udpserv(void *paramemter)
LOG_E("No memory"); LOG_E("No memory");
return; return;
} }
/* Create a socket and set it to SOCK_DGRAM(UDP) */
/* 创建一个socket,类型是SOCK_DGRAM,UDP类型 */ /* 创建一个socket,类型是SOCK_DGRAM,UDP类型 */
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{ {
LOG_E("Create socket error"); LOG_E("Create socket error");
goto __exit; goto __exit;
} }
/* Initialize server side address */
/* 初始化服务端地址 */ /* 初始化服务端地址 */
server_addr.sin_family = AF_INET; server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port); server_addr.sin_port = htons(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 side address */
/* 绑定socket到服务端地址 */ /* 绑定socket到服务端地址 */
if (bind(sock, (struct sockaddr *)&server_addr, if (bind(sock, (struct sockaddr *)&server_addr,
sizeof(struct sockaddr)) == -1) sizeof(struct sockaddr)) == -1)
...@@ -92,7 +96,7 @@ static void udpserv(void *paramemter) ...@@ -92,7 +96,7 @@ static void udpserv(void *paramemter)
/* Wait for read or write */ /* Wait for read or write */
if (select(sock + 1, &readset, RT_NULL, RT_NULL, &timeout) == 0) if (select(sock + 1, &readset, RT_NULL, RT_NULL, &timeout) == 0)
continue; continue;
/* The maximum size received from sock is BUFSZ-1 bytes*/
/* 从sock中收取最大BUFSZ - 1字节数据 */ /* 从sock中收取最大BUFSZ - 1字节数据 */
bytes_read = recvfrom(sock, recv_data, BUFSZ - 1, 0, bytes_read = recvfrom(sock, recv_data, BUFSZ - 1, 0,
(struct sockaddr *)&client_addr, &addr_len); (struct sockaddr *)&client_addr, &addr_len);
...@@ -108,11 +112,11 @@ static void udpserv(void *paramemter) ...@@ -108,11 +112,11 @@ static void udpserv(void *paramemter)
} }
else else
{ {
recv_data[bytes_read] = '\0'; /* 把末端清零 */ recv_data[bytes_read] = '\0'; /* Append '\0' at the end of message *//* 把末端清零 */
/* Output received message */
/* 输出接收的数据 */ /* 输出接收的数据 */
LOG_D("Received data = %s", recv_data); LOG_D("Received data = %s", recv_data);
/* If the message received is 'exit', quit. */
/* 如果接收数据是exit,退出 */ /* 如果接收数据是exit,退出 */
if (strcmp(recv_data, "exit") == 0) if (strcmp(recv_data, "exit") == 0)
{ {
...@@ -136,6 +140,9 @@ __exit: ...@@ -136,6 +140,9 @@ __exit:
is_running = 0; is_running = 0;
} }
/**
* @brief The usage description of udp server on rt-Thread
*/
static void usage(void) static void usage(void)
{ {
rt_kprintf("Usage: udpserver -p <port>\n"); rt_kprintf("Usage: udpserver -p <port>\n");
...@@ -148,6 +155,9 @@ static void usage(void) ...@@ -148,6 +155,9 @@ static void usage(void)
rt_kprintf(" --help Print help information\n"); rt_kprintf(" --help Print help information\n");
} }
/**
* @brief This function is for testing udp server on rt-Thread
*/
static void udpserver_test(int argc, char** argv) static void udpserver_test(int argc, char** argv)
{ {
rt_thread_t tid; rt_thread_t tid;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册