2021-03-08 07:18:51 +08:00
|
|
|
|
/*
|
2022-01-24 16:36:27 +08:00
|
|
|
|
* Copyright (c) 2006-2022, RT-Thread Development Team
|
2021-03-08 07:18:51 +08:00
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*
|
|
|
|
|
* Change Logs:
|
|
|
|
|
* Date Author Notes
|
2022-01-24 16:36:27 +08:00
|
|
|
|
* 2022-01-24 ChungHsuan improve code comments
|
2021-03-08 07:18:51 +08:00
|
|
|
|
*/
|
|
|
|
|
|
2013-01-08 21:05:02 +08:00
|
|
|
|
#include <rtthread.h>
|
2018-11-05 14:34:40 +08:00
|
|
|
|
#include <string.h>
|
2023-12-26 16:24:40 +08:00
|
|
|
|
#include <stdlib.h>
|
2017-08-10 14:09:57 +08:00
|
|
|
|
|
2018-11-05 14:34:40 +08:00
|
|
|
|
#if !defined(SAL_USING_POSIX)
|
|
|
|
|
#error "Please enable SAL_USING_POSIX!"
|
|
|
|
|
#else
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
#include <sys/select.h>
|
|
|
|
|
#endif
|
2022-01-24 16:36:27 +08:00
|
|
|
|
#include <sys/socket.h> /* socket.h header file is needed when using BSD socket */ /* 使用BSD socket,需要包含socket.h头文件 */
|
2017-10-27 15:39:57 +08:00
|
|
|
|
#include "netdb.h"
|
2013-01-08 21:05:02 +08:00
|
|
|
|
|
2018-11-05 14:34:40 +08:00
|
|
|
|
#define DEBUG_TCP_CLIENT
|
|
|
|
|
|
2019-04-12 10:18:57 +08:00
|
|
|
|
#define DBG_TAG "TCP"
|
2018-11-05 14:34:40 +08:00
|
|
|
|
#ifdef DEBUG_TCP_CLIENT
|
2019-04-12 10:18:57 +08:00
|
|
|
|
#define DBG_LVL DBG_LOG
|
2018-11-05 14:34:40 +08:00
|
|
|
|
#else
|
2019-04-12 10:18:57 +08:00
|
|
|
|
#define DBG_LVL DBG_INFO /* DBG_ERROR */
|
2018-11-05 14:34:40 +08:00
|
|
|
|
#endif
|
|
|
|
|
#include <rtdbg.h>
|
|
|
|
|
|
2017-08-10 14:09:57 +08:00
|
|
|
|
#define BUFSZ 1024
|
2013-01-08 21:05:02 +08:00
|
|
|
|
|
2018-11-05 14:34:40 +08:00
|
|
|
|
static int started = 0;
|
|
|
|
|
static int is_running = 0;
|
|
|
|
|
static char url[256];
|
|
|
|
|
static int port = 8080;
|
2022-01-24 16:36:27 +08:00
|
|
|
|
static const char send_data[] = "This is TCP Client from RT-Thread."; /* The message be sent */ /* 发送用到的数据 */
|
2018-11-05 14:34:40 +08:00
|
|
|
|
|
2022-01-24 16:36:27 +08:00
|
|
|
|
/**
|
|
|
|
|
* @brief This function is for creating a tcp client on RT-Thread
|
|
|
|
|
*/
|
2018-11-05 14:34:40 +08:00
|
|
|
|
static void tcpclient(void *arg)
|
2013-01-08 21:05:02 +08:00
|
|
|
|
{
|
2017-08-10 14:09:57 +08:00
|
|
|
|
int ret;
|
2013-01-08 21:05:02 +08:00
|
|
|
|
char *recv_data;
|
2018-11-05 14:34:40 +08:00
|
|
|
|
int bytes_received;
|
|
|
|
|
int sock = -1;
|
|
|
|
|
struct hostent *host = RT_NULL;
|
2013-01-08 21:05:02 +08:00
|
|
|
|
struct sockaddr_in server_addr;
|
|
|
|
|
|
2018-11-05 14:34:40 +08:00
|
|
|
|
struct timeval timeout;
|
|
|
|
|
fd_set readset;
|
2022-01-24 16:36:27 +08:00
|
|
|
|
/* Get host address by parameter url(Domain name resolution if input domain) */
|
2013-01-08 21:05:02 +08:00
|
|
|
|
/* 通过函数入口参数url获得host地址(如果是域名,会做域名解析) */
|
|
|
|
|
host = gethostbyname(url);
|
2018-11-05 14:34:40 +08:00
|
|
|
|
if (host == RT_NULL)
|
|
|
|
|
{
|
|
|
|
|
LOG_E("Get host by name failed!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-01-24 16:36:27 +08:00
|
|
|
|
/* Allocate space for recv_data */
|
2013-01-08 21:05:02 +08:00
|
|
|
|
/* 分配用于存放接收数据的缓冲 */
|
|
|
|
|
recv_data = rt_malloc(BUFSZ);
|
|
|
|
|
if (recv_data == RT_NULL)
|
|
|
|
|
{
|
2018-11-05 14:34:40 +08:00
|
|
|
|
LOG_E("No memory");
|
2013-01-08 21:05:02 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
2022-01-24 16:36:27 +08:00
|
|
|
|
/* Create a socket and set it to SOCK_STREAM(TCP) */
|
2013-01-08 21:05:02 +08:00
|
|
|
|
/* 创建一个socket,类型是SOCKET_STREAM,TCP类型 */
|
|
|
|
|
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
|
|
|
|
|
{
|
2023-05-18 22:42:44 +08:00
|
|
|
|
/* Failed on creating socket */
|
2013-01-08 21:05:02 +08:00
|
|
|
|
/* 创建socket失败 */
|
2018-11-05 14:34:40 +08:00
|
|
|
|
LOG_E("Create socket error");
|
|
|
|
|
goto __exit;
|
2013-01-08 21:05:02 +08:00
|
|
|
|
}
|
2022-01-24 16:36:27 +08:00
|
|
|
|
/* Initialize server side address */
|
2013-01-08 21:05:02 +08:00
|
|
|
|
/* 初始化预连接的服务端地址 */
|
|
|
|
|
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));
|
2022-01-24 16:36:27 +08:00
|
|
|
|
/* Connect to server */
|
2013-01-08 21:05:02 +08:00
|
|
|
|
/* 连接到服务端 */
|
|
|
|
|
if (connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
|
|
|
|
|
{
|
2023-05-18 22:42:44 +08:00
|
|
|
|
/* Failed on connecting to server */
|
2013-01-08 21:05:02 +08:00
|
|
|
|
/* 连接失败 */
|
2018-11-05 14:34:40 +08:00
|
|
|
|
LOG_E("Connect fail!");
|
|
|
|
|
goto __exit;
|
2013-01-08 21:05:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-05 14:34:40 +08:00
|
|
|
|
started = 1;
|
|
|
|
|
is_running = 1;
|
|
|
|
|
|
|
|
|
|
timeout.tv_sec = 3;
|
|
|
|
|
timeout.tv_usec = 0;
|
|
|
|
|
|
|
|
|
|
while (is_running)
|
2013-01-08 21:05:02 +08:00
|
|
|
|
{
|
2018-11-05 14:34:40 +08:00
|
|
|
|
FD_ZERO(&readset);
|
|
|
|
|
FD_SET(sock, &readset);
|
|
|
|
|
|
|
|
|
|
/* Wait for read */
|
|
|
|
|
if (select(sock + 1, &readset, RT_NULL, RT_NULL, &timeout) == 0)
|
|
|
|
|
continue;
|
2022-01-24 16:36:27 +08:00
|
|
|
|
/* Receive the maximum size 1024 bytes from socket */
|
2013-01-08 21:05:02 +08:00
|
|
|
|
/* 从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
|
|
|
|
{
|
2022-01-24 16:36:27 +08:00
|
|
|
|
/* Receive failed and close the connection */
|
2013-01-08 21:05:02 +08:00
|
|
|
|
/* 接收失败,关闭这个连接 */
|
2023-12-26 16:24:40 +08:00
|
|
|
|
LOG_E("Received error(%d), close the socket.", errno);
|
2018-11-05 14:34:40 +08:00
|
|
|
|
goto __exit;
|
2017-08-10 14:09:57 +08:00
|
|
|
|
}
|
|
|
|
|
else if (bytes_received == 0)
|
|
|
|
|
{
|
2023-12-26 16:24:40 +08:00
|
|
|
|
/* Socket has performed an orderly shutdown. */
|
|
|
|
|
/* 连接已断开 */
|
|
|
|
|
LOG_E("Socket has performed an orderly shutdown.");
|
|
|
|
|
goto __exit;
|
2017-08-10 14:09:57 +08:00
|
|
|
|
}
|
2013-01-08 21:05:02 +08:00
|
|
|
|
else
|
|
|
|
|
{
|
2023-05-18 22:42:44 +08:00
|
|
|
|
/* Receive data successfully and append '\0' at the end of message */
|
2018-11-05 14:34:40 +08:00
|
|
|
|
/* 有接收到数据,把末端清零 */
|
|
|
|
|
recv_data[bytes_received] = '\0';
|
|
|
|
|
|
|
|
|
|
if (rt_strcmp(recv_data, "q") == 0 || rt_strcmp(recv_data, "Q") == 0)
|
|
|
|
|
{
|
2022-01-24 16:36:27 +08:00
|
|
|
|
/* If the first letter is 'q' or 'Q', close the connection */
|
2018-11-05 14:34:40 +08:00
|
|
|
|
/* 如果是首字母是q或Q,关闭这个连接 */
|
|
|
|
|
LOG_I("Got a 'q' or 'Q', close the socket.");
|
|
|
|
|
goto __exit;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-01-24 16:36:27 +08:00
|
|
|
|
/* Show the message in terminal */
|
2018-11-05 14:34:40 +08:00
|
|
|
|
/* 在控制终端显示收到的数据 */
|
|
|
|
|
LOG_D("Received data = %s", recv_data);
|
|
|
|
|
}
|
2013-01-08 21:05:02 +08:00
|
|
|
|
}
|
2022-01-24 16:36:27 +08:00
|
|
|
|
/* Send message to connected socket */
|
2013-01-08 21:05:02 +08:00
|
|
|
|
/* 发送数据到sock连接 */
|
2018-11-05 14:34:40 +08:00
|
|
|
|
ret = send(sock, send_data, rt_strlen(send_data), 0);
|
2017-08-10 14:09:57 +08:00
|
|
|
|
if (ret < 0)
|
2017-08-10 10:46:47 +08:00
|
|
|
|
{
|
2022-01-24 16:36:27 +08:00
|
|
|
|
/* Send failed, close the connection */
|
2020-07-17 16:06:33 +08:00
|
|
|
|
/* 发送失败,关闭这个连接 */
|
2023-12-26 16:24:40 +08:00
|
|
|
|
LOG_I("send error(%d), close the socket.", errno);
|
2018-11-05 14:34:40 +08:00
|
|
|
|
goto __exit;
|
2017-08-10 14:09:57 +08:00
|
|
|
|
}
|
|
|
|
|
else if (ret == 0)
|
|
|
|
|
{
|
2023-12-26 16:24:40 +08:00
|
|
|
|
/* Socket has performed an orderly shutdown. */
|
|
|
|
|
/* 连接已断开 */
|
|
|
|
|
LOG_E("Socket has performed an orderly shutdown.");
|
|
|
|
|
goto __exit;
|
|
|
|
|
}
|
|
|
|
|
else if (ret != rt_strlen(send_data))
|
|
|
|
|
{
|
|
|
|
|
LOG_W("%d out of %d bytes sent.", ret, rt_strlen(send_data));
|
2018-11-05 14:34:40 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
__exit:
|
|
|
|
|
if (recv_data)
|
|
|
|
|
{
|
|
|
|
|
rt_free(recv_data);
|
|
|
|
|
recv_data = RT_NULL;
|
|
|
|
|
}
|
|
|
|
|
if (sock >= 0)
|
|
|
|
|
{
|
|
|
|
|
closesocket(sock);
|
|
|
|
|
sock = -1;
|
|
|
|
|
}
|
|
|
|
|
started = 0;
|
|
|
|
|
is_running = 0;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-24 16:36:27 +08:00
|
|
|
|
/**
|
|
|
|
|
* @brief The usage description of tcp client on rt-Thread
|
|
|
|
|
*/
|
2018-11-05 14:34:40 +08:00
|
|
|
|
static void usage(void)
|
|
|
|
|
{
|
2018-11-06 09:45:31 +08:00
|
|
|
|
rt_kprintf("Usage: tcpclient -h <host> -p <port>\n");
|
|
|
|
|
rt_kprintf(" tcpclient --stop\n");
|
|
|
|
|
rt_kprintf(" tcpclient --help\n");
|
|
|
|
|
rt_kprintf("\n");
|
|
|
|
|
rt_kprintf("Miscellaneous:\n");
|
|
|
|
|
rt_kprintf(" -h Specify host address\n");
|
|
|
|
|
rt_kprintf(" -p Specify the host port number\n");
|
|
|
|
|
rt_kprintf(" --stop Stop tcpclient program\n");
|
|
|
|
|
rt_kprintf(" --help Print help information\n");
|
2018-11-05 14:34:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-24 16:36:27 +08:00
|
|
|
|
/**
|
|
|
|
|
* @brief This function is for testing tcp client on rt-Thread
|
|
|
|
|
*/
|
2018-11-05 14:34:40 +08:00
|
|
|
|
static void tcpclient_test(int argc, char** argv)
|
|
|
|
|
{
|
|
|
|
|
rt_thread_t tid;
|
|
|
|
|
|
|
|
|
|
if (argc == 1 || argc > 5)
|
|
|
|
|
{
|
|
|
|
|
LOG_I("Please check the command you entered!\n");
|
|
|
|
|
goto __usage;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (rt_strcmp(argv[1], "--help") == 0)
|
|
|
|
|
{
|
|
|
|
|
goto __usage;
|
|
|
|
|
}
|
|
|
|
|
else if (rt_strcmp(argv[1], "--stop") == 0)
|
|
|
|
|
{
|
|
|
|
|
is_running = 0;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
else if (rt_strcmp(argv[1], "-h") == 0 && rt_strcmp(argv[3], "-p") == 0)
|
|
|
|
|
{
|
|
|
|
|
if (started)
|
|
|
|
|
{
|
|
|
|
|
LOG_I("The tcpclient has started!");
|
|
|
|
|
LOG_I("Please stop tcpclient firstly, by: tcpclient --stop");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (rt_strlen(argv[2]) > sizeof(url))
|
|
|
|
|
{
|
|
|
|
|
LOG_E("The input url is too long, max %d bytes!", sizeof(url));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
rt_memset(url, 0x0, sizeof(url));
|
|
|
|
|
rt_strncpy(url, argv[2], rt_strlen(argv[2]));
|
|
|
|
|
port = atoi(argv[4]);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
goto __usage;
|
2017-08-10 14:09:57 +08:00
|
|
|
|
}
|
2013-01-08 21:05:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-05 14:34:40 +08:00
|
|
|
|
tid = rt_thread_create("tcp_client",
|
|
|
|
|
tcpclient, RT_NULL,
|
|
|
|
|
2048, RT_THREAD_PRIORITY_MAX/3, 20);
|
|
|
|
|
if (tid != RT_NULL)
|
|
|
|
|
{
|
|
|
|
|
rt_thread_startup(tid);
|
|
|
|
|
}
|
2013-01-08 21:05:02 +08:00
|
|
|
|
return;
|
2018-11-05 14:34:40 +08:00
|
|
|
|
|
|
|
|
|
__usage:
|
|
|
|
|
usage();
|
2013-01-08 21:05:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef RT_USING_FINSH
|
2018-11-05 14:34:40 +08:00
|
|
|
|
MSH_CMD_EXPORT_ALIAS(tcpclient_test, tcpclient,
|
|
|
|
|
Start a tcp client. Help: tcpclient --help);
|
2013-01-08 21:05:02 +08:00
|
|
|
|
#endif
|