2013-01-08 21:05:02 +08:00
|
|
|
|
#include <rtthread.h>
|
2017-08-10 14:09:57 +08:00
|
|
|
|
|
|
|
|
|
#include <lwip/netdb.h> /* 为了解析主机名,需要包含netdb.h头文件 */
|
2013-01-08 21:05:02 +08:00
|
|
|
|
#include <lwip/sockets.h> /* 使用BSD socket,需要包含sockets.h头文件 */
|
|
|
|
|
|
2017-08-10 14:09:57 +08:00
|
|
|
|
#define BUFSZ 1024
|
2013-01-08 21:05:02 +08:00
|
|
|
|
|
|
|
|
|
static const char send_data[] = "This is TCP Client from RT-Thread."; /* 发送用到的数据 */
|
|
|
|
|
void tcpclient(const char* url, int port)
|
|
|
|
|
{
|
2017-08-10 14:09:57 +08:00
|
|
|
|
int ret;
|
2013-01-08 21:05:02 +08:00
|
|
|
|
char *recv_data;
|
|
|
|
|
struct hostent *host;
|
|
|
|
|
int sock, bytes_received;
|
|
|
|
|
struct sockaddr_in server_addr;
|
|
|
|
|
|
|
|
|
|
/* 通过函数入口参数url获得host地址(如果是域名,会做域名解析) */
|
|
|
|
|
host = gethostbyname(url);
|
|
|
|
|
|
|
|
|
|
/* 分配用于存放接收数据的缓冲 */
|
|
|
|
|
recv_data = rt_malloc(BUFSZ);
|
|
|
|
|
if (recv_data == RT_NULL)
|
|
|
|
|
{
|
|
|
|
|
rt_kprintf("No memory\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 创建一个socket,类型是SOCKET_STREAM,TCP类型 */
|
|
|
|
|
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
|
|
|
|
|
{
|
|
|
|
|
/* 创建socket失败 */
|
|
|
|
|
rt_kprintf("Socket error\n");
|
|
|
|
|
|
|
|
|
|
/* 释放接收缓冲 */
|
|
|
|
|
rt_free(recv_data);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 初始化预连接的服务端地址 */
|
|
|
|
|
server_addr.sin_family = AF_INET;
|
|
|
|
|
server_addr.sin_port = htons(port);
|
|
|
|
|
server_addr.sin_addr = *((struct in_addr *)host->h_addr);
|
|
|
|
|
rt_memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero));
|
|
|
|
|
|
|
|
|
|
/* 连接到服务端 */
|
|
|
|
|
if (connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
|
|
|
|
|
{
|
|
|
|
|
/* 连接失败 */
|
|
|
|
|
rt_kprintf("Connect fail!\n");
|
|
|
|
|
lwip_close(sock);
|
|
|
|
|
|
|
|
|
|
/*释放接收缓冲 */
|
|
|
|
|
rt_free(recv_data);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while(1)
|
|
|
|
|
{
|
|
|
|
|
/* 从sock连接中接收最大BUFSZ - 1字节数据 */
|
|
|
|
|
bytes_received = recv(sock, recv_data, BUFSZ - 1, 0);
|
2017-08-10 10:46:47 +08:00
|
|
|
|
if (bytes_received < 0)
|
2013-01-08 21:05:02 +08:00
|
|
|
|
{
|
|
|
|
|
/* 接收失败,关闭这个连接 */
|
|
|
|
|
lwip_close(sock);
|
2017-08-10 14:09:57 +08:00
|
|
|
|
rt_kprintf("\nreceived error,close the socket.\r\n");
|
|
|
|
|
|
2013-01-08 21:05:02 +08:00
|
|
|
|
/* 释放接收缓冲 */
|
|
|
|
|
rt_free(recv_data);
|
|
|
|
|
break;
|
2017-08-10 14:09:57 +08:00
|
|
|
|
}
|
|
|
|
|
else if (bytes_received == 0)
|
|
|
|
|
{
|
|
|
|
|
/* 打印recv函数返回值为0的警告信息 */
|
|
|
|
|
rt_kprintf("\nReceived warning,recv function return 0.\r\n");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2013-01-08 21:05:02 +08:00
|
|
|
|
|
|
|
|
|
/* 有接收到数据,把末端清零 */
|
|
|
|
|
recv_data[bytes_received] = '\0';
|
|
|
|
|
|
|
|
|
|
if (strcmp(recv_data , "q") == 0 || strcmp(recv_data , "Q") == 0)
|
|
|
|
|
{
|
|
|
|
|
/* 如果是首字母是q或Q,关闭这个连接 */
|
|
|
|
|
lwip_close(sock);
|
2017-08-10 14:09:57 +08:00
|
|
|
|
rt_kprintf("\n got a 'q' or 'Q',close the socket.\r\n");
|
|
|
|
|
|
2013-01-08 21:05:02 +08:00
|
|
|
|
/* 释放接收缓冲 */
|
|
|
|
|
rt_free(recv_data);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/* 在控制终端显示收到的数据 */
|
2014-08-06 16:19:13 +08:00
|
|
|
|
rt_kprintf("\nReceived data = %s " , recv_data);
|
2013-01-08 21:05:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 发送数据到sock连接 */
|
2017-08-10 10:46:47 +08:00
|
|
|
|
ret = send(sock,send_data,strlen(send_data), 0);
|
2017-08-10 14:09:57 +08:00
|
|
|
|
if (ret < 0)
|
2017-08-10 10:46:47 +08:00
|
|
|
|
{
|
|
|
|
|
/* 接收失败,关闭这个连接 */
|
|
|
|
|
lwip_close(sock);
|
2017-08-10 14:09:57 +08:00
|
|
|
|
rt_kprintf("\nsend error,close the socket.\r\n");
|
|
|
|
|
|
|
|
|
|
rt_free(recv_data);
|
2017-08-10 10:46:47 +08:00
|
|
|
|
break;
|
2017-08-10 14:09:57 +08:00
|
|
|
|
}
|
|
|
|
|
else if (ret == 0)
|
|
|
|
|
{
|
|
|
|
|
/* 打印send函数返回值为0的警告信息 */
|
|
|
|
|
rt_kprintf("\n Send warning,send function return 0.\r\n");
|
|
|
|
|
}
|
2013-01-08 21:05:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef RT_USING_FINSH
|
|
|
|
|
#include <finsh.h>
|
|
|
|
|
/* 输出tcpclient函数到finsh shell中 */
|
|
|
|
|
FINSH_FUNCTION_EXPORT(tcpclient, startup tcp client);
|
|
|
|
|
#endif
|