ESP32 连接WIFI路由器时UDP通信异常 [WIFI-1710]
Posted: Mon Feb 10, 2020 3:02 pm
ESP32模块使用WIFI连接路由器,在使用SOCKET进行UDP通信时,发现在某些路由器下发不出数据,在同一个地方,连接华为的路由器可以正常通信,连接带光猫的天翼路由器则发出的数据收不到,也不能接收客户端发过来的数据,造成产品的兼容性不好,不知道有没遇到类似的情况?测试代码如下:
recSocket = CreateUDPClient(NET_CFG_LISTEN_PORT);
ESP_LOGI(TAG,"Create Config listen socket!,socket:%d",recSocket);
if(recSocket<=0){
ESP_LOGE(TAG,"Create listen socket fail");
return;
}
rec_buffer = audio_malloc(NET_CFG_RECBUFF_SIZE*sizeof(char));
if(rec_buffer == NULL){
ESP_LOGE(TAG,"Memory exhaust!");
return;
}
struct sockaddr_in peerAddr;
socklen_t peerLen = sizeof(peerAddr);
while(1){
NetworkConfigSend(dest_ip_address,"Hello",5);
memset(rec_buffer,0,NET_CFG_RECBUFF_SIZE);
if(recSocket>0){
ESP_LOGE(TAG,"Read socket data!");
//bytes_read = read (recSocket, rec_buffer, NET_CFG_RECBUFF_SIZE);
bytes_read = recvfrom(recSocket,rec_buffer,NET_CFG_RECBUFF_SIZE,0,(struct sockaddr *)&peerAddr,&peerLen);
if(bytes_read>0){
ESP_LOGI(TAG,"Receive packet:%s,bytes:%d",rec_buffer,bytes_read);
ESP_LOGI(TAG,"peer address = %s:%d\n", inet_ntoa(peerAddr.sin_addr), ntohs(peerAddr.sin_port));
if(NetCommandProcess(rec_buffer)>0){
ESP_LOGI(TAG,"Command process OK");
}
dest_ip_address = peerAddr.sin_addr.s_addr;
}
if(bytes_read < 0){
ESP_LOGE(TAG,"UDP Receive had error!Reset socket..");
recSocket = 0;
}
else{
ESP_LOGE(TAG,"Rec timeout");
}
}else{
vTaskDelay(2000);
recSocket = CreateUDPClient(NET_CFG_LISTEN_PORT);
ESP_LOGI(TAG,"Create Config listen socket!,socket:%d",recSocket);
if(recSocket<=0){
ESP_LOGE(TAG,"UDP Create faile!");
break;
}
}
}
recSocket = CreateUDPClient(NET_CFG_LISTEN_PORT);
ESP_LOGI(TAG,"Create Config listen socket!,socket:%d",recSocket);
if(recSocket<=0){
ESP_LOGE(TAG,"Create listen socket fail");
return;
}
rec_buffer = audio_malloc(NET_CFG_RECBUFF_SIZE*sizeof(char));
if(rec_buffer == NULL){
ESP_LOGE(TAG,"Memory exhaust!");
return;
}
struct sockaddr_in peerAddr;
socklen_t peerLen = sizeof(peerAddr);
while(1){
NetworkConfigSend(dest_ip_address,"Hello",5);
memset(rec_buffer,0,NET_CFG_RECBUFF_SIZE);
if(recSocket>0){
ESP_LOGE(TAG,"Read socket data!");
//bytes_read = read (recSocket, rec_buffer, NET_CFG_RECBUFF_SIZE);
bytes_read = recvfrom(recSocket,rec_buffer,NET_CFG_RECBUFF_SIZE,0,(struct sockaddr *)&peerAddr,&peerLen);
if(bytes_read>0){
ESP_LOGI(TAG,"Receive packet:%s,bytes:%d",rec_buffer,bytes_read);
ESP_LOGI(TAG,"peer address = %s:%d\n", inet_ntoa(peerAddr.sin_addr), ntohs(peerAddr.sin_port));
if(NetCommandProcess(rec_buffer)>0){
ESP_LOGI(TAG,"Command process OK");
}
dest_ip_address = peerAddr.sin_addr.s_addr;
}
if(bytes_read < 0){
ESP_LOGE(TAG,"UDP Receive had error!Reset socket..");
recSocket = 0;
}
else{
ESP_LOGE(TAG,"Rec timeout");
}
}else{
vTaskDelay(2000);
recSocket = CreateUDPClient(NET_CFG_LISTEN_PORT);
ESP_LOGI(TAG,"Create Config listen socket!,socket:%d",recSocket);
if(recSocket<=0){
ESP_LOGE(TAG,"UDP Create faile!");
break;
}
}
}