2025-06-27 00:32:57 +08:00
|
|
|
|
#include <rtthread.h>
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
|
|
#if !defined(SAL_USING_POSIX)
|
|
|
|
|
|
#error "Please enable SAL_USING_POSIX!"
|
|
|
|
|
|
#else
|
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
|
#include <sys/select.h>
|
|
|
|
|
|
#endif
|
2025-07-05 19:47:28 +08:00
|
|
|
|
#include <sys/socket.h> /* ʹ<><CAB9>BSD socket<65><74><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>socket.hͷ<68>ļ<EFBFBD> */
|
2025-06-27 00:32:57 +08:00
|
|
|
|
#include "netdb.h"
|
|
|
|
|
|
|
|
|
|
|
|
#define DEBUG_UDP_SERVER
|
|
|
|
|
|
|
|
|
|
|
|
#define DBG_ENABLE
|
|
|
|
|
|
#define DBG_SECTION_NAME "UDP"
|
|
|
|
|
|
#ifdef DEBUG_UDP_SERVER
|
|
|
|
|
|
#define DBG_LEVEL DBG_LOG
|
|
|
|
|
|
#else
|
|
|
|
|
|
#define DBG_LEVEL DBG_INFO /* DBG_ERROR */
|
|
|
|
|
|
#endif
|
|
|
|
|
|
#define DBG_COLOR
|
|
|
|
|
|
#include <rtdbg.h>
|
|
|
|
|
|
|
|
|
|
|
|
#define BUFSZ 1024
|
|
|
|
|
|
|
|
|
|
|
|
static int started = 0;
|
|
|
|
|
|
static int is_running = 0;
|
|
|
|
|
|
static int port = 5000;
|
|
|
|
|
|
|
|
|
|
|
|
static void udpserv(void *paramemter)
|
|
|
|
|
|
{
|
|
|
|
|
|
int sock;
|
|
|
|
|
|
int bytes_read;
|
|
|
|
|
|
char *recv_data;
|
|
|
|
|
|
socklen_t addr_len;
|
|
|
|
|
|
struct sockaddr_in server_addr, client_addr;
|
|
|
|
|
|
|
|
|
|
|
|
struct timeval timeout;
|
|
|
|
|
|
fd_set readset;
|
|
|
|
|
|
|
2025-07-05 19:47:28 +08:00
|
|
|
|
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD><C3B5><EFBFBD><EFBFBD>ݻ<EFBFBD><DDBB><EFBFBD> */
|
2025-06-27 00:32:57 +08:00
|
|
|
|
recv_data = rt_malloc(BUFSZ);
|
|
|
|
|
|
if (recv_data == RT_NULL)
|
|
|
|
|
|
{
|
|
|
|
|
|
LOG_E("No memory");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-05 19:47:28 +08:00
|
|
|
|
/* <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>socket<65><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>SOCK_DGRAM<41><4D>UDP<44><50><EFBFBD><EFBFBD> */
|
2025-06-27 00:32:57 +08:00
|
|
|
|
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
|
|
|
|
|
|
{
|
|
|
|
|
|
LOG_E("Create socket error");
|
|
|
|
|
|
goto __exit;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-05 19:47:28 +08:00
|
|
|
|
/* <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD>ַ */
|
2025-06-27 00:32:57 +08:00
|
|
|
|
server_addr.sin_family = AF_INET;
|
|
|
|
|
|
server_addr.sin_port = htons(port);
|
|
|
|
|
|
server_addr.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
|
|
rt_memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero));
|
|
|
|
|
|
|
2025-07-05 19:47:28 +08:00
|
|
|
|
/* <20><><EFBFBD><EFBFBD>socket<65><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD>ַ */
|
2025-06-27 00:32:57 +08:00
|
|
|
|
if (bind(sock, (struct sockaddr *)&server_addr,
|
|
|
|
|
|
sizeof(struct sockaddr)) == -1)
|
|
|
|
|
|
{
|
|
|
|
|
|
LOG_E("Unable to bind");
|
|
|
|
|
|
goto __exit;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
addr_len = sizeof(struct sockaddr);
|
|
|
|
|
|
LOG_I("UDPServer Waiting for client on port %d...", port);
|
|
|
|
|
|
|
|
|
|
|
|
started = 1;
|
|
|
|
|
|
is_running = 1;
|
|
|
|
|
|
|
|
|
|
|
|
timeout.tv_sec = 3;
|
|
|
|
|
|
timeout.tv_usec = 0;
|
|
|
|
|
|
|
|
|
|
|
|
while (is_running)
|
|
|
|
|
|
{
|
|
|
|
|
|
FD_ZERO(&readset);
|
|
|
|
|
|
FD_SET(sock, &readset);
|
|
|
|
|
|
|
|
|
|
|
|
/* Wait for read or write */
|
|
|
|
|
|
if (select(sock + 1, &readset, RT_NULL, RT_NULL, &timeout) == 0)
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
2025-07-05 19:47:28 +08:00
|
|
|
|
/* <20><>sock<63><6B><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>BUFSZ - 1<>ֽ<EFBFBD><D6BD><EFBFBD><EFBFBD><EFBFBD> */
|
2025-06-27 00:32:57 +08:00
|
|
|
|
bytes_read = recvfrom(sock, recv_data, BUFSZ - 1, 0,
|
|
|
|
|
|
(struct sockaddr *)&client_addr, &addr_len);
|
|
|
|
|
|
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
|
|
|
|
|
|
{
|
2025-07-05 19:47:28 +08:00
|
|
|
|
recv_data[bytes_read] = '\0'; /* <20><>ĩ<EFBFBD><C4A9><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
2025-06-27 00:32:57 +08:00
|
|
|
|
|
2025-07-05 19:47:28 +08:00
|
|
|
|
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD> */
|
2025-06-27 00:32:57 +08:00
|
|
|
|
LOG_D("Received data = %s", recv_data);
|
|
|
|
|
|
|
2025-07-05 19:47:28 +08:00
|
|
|
|
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>exit<69><74><EFBFBD>˳<EFBFBD> */
|
2025-06-27 00:32:57 +08:00
|
|
|
|
if (strcmp(recv_data, "exit") == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
goto __exit;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
__exit:
|
|
|
|
|
|
if (recv_data)
|
|
|
|
|
|
{
|
|
|
|
|
|
rt_free(recv_data);
|
|
|
|
|
|
recv_data = RT_NULL;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (sock >= 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
closesocket(sock);
|
|
|
|
|
|
sock = -1;
|
|
|
|
|
|
}
|
|
|
|
|
|
started = 0;
|
|
|
|
|
|
is_running = 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void usage(void)
|
|
|
|
|
|
{
|
|
|
|
|
|
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");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
{
|
|
|
|
|
|
LOG_I("The tcpclient has started!");
|
|
|
|
|
|
LOG_I("Please stop tcpclient firstly, by: tcpclient --stop");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
port = atoi(argv[2]);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
goto __usage;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
tid = rt_thread_create("udp_serv",
|
|
|
|
|
|
udpserv, RT_NULL,
|
|
|
|
|
|
2048, RT_THREAD_PRIORITY_MAX/3, 20);
|
|
|
|
|
|
if (tid != RT_NULL)
|
|
|
|
|
|
{
|
|
|
|
|
|
rt_thread_startup(tid);
|
|
|
|
|
|
}
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
__usage:
|
|
|
|
|
|
usage();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef RT_USING_FINSH
|
|
|
|
|
|
MSH_CMD_EXPORT_ALIAS(udpserver_test, udpserver,
|
|
|
|
|
|
Start a udp server. Help: udpserver --help);
|
|
|
|
|
|
#endif
|