更改一下例子代码,这个例子代码的作用是本地UDP接收到0x01 0xAA指令就向目标UDP回显数据。目标IP和端口是192.168.1.220:77,当然这可以自己随意改动
// Notify the user application about UDP socket events.
uint32_t udp_cb_func (int32_t socket, const NET_ADDR *addr, const uint8_t *buf, uint32_t len) {
// Data received
if ((buf[0] == 0x01) && (len == 2)) {
// Switch LEDs on and off
// LED_out (buf[1]);
send_udp_data();
}
return (0);
}
// Send UDP data to destination client.
void send_udp_data (void) {
if (udp_sock > 0) {
// IPv4 address: 192.168.0.1
NET_ADDR addr = { NET_ADDR_IP4, 77, 192, 168, 1, 220 };//可以根据自己的IP地址来
// IPv6 address: [fe80::1c30:6cff:fea2:455e]
// NET_ADDR addr = { NET_ADDR_IP6, 2000,
// 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// 0x1c, 0x30, 0x6c, 0xff, 0xfe, 0xa2, 0x45, 0x5e };
uint8_t *sendbuf;
sendbuf = netUDP_GetBuffer (2);
sendbuf[0] = 0x01;
sendbuf[1] = 0xAA;
netUDP_Send (udp_sock, &addr, sendbuf, 2);
}
}
|