Complete some error handling when send or receive function returns a value is zero.
This commit is contained in:
parent
f9ec093f51
commit
07cc5047bb
|
@ -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,14 +57,18 @@ 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");
|
||||
}
|
||||
|
||||
/* 有接收到数据,把末端清零 */
|
||||
|
@ -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;
|
||||
|
|
|
@ -51,6 +51,10 @@ 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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,15 +73,30 @@ 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");
|
||||
}
|
||||
|
||||
/* 有接收到数据,把末端清零 */
|
||||
|
|
Loading…
Reference in New Issue