2013-01-08 21:05:02 +08:00
|
|
|
|
#include <rtthread.h>
|
2018-11-05 16:36:47 +08:00
|
|
|
|
|
2017-10-27 15:49:27 +08:00
|
|
|
|
#include <sys/socket.h> /* 使用BSD socket,需要包含sockets.h头文件 */
|
|
|
|
|
#include "netdb.h"
|
2013-01-08 21:05:02 +08:00
|
|
|
|
|
2018-11-05 16:36:47 +08:00
|
|
|
|
#define DEBUG_UDP_CLIENT
|
|
|
|
|
|
|
|
|
|
#define DBG_ENABLE
|
|
|
|
|
#define DBG_SECTION_NAME "UDP"
|
|
|
|
|
#ifdef DEBUG_UDP_CLIENT
|
|
|
|
|
#define DBG_LEVEL DBG_LOG
|
|
|
|
|
#else
|
|
|
|
|
#define DBG_LEVEL DBG_INFO /* DBG_ERROR */
|
|
|
|
|
#endif
|
|
|
|
|
#define DBG_COLOR
|
|
|
|
|
#include <rtdbg.h>
|
|
|
|
|
|
|
|
|
|
static int started = 0;
|
|
|
|
|
static int is_running = 0;
|
|
|
|
|
static char url[256];
|
|
|
|
|
static int port = 8080;
|
|
|
|
|
static int count = 10;
|
2013-01-08 21:05:02 +08:00
|
|
|
|
const char send_data[] = "This is UDP Client from RT-Thread.\n"; /* 发送用到的数据 */
|
2018-11-05 16:36:47 +08:00
|
|
|
|
|
|
|
|
|
static void udpclient(void *arg)
|
2013-01-08 21:05:02 +08:00
|
|
|
|
{
|
2018-02-06 18:59:24 +08:00
|
|
|
|
int sock;
|
|
|
|
|
struct hostent *host;
|
|
|
|
|
struct sockaddr_in server_addr;
|
|
|
|
|
|
|
|
|
|
/* 通过函数入口参数url获得host地址(如果是域名,会做域名解析) */
|
|
|
|
|
host = (struct hostent *) gethostbyname(url);
|
2018-11-05 16:36:47 +08:00
|
|
|
|
if (host == RT_NULL)
|
|
|
|
|
{
|
|
|
|
|
LOG_E("Get host by name failed!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-02-06 18:59:24 +08:00
|
|
|
|
|
|
|
|
|
/* 创建一个socket,类型是SOCK_DGRAM,UDP类型 */
|
2018-11-05 16:36:47 +08:00
|
|
|
|
if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
|
2018-02-06 18:59:24 +08:00
|
|
|
|
{
|
2018-11-05 16:36:47 +08:00
|
|
|
|
LOG_E("Create socket error");
|
2018-02-06 18:59:24 +08:00
|
|
|
|
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));
|
|
|
|
|
|
2018-11-05 16:36:47 +08:00
|
|
|
|
started = 1;
|
|
|
|
|
is_running = 1;
|
|
|
|
|
|
2018-02-06 18:59:24 +08:00
|
|
|
|
/* 总计发送count次数据 */
|
2018-11-05 16:36:47 +08:00
|
|
|
|
while (count && is_running)
|
2018-02-06 18:59:24 +08:00
|
|
|
|
{
|
|
|
|
|
/* 发送数据到服务远端 */
|
2018-11-05 16:36:47 +08:00
|
|
|
|
sendto(sock, send_data, rt_strlen(send_data), 0,
|
2018-02-06 18:59:24 +08:00
|
|
|
|
(struct sockaddr *)&server_addr, sizeof(struct sockaddr));
|
|
|
|
|
|
|
|
|
|
/* 线程休眠一段时间 */
|
2018-11-05 16:36:47 +08:00
|
|
|
|
rt_thread_mdelay(1000);
|
2018-02-06 18:59:24 +08:00
|
|
|
|
|
|
|
|
|
/* 计数值减一 */
|
|
|
|
|
count --;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-05 16:36:47 +08:00
|
|
|
|
if (count == 0)
|
|
|
|
|
{
|
|
|
|
|
LOG_I("UDP client send data finished!");
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-06 18:59:24 +08:00
|
|
|
|
/* 关闭这个socket */
|
2018-11-05 16:36:47 +08:00
|
|
|
|
if (sock >= 0)
|
|
|
|
|
{
|
|
|
|
|
closesocket(sock);
|
|
|
|
|
sock = -1;
|
|
|
|
|
}
|
|
|
|
|
started = 0;
|
|
|
|
|
is_running = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void usage(void)
|
|
|
|
|
{
|
|
|
|
|
LOG_I("Usage: udpclient -h <host> -p <port> [--cnt] [count]");
|
|
|
|
|
LOG_I(" udpclient --stop");
|
|
|
|
|
LOG_I(" udpclient --help");
|
|
|
|
|
LOG_I("");
|
|
|
|
|
LOG_I("Miscellaneous:");
|
|
|
|
|
LOG_I(" -h Specify host address");
|
|
|
|
|
LOG_I(" -p Specify the host port number");
|
|
|
|
|
LOG_I(" --cnt Specify the send data count");
|
|
|
|
|
LOG_I(" --stop Stop tcpclient program");
|
|
|
|
|
LOG_I(" --help Print help information");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void udpclient_test(int argc, char** argv)
|
|
|
|
|
{
|
|
|
|
|
rt_thread_t tid;
|
|
|
|
|
|
|
|
|
|
if (argc == 1 || argc > 7)
|
|
|
|
|
{
|
|
|
|
|
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 (argc == 7 && rt_strcmp(argv[6], "--cnt") == 0)
|
|
|
|
|
{
|
|
|
|
|
count = atoi(argv[7]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tid = rt_thread_create("udp_client",
|
|
|
|
|
udpclient, RT_NULL,
|
|
|
|
|
2048, RT_THREAD_PRIORITY_MAX/3, 20);
|
|
|
|
|
if (tid != RT_NULL)
|
|
|
|
|
{
|
|
|
|
|
rt_thread_startup(tid);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
__usage:
|
|
|
|
|
usage();
|
2013-01-08 21:05:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef RT_USING_FINSH
|
2018-11-05 16:36:47 +08:00
|
|
|
|
MSH_CMD_EXPORT_ALIAS(udpclient_test, udpclient,
|
|
|
|
|
Start a udp client. Help: udpclient --help);
|
2013-01-08 21:05:02 +08:00
|
|
|
|
#endif
|