Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[examples]更正tcp例程中recv/send返回值判断 #8422

Merged
merged 2 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions examples/network/tcpclient.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include <rtthread.h>
#include <string.h>
#include <stdlib.h>

#if !defined(SAL_USING_POSIX)
#error "Please enable SAL_USING_POSIX!"
Expand Down Expand Up @@ -114,15 +115,15 @@ static void tcpclient(void *arg)
{
/* Receive failed and close the connection */
/* 接收失败,关闭这个连接 */
LOG_E("Received error, close the socket.");
LOG_E("Received error(%d), close the socket.", errno);
goto __exit;
}
else if (bytes_received == 0)
{
/* Print warning message when recv function returns 0 */
/* 打印recv函数返回值为0的警告信息 */
LOG_W("Received warning, recv function returns 0.");
continue;
/* Socket has performed an orderly shutdown. */
/* 连接已断开 */
LOG_E("Socket has performed an orderly shutdown.");
goto __exit;
}
else
{
Expand Down Expand Up @@ -151,14 +152,19 @@ static void tcpclient(void *arg)
{
/* Send failed, close the connection */
/* 发送失败,关闭这个连接 */
LOG_I("send error, close the socket.");
LOG_I("send error(%d), close the socket.", errno);
goto __exit;
}
else if (ret == 0)
{
/* Print warning message when send function returns 0 */
/* 打印send函数返回值为0的警告信息 */
LOG_W("Send warning, send function returns 0.");
/* Socket has performed an orderly shutdown. */
/* 连接已断开 */
LOG_E("Socket has performed an orderly shutdown.");
goto __exit;
}
else if (ret != rt_strlen(send_data))
{
LOG_W("%d out of %d bytes sent.", ret, rt_strlen(send_data));
}
}

Expand Down
28 changes: 19 additions & 9 deletions examples/network/tcpserver.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include <rtthread.h>
#include <string.h>
#include <stdlib.h>

#if !defined(SAL_USING_POSIX)
#error "Please enable SAL_USING_POSIX!"
Expand Down Expand Up @@ -129,20 +130,22 @@
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大小的数据 */
bytes_received = recv(connected, recv_data, BUFSZ, 0);

Check warning on line 133 in examples/network/tcpserver.c

View workflow job for this annotation

GitHub Actions / Check Spelling

`BUFSZ` is not a recognized word -- found 11 times. (limited-references)
if (bytes_received < 0)
{
LOG_E("Received error, close the connect.");
LOG_E("Received error(%d), close the connect.", errno);
closesocket(connected);
connected = -1;
break;
}
else if (bytes_received == 0)
{
/* Print warning message when recv function returns 0 */
/* 打印recv函数返回值为0的警告信息 */
LOG_W("Received warning, recv function returns 0.");
continue;
/* Socket has performed an orderly shutdown */
/* 连接已断开 */
LOG_E("Socket has performed an orderly shutdown.");
closesocket(connected);
connected = -1;
break;
}
else
{ /* Receive data successfully and append '\0' at the end of message */
Expand Down Expand Up @@ -177,16 +180,23 @@
ret = send(connected, send_data, rt_strlen(send_data), 0);
if (ret < 0)
{
LOG_E("send error, close the connect.");
LOG_E("send error(%d), close the connect.", errno);
closesocket(connected);
connected = -1;
break;
}
else if (ret == 0)
{
/* Print warning message when send function returns 0 */
/* 打印send函数返回值为0的警告信息 */
LOG_W("Send warning, send function returns 0.");
/* Socket has performed an orderly shutdown */
/* 连接已断开 */
LOG_E("Socket has performed an orderly shutdown.");
closesocket(connected);
connected = -1;
break;
}
else if (ret != rt_strlen(send_data))
{
LOG_W("%d out of %d bytes sent.", ret, rt_strlen(send_data));
}
}
}
Expand Down
Loading