From 07cc5047bb4b92f3a0a95df1e1f95f2cac7b9cd9 Mon Sep 17 00:00:00 2001 From: SummerGift Date: Thu, 10 Aug 2017 10:46:47 +0800 Subject: [PATCH] Complete some error handling when send or receive function returns a value is zero. --- examples/network/tcpclient.c | 26 +++++++++++++++++++++----- examples/network/tcpsendpacket.c | 6 +++++- examples/network/tcpserver.c | 24 ++++++++++++++++++++---- 3 files changed, 46 insertions(+), 10 deletions(-) diff --git a/examples/network/tcpclient.c b/examples/network/tcpclient.c index 7a0d4b5365..529c612b9a 100644 --- a/examples/network/tcpclient.c +++ b/examples/network/tcpclient.c @@ -10,6 +10,7 @@ void tcpclient(const char* url, int port) char *recv_data; struct hostent *host; int sock, bytes_received; + int ret; struct sockaddr_in server_addr; /* 通过函数入口参数url获得host地址(如果是域名,会做域名解析) */ @@ -56,15 +57,19 @@ void tcpclient(const char* url, int port) { /* 从sock连接中接收最大BUFSZ - 1字节数据 */ bytes_received = recv(sock, recv_data, BUFSZ - 1, 0); - if (bytes_received <= 0) + if (bytes_received < 0) { /* 接收失败,关闭这个连接 */ lwip_close(sock); - + rt_kprintf("\nreceived error,close the socket.\r\n"); /* 释放接收缓冲 */ rt_free(recv_data); break; - } + }else if (bytes_received == 0) + { + /* 打印recv函数返回值为0的警告信息 */ + rt_kprintf("\nReceived warning,recv function return 0.\r\n"); + } /* 有接收到数据,把末端清零 */ recv_data[bytes_received] = '\0'; @@ -73,7 +78,7 @@ void tcpclient(const char* url, int port) { /* 如果是首字母是q或Q,关闭这个连接 */ lwip_close(sock); - + rt_kprintf("\n got a 'q' or 'Q',close the socket.\r\n"); /* 释放接收缓冲 */ rt_free(recv_data); break; @@ -85,7 +90,18 @@ void tcpclient(const char* url, int port) } /* 发送数据到sock连接 */ - send(sock,send_data,strlen(send_data), 0); + ret = send(sock,send_data,strlen(send_data), 0); + if (ret < 0) + { + /* 接收失败,关闭这个连接 */ + lwip_close(sock); + rt_kprintf("\nsend error,close the socket.\r\n"); + break; + }else if (ret == 0) + { + /* 打印send函数返回值为0的警告信息 */ + rt_kprintf("\n Send warning,send function return 0.\r\n"); + } } return; diff --git a/examples/network/tcpsendpacket.c b/examples/network/tcpsendpacket.c index c4604f1079..ec23a15d23 100644 --- a/examples/network/tcpsendpacket.c +++ b/examples/network/tcpsendpacket.c @@ -51,7 +51,11 @@ void tcp_senddata(const char* url, int port, int length) rt_kprintf("TCP Socket error:%d\n",sock); err = connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)); rt_kprintf("TCP thread connect error code: %d\n", err); - } + }else if (result == 0) + { + /* ӡsendֵΪ0ľϢ */ + rt_kprintf("\n Send warning,send function return 0.\r\n"); + } } } diff --git a/examples/network/tcpserver.c b/examples/network/tcpserver.c index 3f34bc2c29..e68d9e53f0 100644 --- a/examples/network/tcpserver.c +++ b/examples/network/tcpserver.c @@ -9,6 +9,7 @@ void tcpserv(void* parameter) int sock, connected, bytes_received; struct sockaddr_in server_addr, client_addr; rt_bool_t stop = RT_FALSE; /* 停止标志 */ + int ret; recv_data = rt_malloc(1024); /* 分配接收用的数据缓冲 */ if (recv_data == RT_NULL) @@ -72,16 +73,31 @@ void tcpserv(void* parameter) while (1) { /* 发送数据到connected socket */ - send(connected, send_data, strlen(send_data), 0); - + ret = send(connected, send_data, strlen(send_data), 0); + if (ret < 0) + { + /* 接收失败,关闭这个连接 */ + lwip_close(sock); + rt_kprintf("\nsend error,close the socket.\r\n"); + break; + }else if (ret == 0) + { + /* 打印send函数返回值为0的警告信息 */ + rt_kprintf("\n Send warning,send function return 0.\r\n"); + } + /* 从connected socket中接收数据,接收buffer是1024大小,但并不一定能够收到1024大小的数据 */ bytes_received = recv(connected,recv_data, 1024, 0); - if (bytes_received <= 0) + if (bytes_received < 0) { /* 接收失败,关闭这个connected socket */ lwip_close(connected); break; - } + }else if (bytes_received == 0) + { + /* 打印recv函数返回值为0的警告信息 */ + rt_kprintf("\nReceived warning,recv function return 0.\r\n"); + } /* 有接收到数据,把末端清零 */ recv_data[bytes_received] = '\0';