Skip to content

Commit 382e19c

Browse files
ChungHsuanChenGuozhanxin
authored andcommitted
improve and add comments in examples/network/ including tcpserver.c
tcpclient.c udpserver.c and udpclient.c Formatting Co-authored-by: Man, Jianting (Meco) <[email protected]>
1 parent 4db9cfb commit 382e19c

File tree

4 files changed

+103
-46
lines changed

4 files changed

+103
-46
lines changed

examples/network/tcpclient.c

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
/*
2-
* Copyright (c) 2006-2021, RT-Thread Development Team
2+
* Copyright (c) 2006-2022, RT-Thread Development Team
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*
66
* Change Logs:
77
* Date Author Notes
8+
* 2022-01-24 ChungHsuan improve code comments
89
*/
910

1011
#include <rtthread.h>
@@ -16,7 +17,7 @@
1617
#include <sys/time.h>
1718
#include <sys/select.h>
1819
#endif
19-
#include <sys/socket.h> /* 使用BSD socket,需要包含socket.h头文件 */
20+
#include <sys/socket.h> /* socket.h header file is needed when using BSD socket */ /* 使用BSD socket,需要包含socket.h头文件 */
2021
#include "netdb.h"
2122

2223
#define DEBUG_TCP_CLIENT
@@ -35,8 +36,11 @@ static int started = 0;
3536
static int is_running = 0;
3637
static char url[256];
3738
static int port = 8080;
38-
static const char send_data[] = "This is TCP Client from RT-Thread."; /* 发送用到的数据 */
39+
static const char send_data[] = "This is TCP Client from RT-Thread."; /* The message be sent */ /* 发送用到的数据 */
3940

41+
/**
42+
* @brief This function is for creating a tcp client on RT-Thread
43+
*/
4044
static void tcpclient(void *arg)
4145
{
4246
int ret;
@@ -48,40 +52,42 @@ static void tcpclient(void *arg)
4852

4953
struct timeval timeout;
5054
fd_set readset;
51-
55+
/* Get host address by parameter url(Domain name resolution if input domain) */
5256
/* 通过函数入口参数url获得host地址(如果是域名,会做域名解析) */
5357
host = gethostbyname(url);
5458
if (host == RT_NULL)
5559
{
5660
LOG_E("Get host by name failed!");
5761
return;
5862
}
59-
63+
/* Allocate space for recv_data */
6064
/* 分配用于存放接收数据的缓冲 */
6165
recv_data = rt_malloc(BUFSZ);
6266
if (recv_data == RT_NULL)
6367
{
6468
LOG_E("No memory");
6569
return;
6670
}
67-
71+
/* Create a socket and set it to SOCK_STREAM(TCP) */
6872
/* 创建一个socket,类型是SOCKET_STREAM,TCP类型 */
6973
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
7074
{
75+
/* Failed on creatinf socket */
7176
/* 创建socket失败 */
7277
LOG_E("Create socket error");
7378
goto __exit;
7479
}
75-
80+
/* Initialize server side address */
7681
/* 初始化预连接的服务端地址 */
7782
server_addr.sin_family = AF_INET;
7883
server_addr.sin_port = htons(port);
7984
server_addr.sin_addr = *((struct in_addr *)host->h_addr);
8085
rt_memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero));
81-
86+
/* Connect to server */
8287
/* 连接到服务端 */
8388
if (connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
8489
{
90+
/*Failed on connecting to server*/
8591
/* 连接失败 */
8692
LOG_E("Connect fail!");
8793
goto __exit;
@@ -101,49 +107,56 @@ static void tcpclient(void *arg)
101107
/* Wait for read */
102108
if (select(sock + 1, &readset, RT_NULL, RT_NULL, &timeout) == 0)
103109
continue;
104-
110+
/* Receive the maximum size 1024 bytes from socket */
105111
/* 从sock连接中接收最大BUFSZ - 1字节数据 */
106112
bytes_received = recv(sock, recv_data, BUFSZ - 1, 0);
107113
if (bytes_received < 0)
108114
{
115+
/* Receive failed and close the connection */
109116
/* 接收失败,关闭这个连接 */
110117
LOG_E("Received error, close the socket.");
111118
goto __exit;
112119
}
113120
else if (bytes_received == 0)
114121
{
122+
/* Print warning message when recv function return 0 */
115123
/* 打印recv函数返回值为0的警告信息 */
116124
LOG_W("Received warning, recv function return 0.");
117125
continue;
118126
}
119127
else
120128
{
129+
/* Receive data sucessfully and append '\0' at the end of message */
121130
/* 有接收到数据,把末端清零 */
122131
recv_data[bytes_received] = '\0';
123132

124133
if (rt_strcmp(recv_data, "q") == 0 || rt_strcmp(recv_data, "Q") == 0)
125134
{
135+
/* If the first letter is 'q' or 'Q', close the connection */
126136
/* 如果是首字母是q或Q,关闭这个连接 */
127137
LOG_I("Got a 'q' or 'Q', close the socket.");
128138
goto __exit;
129139
}
130140
else
131141
{
142+
/* Show the message in terminal */
132143
/* 在控制终端显示收到的数据 */
133144
LOG_D("Received data = %s", recv_data);
134145
}
135146
}
136-
147+
/* Send message to connected socket */
137148
/* 发送数据到sock连接 */
138149
ret = send(sock, send_data, rt_strlen(send_data), 0);
139150
if (ret < 0)
140151
{
152+
/* Send failed, close the connection */
141153
/* 发送失败,关闭这个连接 */
142154
LOG_I("send error, close the socket.");
143155
goto __exit;
144156
}
145157
else if (ret == 0)
146158
{
159+
/* Print warning message when send function return 0 */
147160
/* 打印send函数返回值为0的警告信息 */
148161
LOG_W("Send warning, send function return 0.");
149162
}
@@ -165,6 +178,9 @@ static void tcpclient(void *arg)
165178
return;
166179
}
167180

181+
/**
182+
* @brief The usage description of tcp client on rt-Thread
183+
*/
168184
static void usage(void)
169185
{
170186
rt_kprintf("Usage: tcpclient -h <host> -p <port>\n");
@@ -178,6 +194,9 @@ static void usage(void)
178194
rt_kprintf(" --help Print help information\n");
179195
}
180196

197+
/**
198+
* @brief This function is for testing tcp client on rt-Thread
199+
*/
181200
static void tcpclient_test(int argc, char** argv)
182201
{
183202
rt_thread_t tid;

examples/network/tcpserver.c

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
/*
2-
* Copyright (c) 2006-2021, RT-Thread Development Team
2+
* Copyright (c) 2006-2022, RT-Thread Development Team
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*
66
* Change Logs:
77
* Date Author Notes
8+
* 2022-01-24 ChungHsuan improve code comments
89
*/
910

1011
#include <rtthread.h>
@@ -16,7 +17,7 @@
1617
#include <sys/time.h>
1718
#include <sys/select.h>
1819
#endif
19-
#include <sys/socket.h> /* 使用BSD socket,需要包含socket.h头文件 */
20+
#include <sys/socket.h> /* socket.h header file is needed when using BSD socket */ /* 使用BSD socket,需要包含socket.h头文件 */
2021
#include "netdb.h"
2122

2223
#define DEBUG_TCP_SERVER
@@ -34,46 +35,49 @@
3435
static int started = 0;
3536
static int is_running = 0;
3637
static int port = 5000;
37-
static const char send_data[] = "This is TCP Server from RT-Thread."; /* 发送用到的数据 */
38+
static const char send_data[] = "This is TCP Server from RT-Thread."; /* The message be sent */ /* 发送用到的数据 */
3839

40+
/**
41+
* @brief This function is for creating a tcp server on RT-Thread
42+
*/
3943
static void tcpserv(void *arg)
4044
{
4145
int ret;
42-
char *recv_data; /* 用于接收的指针,后面会做一次动态分配以请求可用内存 */
46+
char *recv_data; /* recv_data is a pointer used to receive data */ /* 用于接收的指针,后面会做一次动态分配以请求可用内存 */
4347
int sock, connected, bytes_received;
4448
struct sockaddr_in server_addr, client_addr;
4549

4650
struct timeval timeout;
4751
fd_set readset, readset_c;
4852
socklen_t sin_size = sizeof(struct sockaddr_in);
4953

50-
recv_data = rt_malloc(BUFSZ + 1); /* 分配接收用的数据缓冲 */
54+
recv_data = rt_malloc(BUFSZ + 1);/* Allocate space for recv_data */ /* 分配接收用的数据缓冲 */
5155
if (recv_data == RT_NULL)
5256
{
5357
LOG_E("No memory");
5458
return;
5559
}
56-
60+
/* Before making use of socket, socket should be created first and set the socket created to SOCK_STREAM(TCP) */
5761
/* 一个socket在使用前,需要预先创建出来,指定SOCK_STREAM为TCP的socket */
5862
if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
5963
{
6064
LOG_E("Create socket error");
6165
goto __exit;
6266
}
63-
67+
/* Initialize server side address */
6468
/* 初始化服务端地址 */
6569
server_addr.sin_family = AF_INET;
66-
server_addr.sin_port = htons(port); /* 服务端工作的端口 */
70+
server_addr.sin_port = htons(port); /*Server side port number*//* 服务端工作的端口 */
6771
server_addr.sin_addr.s_addr = INADDR_ANY;
6872
rt_memset(&(server_addr.sin_zero), 0x0, sizeof(server_addr.sin_zero));
69-
73+
/* Bind socket to server side address */
7074
/* 绑定socket到服务端地址 */
7175
if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
7276
{
7377
LOG_E("Unable to bind");
7478
goto __exit;
7579
}
76-
80+
/* Listen on socket */
7781
/* 在socket上进行监听 */
7882
if (listen(sock, 10) == -1)
7983
{
@@ -99,20 +103,21 @@ static void tcpserv(void *arg)
99103
/* Wait for read or write */
100104
if (select(sock + 1, &readset, RT_NULL, RT_NULL, &timeout) == 0)
101105
continue;
102-
106+
/* Accept a request from client and the function is blocking */
103107
/* 接受一个客户端连接socket的请求,这个函数调用是阻塞式的 */
104108
connected = accept(sock, (struct sockaddr *)&client_addr, &sin_size);
109+
/* Return the socket connected sucessfully */
105110
/* 返回的是连接成功的socket */
106111
if (connected < 0)
107112
{
108113
LOG_E("accept connection failed! errno = %d", errno);
109114
continue;
110115
}
111-
116+
/* Accept the message which points by client address */
112117
/* 接受返回的client_addr指向了客户端的地址信息 */
113118
LOG_I("I got a connection from (%s , %d)\n",
114119
inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
115-
120+
/* Handle method of client connection */
116121
/* 客户端连接的处理 */
117122
while (is_running)
118123
{
@@ -122,7 +127,7 @@ static void tcpserv(void *arg)
122127
/* Wait for read or write */
123128
if (select(connected + 1, &readset_c, RT_NULL, RT_NULL, &timeout) == 0)
124129
continue;
125-
130+
/* Receive message from connected socket. Buffer size is 1024 bytes,but it's not guranteed to receive size exactly 1024 */
126131
/* 从connected socket中接收数据,接收buffer是1024大小,但并不一定能够收到1024大小的数据 */
127132
bytes_received = recv(connected, recv_data, BUFSZ, 0);
128133
if (bytes_received < 0)
@@ -134,16 +139,18 @@ static void tcpserv(void *arg)
134139
}
135140
else if (bytes_received == 0)
136141
{
142+
/* Print warning message when recv function return 0 */
137143
/* 打印recv函数返回值为0的警告信息 */
138144
LOG_W("Received warning, recv function return 0.");
139145
continue;
140146
}
141147
else
142-
{
148+
{ /* Receive data sucessfully and append '\0' at the end of message */
143149
/* 有接收到数据,把末端清零 */
144150
recv_data[bytes_received] = '\0';
145151
if (strcmp(recv_data, "q") == 0 || strcmp(recv_data, "Q") == 0)
146152
{
153+
/* If the first letter is 'q' or 'Q', close the connection */
147154
/* 如果是首字母是q或Q,关闭这个连接 */
148155
LOG_I("Got a 'q' or 'Q', close the connect.");
149156
closesocket(connected);
@@ -152,18 +159,20 @@ static void tcpserv(void *arg)
152159
}
153160
else if (strcmp(recv_data, "exit") == 0)
154161
{
162+
/* If the message received is 'exit', close the whole server side. */
155163
/* 如果接收的是exit,则关闭整个服务端 */
156164
closesocket(connected);
157165
connected = -1;
158166
goto __exit;
159167
}
160168
else
161169
{
170+
/* Show the message in terminal */
162171
/* 在控制终端显示收到的数据 */
163172
LOG_D("Received data = %s", recv_data);
164173
}
165174
}
166-
175+
/* Send message to connected socket */
167176
/* 发送数据到connected socket */
168177
ret = send(connected, send_data, rt_strlen(send_data), 0);
169178
if (ret < 0)
@@ -175,6 +184,7 @@ static void tcpserv(void *arg)
175184
}
176185
else if (ret == 0)
177186
{
187+
/* Print warning message when send function return 0 */
178188
/* 打印send函数返回值为0的警告信息 */
179189
LOG_W("Send warning, send function return 0.");
180190
}
@@ -202,6 +212,9 @@ static void tcpserv(void *arg)
202212
return;
203213
}
204214

215+
/**
216+
* @brief The usage description of tcp server on rt-Thread
217+
*/
205218
static void usage(void)
206219
{
207220
rt_kprintf("Usage: tcpserver -p <port>\n");
@@ -214,6 +227,9 @@ static void usage(void)
214227
rt_kprintf(" --help Print help information\n");
215228
}
216229

230+
/**
231+
* @brief This function is for testing tcp server on rt-Thread
232+
*/
217233
static void tcpserver_test(int argc, char** argv)
218234
{
219235
rt_thread_t tid;

0 commit comments

Comments
 (0)