From b4c11dd842ac30dc02af2c833a7180a4f32c8fb7 Mon Sep 17 00:00:00 2001 From: yangpeng Date: Tue, 26 Dec 2023 16:24:40 +0800 Subject: [PATCH 1/2] =?UTF-8?q?[examples]=E6=9B=B4=E6=AD=A3tcp=E4=BE=8B?= =?UTF-8?q?=E7=A8=8B=E4=B8=ADrecv/send=E8=BF=94=E5=9B=9E=E5=80=BC=E5=88=A4?= =?UTF-8?q?=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/network/tcpclient.c | 24 +++++++++++++++--------- examples/network/tcpserver.c | 24 +++++++++++++++++------- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/examples/network/tcpclient.c b/examples/network/tcpclient.c index 453910ec90d..09b94c52162 100644 --- a/examples/network/tcpclient.c +++ b/examples/network/tcpclient.c @@ -10,6 +10,7 @@ #include #include +#include #if !defined(SAL_USING_POSIX) #error "Please enable SAL_USING_POSIX!" @@ -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 { @@ -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)); } } diff --git a/examples/network/tcpserver.c b/examples/network/tcpserver.c index 6ec5f0e7a38..4da840d627a 100644 --- a/examples/network/tcpserver.c +++ b/examples/network/tcpserver.c @@ -10,6 +10,7 @@ #include #include +#include #if !defined(SAL_USING_POSIX) #error "Please enable SAL_USING_POSIX!" @@ -139,10 +140,12 @@ static void tcpserv(void *arg) } 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 */ @@ -184,9 +187,16 @@ static void tcpserv(void *arg) } 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)); } } } From 38574d45d58d5cf76cb0f8e9b334e834934748c1 Mon Sep 17 00:00:00 2001 From: yangpeng Date: Tue, 26 Dec 2023 19:24:55 +0800 Subject: [PATCH 2/2] =?UTF-8?q?[examples][tcpserver]=E6=89=93=E5=8D=B0?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E5=80=BCerrno?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/network/tcpserver.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/network/tcpserver.c b/examples/network/tcpserver.c index 4da840d627a..9a738fe9ce1 100644 --- a/examples/network/tcpserver.c +++ b/examples/network/tcpserver.c @@ -133,7 +133,7 @@ static void tcpserv(void *arg) bytes_received = recv(connected, recv_data, BUFSZ, 0); 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; @@ -180,7 +180,7 @@ static void tcpserv(void *arg) 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;