XFusion API v1.3.0
载入中...
搜索中...
未找到
examples/protocols/sockets/udp_client/main/xf_main.c

sockets udp 客户端 示例。

1
17/* ==================== [Includes] ========================================== */
18
19#include "lwip/err.h"
20#include "lwip/sockets.h"
21#include "lwip/sys.h"
22#include "lwip/netdb.h"
23#include "lwip/inet.h"
24
25#include "xf_utils.h"
26#include "xf_wifi.h"
27#include "xf_netif.h"
28#include "xf_osal.h"
29
30#include "ex_easy_wifi.h"
31
32/* ==================== [Defines] =========================================== */
33
34#define PORT CONFIG_EXAMPLE_PORT
35#define EXAMPLE_DELAY_MS 1000
36
37/* ==================== [Typedefs] ========================================== */
38
39/* ==================== [Static Prototypes] ================================= */
40
41static void _example_thread(void *argument);
42
43/* ==================== [Static Variables] ================================== */
44
45static const char *TAG = "sta";
46static const char *payload = "This is a message from client.";
47
48static xf_osal_thread_t s_thread_hdl = NULL;
49#define EX_THREAD_NAME "ex_thread"
50#define EX_THREAD_PRIORITY XF_OSAL_PRIORITY_NORMOL
51#define EX_THREAD_STACK_SIZE (1024 * 4)
54 .priority = EX_THREAD_PRIORITY,
55 .stack_size = EX_THREAD_STACK_SIZE,
56};
57
58/* ==================== [Macros] ============================================ */
59
60/* ==================== [Global Functions] ================================== */
61
62void xf_main(void)
63{
65 if (s_thread_hdl == NULL) {
66 XF_LOGE(TAG, "xf_osal_thread_create error");
67 return;
68 }
69}
70
71/* ==================== [Static Functions] ================================== */
72
73static void _example_thread(void *argument)
74{
75 UNUSED(argument);
76
78
79 /* 默认网关为主机 */
80 xf_ip4_addr_t host_ip4 = {0};
81 host_ip4 = ex_easy_wifi_sta_get_gw_ip();
82
83 char rx_buffer[128];
84 int addr_family = 0;
85 int ip_protocol = 0;
86 int sockfd = 0;
87
88 struct sockaddr_in dest_addr = {0};
89 struct sockaddr_in source_addr = {0};
90 struct timeval timeout = {0};
91
92 dest_addr.sin_addr.s_addr = host_ip4.addr;
93 dest_addr.sin_family = AF_INET;
94 dest_addr.sin_port = htons(PORT);
95 addr_family = AF_INET;
96 ip_protocol = IPPROTO_IP;
97
98 sockfd = socket(addr_family, SOCK_DGRAM, ip_protocol);
99 if (sockfd < 0) {
100 XF_LOGE(TAG, "Unable to create socket: errno %d", errno);
101 goto l_err;
102 }
103
104 /* 设置超时时间 */
105 timeout.tv_sec = 10;
106 timeout.tv_usec = 0;
107 setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
108
109 XF_LOGI(TAG, "Socket created, sending to " XF_IPSTR ":%d",
110 XF_IP2STR(&host_ip4), PORT);
111
112 for (;;) {
114
115 /* 发送 */
116 int err = sendto(sockfd,
117 payload, strlen(payload), 0,
118 (struct sockaddr *)&dest_addr, sizeof(dest_addr));
119 if (err < 0) {
120 XF_LOGE(TAG, "Error occurred during sending: errno %d", errno);
121 continue;
122 }
123 XF_LOGI(TAG, "Message sent: \"%s\".", payload);
124
125 /* 接收 */
126 socklen_t socklen = sizeof(source_addr);
127 int len = recvfrom(sockfd,
128 rx_buffer, sizeof(rx_buffer) - 1, 0,
129 (struct sockaddr *)&source_addr, &socklen);
130 if (len < 0) {
131 XF_LOGE(TAG, "recvfrom failed: errno %d", errno);
132 continue;
133 }
134
135 /* 收到数据 */
136 rx_buffer[len] = 0;
137 XF_LOGI(TAG, "Received %d bytes from " XF_IPSTR ":",
138 len, XF_IP2STR(&host_ip4));
139 XF_LOGI(TAG, "%s", rx_buffer);
140 }
141
142l_err:;
143 if (sockfd != -1) {
144 XF_LOGE(TAG, "Shutting down socket...");
145 shutdown(sockfd, 0);
146 closesocket(sockfd);
147 }
149}
#define EX_THREAD_PRIORITY
#define EX_THREAD_STACK_SIZE
xf_err_t ex_easy_wifi_sta(void)
#define EX_THREAD_NAME
xf_ip4_addr_t ex_easy_wifi_sta_get_gw_ip(void)
void xf_main(void)
Definition xf_main.c:28
#define XF_IPSTR
#define XF_IP2STR(ipaddr)
xf_err_t xf_osal_thread_delete(xf_osal_thread_t thread)
终止线程的执行。
xf_osal_thread_t xf_osal_thread_create(xf_osal_thread_func_t func, void *argument, const xf_osal_thread_attr_t *attr)
创建一个线程并将其添加到活动线程中。
void * xf_osal_thread_t
线程句柄。
xf_err_t xf_osal_delay_ms(uint32_t ms)
(睡眠)等待超时,以 ms 为单位。
#define TAG
Definition xf_main.c:24
static const xf_osal_thread_attr_t s_thread_attr
Definition xf_main.c:57
static void _example_thread(void *argument)
Definition xf_main.c:78
static xf_osal_thread_t s_thread_hdl
Definition xf_main.c:53
#define EXAMPLE_DELAY_MS
Definition xf_main.c:35
#define PORT
Definition xf_main.c:34
static const char * payload
Definition xf_main.c:46
线程的属性结构。
#define XF_LOGI(tag, format,...)
#define XF_LOGE(tag, format,...)
#define UNUSED(_x)
xf_wifi 包含 STA、AP 接口。