Complete some error handling when send or receive function returns a value is zero.

This commit is contained in:
SummerGift 2017-08-10 10:46:47 +08:00
parent f9ec093f51
commit 07cc5047bb
3 changed files with 46 additions and 10 deletions

View File

@ -10,6 +10,7 @@ void tcpclient(const char* url, int port)
char *recv_data; char *recv_data;
struct hostent *host; struct hostent *host;
int sock, bytes_received; int sock, bytes_received;
int ret;
struct sockaddr_in server_addr; struct sockaddr_in server_addr;
/* 通过函数入口参数url获得host地址如果是域名会做域名解析 */ /* 通过函数入口参数url获得host地址如果是域名会做域名解析 */
@ -56,15 +57,19 @@ void tcpclient(const char* url, int port)
{ {
/* 从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)
{ {
/* 接收失败,关闭这个连接 */ /* 接收失败,关闭这个连接 */
lwip_close(sock); lwip_close(sock);
rt_kprintf("\nreceived error,close the socket.\r\n");
/* 释放接收缓冲 */ /* 释放接收缓冲 */
rt_free(recv_data); rt_free(recv_data);
break; break;
} }else if (bytes_received == 0)
{
/* 打印recv函数返回值为0的警告信息 */
rt_kprintf("\nReceived warning,recv function return 0.\r\n");
}
/* 有接收到数据,把末端清零 */ /* 有接收到数据,把末端清零 */
recv_data[bytes_received] = '\0'; recv_data[bytes_received] = '\0';
@ -73,7 +78,7 @@ void tcpclient(const char* url, int port)
{ {
/* 如果是首字母是q或Q关闭这个连接 */ /* 如果是首字母是q或Q关闭这个连接 */
lwip_close(sock); lwip_close(sock);
rt_kprintf("\n got a 'q' or 'Q',close the socket.\r\n");
/* 释放接收缓冲 */ /* 释放接收缓冲 */
rt_free(recv_data); rt_free(recv_data);
break; break;
@ -85,7 +90,18 @@ void tcpclient(const char* url, int port)
} }
/* 发送数据到sock连接 */ /* 发送数据到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; return;

View File

@ -51,7 +51,11 @@ void tcp_senddata(const char* url, int port, int length)
rt_kprintf("TCP Socket error:%d\n",sock); rt_kprintf("TCP Socket error:%d\n",sock);
err = connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)); err = connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr));
rt_kprintf("TCP thread connect error code: %d\n", err); 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");
}
} }
} }

View File

@ -9,6 +9,7 @@ void tcpserv(void* parameter)
int sock, connected, bytes_received; int sock, connected, bytes_received;
struct sockaddr_in server_addr, client_addr; struct sockaddr_in server_addr, client_addr;
rt_bool_t stop = RT_FALSE; /* 停止标志 */ rt_bool_t stop = RT_FALSE; /* 停止标志 */
int ret;
recv_data = rt_malloc(1024); /* 分配接收用的数据缓冲 */ recv_data = rt_malloc(1024); /* 分配接收用的数据缓冲 */
if (recv_data == RT_NULL) if (recv_data == RT_NULL)
@ -72,16 +73,31 @@ void tcpserv(void* parameter)
while (1) while (1)
{ {
/* 发送数据到connected socket */ /* 发送数据到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大小的数据 */ /* 从connected socket中接收数据接收buffer是1024大小但并不一定能够收到1024大小的数据 */
bytes_received = recv(connected,recv_data, 1024, 0); bytes_received = recv(connected,recv_data, 1024, 0);
if (bytes_received <= 0) if (bytes_received < 0)
{ {
/* 接收失败关闭这个connected socket */ /* 接收失败关闭这个connected socket */
lwip_close(connected); lwip_close(connected);
break; break;
} }else if (bytes_received == 0)
{
/* 打印recv函数返回值为0的警告信息 */
rt_kprintf("\nReceived warning,recv function return 0.\r\n");
}
/* 有接收到数据,把末端清零 */ /* 有接收到数据,把末端清零 */
recv_data[bytes_received] = '\0'; recv_data[bytes_received] = '\0';