[examples] code cleanup.
This commit is contained in:
parent
f6b6a5cf22
commit
5f4bcfa3e1
|
@ -1,4 +1,5 @@
|
||||||
#include <rtthread.h>
|
#include <rtthread.h>
|
||||||
|
|
||||||
#include <lwip/netdb.h> /* 为了解析主机名,需要包含netdb.h头文件 */
|
#include <lwip/netdb.h> /* 为了解析主机名,需要包含netdb.h头文件 */
|
||||||
#include <lwip/sockets.h> /* 使用BSD socket,需要包含sockets.h头文件 */
|
#include <lwip/sockets.h> /* 使用BSD socket,需要包含sockets.h头文件 */
|
||||||
|
|
||||||
|
@ -7,10 +8,10 @@
|
||||||
static const char send_data[] = "This is TCP Client from RT-Thread."; /* 发送用到的数据 */
|
static const char send_data[] = "This is TCP Client from RT-Thread."; /* 发送用到的数据 */
|
||||||
void tcpclient(const char* url, int port)
|
void tcpclient(const char* url, int port)
|
||||||
{
|
{
|
||||||
|
int ret;
|
||||||
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地址(如果是域名,会做域名解析) */
|
||||||
|
@ -62,13 +63,16 @@ void tcpclient(const char* url, int port)
|
||||||
/* 接收失败,关闭这个连接 */
|
/* 接收失败,关闭这个连接 */
|
||||||
lwip_close(sock);
|
lwip_close(sock);
|
||||||
rt_kprintf("\nreceived error,close the socket.\r\n");
|
rt_kprintf("\nreceived error,close the socket.\r\n");
|
||||||
|
|
||||||
/* 释放接收缓冲 */
|
/* 释放接收缓冲 */
|
||||||
rt_free(recv_data);
|
rt_free(recv_data);
|
||||||
break;
|
break;
|
||||||
}else if (bytes_received == 0)
|
}
|
||||||
|
else if (bytes_received == 0)
|
||||||
{
|
{
|
||||||
/* 打印recv函数返回值为0的警告信息 */
|
/* 打印recv函数返回值为0的警告信息 */
|
||||||
rt_kprintf("\nReceived warning,recv function return 0.\r\n");
|
rt_kprintf("\nReceived warning,recv function return 0.\r\n");
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 有接收到数据,把末端清零 */
|
/* 有接收到数据,把末端清零 */
|
||||||
|
@ -79,6 +83,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_kprintf("\n got a 'q' or 'Q',close the socket.\r\n");
|
||||||
|
|
||||||
/* 释放接收缓冲 */
|
/* 释放接收缓冲 */
|
||||||
rt_free(recv_data);
|
rt_free(recv_data);
|
||||||
break;
|
break;
|
||||||
|
@ -96,8 +101,11 @@ void tcpclient(const char* url, int port)
|
||||||
/* 接收失败,关闭这个连接 */
|
/* 接收失败,关闭这个连接 */
|
||||||
lwip_close(sock);
|
lwip_close(sock);
|
||||||
rt_kprintf("\nsend error,close the socket.\r\n");
|
rt_kprintf("\nsend error,close the socket.\r\n");
|
||||||
|
|
||||||
|
rt_free(recv_data);
|
||||||
break;
|
break;
|
||||||
}else if (ret == 0)
|
}
|
||||||
|
else if (ret == 0)
|
||||||
{
|
{
|
||||||
/* 打印send函数返回值为0的警告信息 */
|
/* 打印send函数返回值为0的警告信息 */
|
||||||
rt_kprintf("\n Send warning,send function return 0.\r\n");
|
rt_kprintf("\n Send warning,send function return 0.\r\n");
|
||||||
|
|
|
@ -42,16 +42,21 @@ void tcp_senddata(const char* url, int port, int length)
|
||||||
{
|
{
|
||||||
/* 发送数据到sock连接 */
|
/* 发送数据到sock连接 */
|
||||||
result = send(sock, buffer_ptr, length, MSG_DONTWAIT);
|
result = send(sock, buffer_ptr, length, MSG_DONTWAIT);
|
||||||
if(result == -1) //数据发送错误处理
|
if(result < 0) //数据发送错误处理
|
||||||
{
|
{
|
||||||
rt_kprintf("TCP thread send error: %d\n", result);
|
rt_kprintf("TCP thread send error: %d\n", result);
|
||||||
lwip_close(sock); //关闭连接,重新创建连接
|
lwip_close(sock);
|
||||||
|
|
||||||
|
/* 关闭连接,重新创建连接 */
|
||||||
rt_thread_delay(10);
|
rt_thread_delay(10);
|
||||||
|
|
||||||
if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
|
if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
|
||||||
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)
|
}
|
||||||
|
else if (result == 0)
|
||||||
{
|
{
|
||||||
/* 打印send函数返回值为0的警告信息 */
|
/* 打印send函数返回值为0的警告信息 */
|
||||||
rt_kprintf("\n Send warning,send function return 0.\r\n");
|
rt_kprintf("\n Send warning,send function return 0.\r\n");
|
||||||
|
|
|
@ -76,11 +76,12 @@ void tcpserv(void* parameter)
|
||||||
ret = send(connected, send_data, strlen(send_data), 0);
|
ret = send(connected, send_data, strlen(send_data), 0);
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
{
|
{
|
||||||
/* 接收失败,关闭这个连接 */
|
/* 发送失败,关闭这个连接 */
|
||||||
lwip_close(sock);
|
lwip_close(connected);
|
||||||
rt_kprintf("\nsend error,close the socket.\r\n");
|
rt_kprintf("\nsend error,close the socket.\r\n");
|
||||||
break;
|
break;
|
||||||
}else if (ret == 0)
|
}
|
||||||
|
else if (ret == 0)
|
||||||
{
|
{
|
||||||
/* 打印send函数返回值为0的警告信息 */
|
/* 打印send函数返回值为0的警告信息 */
|
||||||
rt_kprintf("\n Send warning,send function return 0.\r\n");
|
rt_kprintf("\n Send warning,send function return 0.\r\n");
|
||||||
|
@ -93,10 +94,12 @@ void tcpserv(void* parameter)
|
||||||
/* 接收失败,关闭这个connected socket */
|
/* 接收失败,关闭这个connected socket */
|
||||||
lwip_close(connected);
|
lwip_close(connected);
|
||||||
break;
|
break;
|
||||||
}else if (bytes_received == 0)
|
}
|
||||||
|
else if (bytes_received == 0)
|
||||||
{
|
{
|
||||||
/* 打印recv函数返回值为0的警告信息 */
|
/* 打印recv函数返回值为0的警告信息 */
|
||||||
rt_kprintf("\nReceived warning,recv function return 0.\r\n");
|
rt_kprintf("\nReceived warning,recv function return 0.\r\n");
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 有接收到数据,把末端清零 */
|
/* 有接收到数据,把末端清零 */
|
||||||
|
|
Loading…
Reference in New Issue