void vLANcommunication( void *pvParameters )
{
int32 listenfd;
do{
listenfd = socket(AF_INET, SOCK_DGRAM, );
if(listenfd == -)
{
debug("ESP8266 UDP server task > socket error\n");
vTaskDelay(/portTICK_RATE_MS);
}
}while(listenfd == -);
debug("---------------UDP server task > socket ok\n");
struct sockaddr_in server_addr;
memset(&server_addr, , 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(); /* Local port */
int32 ret;
do{
ret = bind(listenfd, (struct sockaddr *)&server_addr, sizeof(server_addr));
if (ret != )
{
debug("ESP8266 UDP server task > bind fail: %d\n",ret);
vTaskDelay(/portTICK_RATE_MS);
}
}while(ret != );
debug("---------------UDP server task > bind ok\n");
u8 *recv_buf = (u8 *)zalloc( * sizeof(u8));
int32 len = sizeof(struct sockaddr_in);
struct sockaddr_in remote_addr;
int32 recbytes;
u16 i;
u8 tmp_index;
u8 udp_RxBuf[];//单条命令不能超过50字节
u8 udp_RxBuf_index = ;
u8 udp_package_length;
u16 cmd;
for(;;)
{ //缓存的数据需要一次性读出来,否则余下的数据读取不到
recbytes = recvfrom(listenfd, recv_buf, , , (struct sockaddr *)&remote_addr, (socklen_t *)&len);
sendto(listenfd, recv_buf, recbytes, 0, (struct sockaddr *)&remote_addr, len);// debug("receive a UDP package\n");
}
}