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 16:36:47 +08:00
|
|
|
|
#include <string.h>
|
2017-10-27 15:49:27 +08:00
|
|
|
|
|
2018-11-05 16:36:47 +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头文件 */
|
2018-02-06 18:59:24 +08:00
|
|
|
|
#include "netdb.h"
|
2013-01-08 21:05:02 +08:00
|
|
|
|
|
2018-11-05 16:36:47 +08:00
|
|
|
|
#define DEBUG_UDP_SERVER
|
|
|
|
|
|
2019-04-12 10:18:57 +08:00
|
|
|
|
#define DBG_TAG "UDP"
|
2018-11-05 16:36:47 +08:00
|
|
|
|
#ifdef DEBUG_UDP_SERVER
|
2019-04-12 10:18:57 +08:00
|
|
|
|
#define DBG_LVL DBG_LOG
|
2018-11-05 16:36:47 +08:00
|
|
|
|
#else
|
2019-04-12 10:18:57 +08:00
|
|
|
|
#define DBG_LVL DBG_INFO /* DBG_ERROR */
|
2018-11-05 16:36:47 +08:00
|
|
|
|
#endif
|
|
|
|
|
#include <rtdbg.h>
|
|
|
|
|
|
2018-02-06 18:59:24 +08:00
|
|
|
|
#define BUFSZ 1024
|
2013-01-08 21:05:02 +08:00
|
|
|
|
|
2018-11-05 16:36:47 +08:00
|
|
|
|
static int started = 0;
|
|
|
|
|
static int is_running = 0;
|
|
|
|
|
static int port = 5000;
|
|
|
|
|
|
2022-01-24 16:36:27 +08:00
|
|
|
|
/**
|
|
|
|
|
* @brief This function is for creating a udp server on RT-Thread
|
|
|
|
|
*/
|
2018-02-06 18:59:24 +08:00
|
|
|
|
static void udpserv(void *paramemter)
|
2013-01-08 21:05:02 +08:00
|
|
|
|
{
|
2018-02-06 18:59:24 +08:00
|
|
|
|
int sock;
|
|
|
|
|
int bytes_read;
|
|
|
|
|
char *recv_data;
|
|
|
|
|
socklen_t addr_len;
|
|
|
|
|
struct sockaddr_in server_addr, client_addr;
|
|
|
|
|
|
2018-11-05 16:36:47 +08:00
|
|
|
|
struct timeval timeout;
|
|
|
|
|
fd_set readset;
|
2022-01-24 16:36:27 +08:00
|
|
|
|
/* Allocate space for recv_data */
|
2018-02-06 18:59:24 +08:00
|
|
|
|
/* 分配接收用的数据缓冲 */
|
|
|
|
|
recv_data = rt_malloc(BUFSZ);
|
|
|
|
|
if (recv_data == RT_NULL)
|
|
|
|
|
{
|
2018-11-05 16:36:47 +08:00
|
|
|
|
LOG_E("No memory");
|
2018-02-06 18:59:24 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
2022-01-24 16:36:27 +08:00
|
|
|
|
/* Create a socket and set it to SOCK_DGRAM(UDP) */
|
2018-02-06 18:59:24 +08:00
|
|
|
|
/* 创建一个socket,类型是SOCK_DGRAM,UDP类型 */
|
|
|
|
|
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
|
|
|
|
|
{
|
2018-11-05 16:36:47 +08:00
|
|
|
|
LOG_E("Create socket error");
|
|
|
|
|
goto __exit;
|
2018-02-06 18:59:24 +08:00
|
|
|
|
}
|
2022-01-24 16:36:27 +08:00
|
|
|
|
/* Initialize server side address */
|
2018-02-06 18:59:24 +08:00
|
|
|
|
/* 初始化服务端地址 */
|
|
|
|
|
server_addr.sin_family = AF_INET;
|
2018-11-05 16:36:47 +08:00
|
|
|
|
server_addr.sin_port = htons(port);
|
2018-02-06 18:59:24 +08:00
|
|
|
|
server_addr.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
|
rt_memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero));
|
2022-01-24 16:36:27 +08:00
|
|
|
|
/* Bind socket to server side address */
|
2018-02-06 18:59:24 +08:00
|
|
|
|
/* 绑定socket到服务端地址 */
|
|
|
|
|
if (bind(sock, (struct sockaddr *)&server_addr,
|
|
|
|
|
sizeof(struct sockaddr)) == -1)
|
|
|
|
|
{
|
2018-11-05 16:36:47 +08:00
|
|
|
|
LOG_E("Unable to bind");
|
|
|
|
|
goto __exit;
|
2018-02-06 18:59:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
addr_len = sizeof(struct sockaddr);
|
2018-11-05 16:36:47 +08:00
|
|
|
|
LOG_I("UDPServer Waiting for client on port %d...", port);
|
2018-02-06 18:59:24 +08:00
|
|
|
|
|
2018-11-05 16:36:47 +08:00
|
|
|
|
started = 1;
|
|
|
|
|
is_running = 1;
|
|
|
|
|
|
|
|
|
|
timeout.tv_sec = 3;
|
|
|
|
|
timeout.tv_usec = 0;
|
|
|
|
|
|
|
|
|
|
while (is_running)
|
2018-02-06 18:59:24 +08:00
|
|
|
|
{
|
2018-11-05 16:36:47 +08:00
|
|
|
|
FD_ZERO(&readset);
|
|
|
|
|
FD_SET(sock, &readset);
|
|
|
|
|
|
|
|
|
|
/* Wait for read or write */
|
|
|
|
|
if (select(sock + 1, &readset, RT_NULL, RT_NULL, &timeout) == 0)
|
|
|
|
|
continue;
|
2022-01-24 16:36:27 +08:00
|
|
|
|
/* The maximum size received from sock is BUFSZ-1 bytes*/
|
2018-02-06 18:59:24 +08:00
|
|
|
|
/* 从sock中收取最大BUFSZ - 1字节数据 */
|
|
|
|
|
bytes_read = recvfrom(sock, recv_data, BUFSZ - 1, 0,
|
|
|
|
|
(struct sockaddr *)&client_addr, &addr_len);
|
2018-11-05 16:36:47 +08:00
|
|
|
|
if (bytes_read < 0)
|
|
|
|
|
{
|
|
|
|
|
LOG_E("Received error, close the connect.");
|
|
|
|
|
goto __exit;
|
|
|
|
|
}
|
|
|
|
|
else if (bytes_read == 0)
|
|
|
|
|
{
|
|
|
|
|
LOG_W("Received warning, recv function return 0.");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-01-24 16:36:27 +08:00
|
|
|
|
recv_data[bytes_read] = '\0'; /* Append '\0' at the end of message *//* 把末端清零 */
|
|
|
|
|
/* Output received message */
|
2018-11-05 16:36:47 +08:00
|
|
|
|
/* 输出接收的数据 */
|
|
|
|
|
LOG_D("Received data = %s", recv_data);
|
2022-01-24 16:36:27 +08:00
|
|
|
|
/* If the message received is 'exit', quit. */
|
2018-11-05 16:36:47 +08:00
|
|
|
|
/* 如果接收数据是exit,退出 */
|
|
|
|
|
if (strcmp(recv_data, "exit") == 0)
|
|
|
|
|
{
|
|
|
|
|
goto __exit;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-02-06 18:59:24 +08:00
|
|
|
|
|
2018-11-05 16:36:47 +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;
|
|
|
|
|
}
|
2018-02-06 18:59:24 +08:00
|
|
|
|
|
2022-01-24 16:36:27 +08:00
|
|
|
|
/**
|
|
|
|
|
* @brief The usage description of udp server on rt-Thread
|
|
|
|
|
*/
|
2018-11-05 16:36:47 +08:00
|
|
|
|
static void usage(void)
|
|
|
|
|
{
|
2018-11-06 09:45:31 +08:00
|
|
|
|
rt_kprintf("Usage: udpserver -p <port>\n");
|
|
|
|
|
rt_kprintf(" udpserver --stop\n");
|
|
|
|
|
rt_kprintf(" udpserver --help\n");
|
|
|
|
|
rt_kprintf("\n");
|
|
|
|
|
rt_kprintf("Miscellaneous:\n");
|
|
|
|
|
rt_kprintf(" -p Specify the host port number\n");
|
|
|
|
|
rt_kprintf(" --stop Stop udpserver program\n");
|
|
|
|
|
rt_kprintf(" --help Print help information\n");
|
2018-11-05 16:36:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-24 16:36:27 +08:00
|
|
|
|
/**
|
|
|
|
|
* @brief This function is for testing udp server on rt-Thread
|
|
|
|
|
*/
|
2018-11-05 16:36:47 +08:00
|
|
|
|
static void udpserver_test(int argc, char** argv)
|
|
|
|
|
{
|
|
|
|
|
rt_thread_t tid;
|
|
|
|
|
|
|
|
|
|
if (argc == 1 || argc > 3)
|
|
|
|
|
{
|
|
|
|
|
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], "-p") == 0)
|
|
|
|
|
{
|
|
|
|
|
if (started)
|
|
|
|
|
{
|
2020-06-13 10:57:38 +08:00
|
|
|
|
LOG_I("The udpserver has started!");
|
|
|
|
|
LOG_I("Please stop udpserver firstly, by: udpserver --stop");
|
2018-11-05 16:36:47 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
port = atoi(argv[2]);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
goto __usage;
|
2018-02-06 18:59:24 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-05 16:36:47 +08:00
|
|
|
|
tid = rt_thread_create("udp_serv",
|
|
|
|
|
udpserv, RT_NULL,
|
|
|
|
|
2048, RT_THREAD_PRIORITY_MAX/3, 20);
|
|
|
|
|
if (tid != RT_NULL)
|
|
|
|
|
{
|
|
|
|
|
rt_thread_startup(tid);
|
|
|
|
|
}
|
2018-02-06 18:59:24 +08:00
|
|
|
|
return;
|
2018-11-05 16:36:47 +08:00
|
|
|
|
|
|
|
|
|
__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(udpserver_test, udpserver,
|
|
|
|
|
Start a udp server. Help: udpserver --help);
|
2013-01-08 21:05:02 +08:00
|
|
|
|
#endif
|