listening server
Posted: Mon Mar 12, 2018 6:26 pm
I bought Kolban's book and am using his code example (I added more logging after I ran into problems) for a listening server.
D (3740) event: SYSTEM_EVENT_STA_GOT_IP, ip:192.168.0.34, mask:255.255.255.0, gw:192.168.0.1
V (3740) event: enter default callback
I (3750) event: sta ip: 192.168.0.34, mask: 255.255.255.0, gw: 192.168.0.1
V (3750) event: exit default callback
E (3760) >: socket: 4096 Success
E (3760) >: bind: 0 Success
E (3760) >: listen: 0 Success
E (3790) >: accept: -1 Bad file number
I (5510) wifi: pm start, type:0
Thinking it might be my firewall, I tried both 7777 and port 80. used portqry.exe to see if port is open.
C:\Users\Peter>C:\PortQryV2\PortQry.exe -n 192.168.0.34
Querying target system called:
192.168.0.34
Attempting to resolve IP address to a name...
IP address resolved to espressif.domain
querying...
TCP port 80 (http service): NOT LISTENING
Any ideas what I am missing?
Code: Select all
int s, r, sock;
char recv_buf[64];
struct sockaddr_in clientAddress; struct sockaddr_in serverAddress;
Code: Select all
static void startServer(void)
{
/* Wait for the callback to set the CONNECTED_BIT in the
event group.
*/
xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT,
false, true, portMAX_DELAY);
// Create a socket that we will listen upon.
int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock < 0)
{
ESP_LOGE(TAG, "socket: %d %s", sock, strerror(errno));
goto END;
}
ESP_LOGE(TAG, "socket: %d %s", sock, strerror(errno));
// Bind our server socket to a port.
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
serverAddress.sin_port = htons(PORT);
int rc = bind(sock, (struct sockaddr *)&serverAddress, sizeof(serverAddress));
if (rc < 0)
{
ESP_LOGE(TAG, "bind: %d %s", rc, strerror(errno));
goto END;
}
ESP_LOGE(TAG, "bind: %d %s", rc, strerror(errno));
// Flag the socket as listening for new connections.
rc = listen(sock, 5);
if (rc < 0)
{
ESP_LOGE(TAG, "listen: %d %s", rc, strerror(errno));
goto END;
}
ESP_LOGE(TAG, "listen: %d %s", rc, strerror(errno));
ESP_LOGI(TAG, "Listening on port %s...", PORT);
END:
ESP_LOGI(TAG, "there was an error, skipped the rest of the port setup: %s", strerror(errno));
// vTaskDelete(NULL);
}
Code: Select all
static void rxtxpacket_server(void *pvParameters)
{
ESP_LOGI(TAG, "beginning rxtx packet server...");
while (1)
{
// Listen for a new client connection.
socklen_t clientAddressLength = sizeof(clientAddress);
int clientSock = accept(sock, (struct sockaddr *)&clientAddress, &clientAddressLength);
if (clientSock < 0)
{
ESP_LOGE(TAG, "accept: %d %s", clientSock, strerror(errno));
goto END;
}
// We now have a new client ...
int total = 10*1024;
int sizeUsed = 0;
char *data = malloc(total);
ESP_LOGI(TAG, "client accepted %d...", clientSock);
// Loop reading data.
while(1)
{
ssize_t sizeRead = recv(clientSock, data + sizeUsed, total-sizeUsed, 0);
if (sizeRead < 0)
{
ESP_LOGE(TAG, "recv: %d %s", sizeRead, strerror(errno));
goto END;
}
if (sizeRead == 0)
{
break;
}
sizeUsed += sizeRead;
}
// Finished reading data.
ESP_LOGE(TAG, "Data read (size: %d) was: %.*s", sizeUsed, sizeUsed, data);
free(data);
close(clientSock);
}
END:
vTaskDelete(NULL);
}
Code: Select all
void app_main()
{
nvs_flash_init();
initialise_wifi();
vTaskDelay(2000 / portTICK_RATE_MS);
if (beServer == 1)
{
ESP_LOGI(TAG, "Initializing server to listen for connections...");
startServer();
ESP_LOGI(TAG, "trying to start rxtxpacket_server...");
xTaskCreate(&rxtxpacket_server, "rxtxpacket_server", 2048, NULL, 5, NULL);
}
V (3740) event: enter default callback
I (3750) event: sta ip: 192.168.0.34, mask: 255.255.255.0, gw: 192.168.0.1
V (3750) event: exit default callback
E (3760) >: socket: 4096 Success
E (3760) >: bind: 0 Success
E (3760) >: listen: 0 Success
E (3790) >: accept: -1 Bad file number
I (5510) wifi: pm start, type:0
Thinking it might be my firewall, I tried both 7777 and port 80. used portqry.exe to see if port is open.
C:\Users\Peter>C:\PortQryV2\PortQry.exe -n 192.168.0.34
Querying target system called:
192.168.0.34
Attempting to resolve IP address to a name...
IP address resolved to espressif.domain
querying...
TCP port 80 (http service): NOT LISTENING
Any ideas what I am missing?