void vLANcommunication( void *pvParameters )
{
int32 listenfd;
do{
listenfd = socket(AF_INET, SOCK_DGRAM, 0);
if(listenfd == -1)
{
debug("ESP8266 UDP server task > socket error
");
vTaskDelay(1000/portTICK_RATE_MS);
}
}while(listenfd == -1);
debug("---------------UDP server task > socket ok
");
struct sockaddr_in server_addr;
memset(&server_addr, 0, sizeof(server_addr));/* Zero out structure */
server_addr.sin_family = AF_INET; /* Internet address family,AF_INET表示IPv4*/
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_len = sizeof(server_addr);
server_addr.sin_port = htons(6666); /* Local port */
int32 ret;
do{
ret = bind(listenfd, (struct sockaddr *)&server_addr, sizeof(server_addr));
if (ret != 0)
{
debug("ESP8266 UDP server task > bind fail: %d
",ret);
vTaskDelay(1000/portTICK_RATE_MS);
}
}while(ret != 0);
debug("---------------UDP server task > bind ok
");
u8 *recv_buf = (u8 *)zalloc(128 * sizeof(u8));
int32 len = sizeof(struct sockaddr_in);
struct sockaddr_in remote_addr;
int32 recbytes;
u16 i;
u8 tmp_index;
u8 udp_RxBuf[50];//单条命令不能超过50字节
u8 udp_RxBuf_index = 0;
u8 udp_package_length;
u16 cmd;
for(;;)
{ //缓存的数据需要一次性读出来,否则余下的数据读取不到
recbytes = recvfrom(listenfd, recv_buf, 128, 0, (struct sockaddr *)&remote_addr, (socklen_t *)&len);
sendto(listenfd, recv_buf, recbytes, 0, (struct sockaddr *)&remote_addr, len);// debug("receive a UDP package
");
}
}