One of the things my app does is to serve image files to other nodes. After a network reconnect, the server code often crashes on the return statement just after bind.
Messages I typically see then are :
Can anyone help ?E (193533) ImageTaskLoop: ImageTaskLoop: Can't bind socket to address, errno 112 Address already in use, ServerSocket 63
uru Meditation Error: Core 1 panic'ed (IllegalInstruction). Exception was unhandled.
Core 1 register dump:
PC : 0x400d5d80 PS : 0x00060e30 A0 : 0x00000000 A1 : 0x3ffcaf10
Thanks,
Danny
Code: Select all
/*
* Background task to serve the image transfer
* As this can't be a class method, the variables are global.
*/
void ImageTaskLoop(void *ptr) {
const char *itl_tag = "ImageTaskLoop";
if (weather && weather->isWeatherNode()) {
const int listen_queue = 5; // Number of connections allowed
// Start server
struct sockaddr_in ServerAddr;
ServerAddr.sin_addr.s_addr = htonl(INADDR_ANY);
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons(portImage);
ServerSocket = socket(AF_INET, SOCK_STREAM, 0);
if (ServerSocket < 0) {
ESP_LOGE(itl_tag, "Can't create socket");
return;
}
ESP_LOGI(itl_tag, "Socket created, %d", ServerSocket);
if (bind(ServerSocket, (struct sockaddr *)&ServerAddr, sizeof(ServerAddr)) != 0) {
ESP_LOGE(itl_tag, "ImageTaskLoop: Can't bind socket to address, errno %d %s, ServerSocket %d", errno, strerror(errno), ServerSocket);
close(ServerSocket);
ServerSocket = -1;
return; // Crash here
}
if (listen(ServerSocket, listen_queue) != 0) {
ESP_LOGE(itl_tag, "Listen failed, errno %d %s", errno, strerror(errno));
close(ServerSocket);
ServerSocket = -1;
return;
}
fcntl(ServerSocket, F_SETFL, O_NONBLOCK);
}
ESP_LOGD("ImageTaskLoop", "Start");
while (1) {
// Task 1 : serve an icon image, only in the weather node
struct sockaddr_in RemoteAddr;
socklen_t RAlen = sizeof (RemoteAddr);
int ClientSocket = accept(ServerSocket, (struct sockaddr *)&RemoteAddr, &RAlen);
// deleted
}
ESP_LOGE("Peers static", "ImageTaskLoop exiting (after while) !");
}
void Peers::ImageServerStopTask() {
ESP_LOGI(peer_tag, "ImageServerStopTask()");
if (ServerSocket >= 0) {
ESP_LOGI(peer_tag, "Closing ServerSocket %d", ServerSocket);
close(ServerSocket);
}
ServerSocket = -1;
if (imageTask)
vTaskDelete(imageTask);
imageTask = 0;
ESP_LOGI(peer_tag, "ImageServerStopTask end");
}