Slow WiFi on socket echo server

M3te0ra2
Posts: 3
Joined: Fri Apr 05, 2019 11:00 am

Slow WiFi on socket echo server

Postby M3te0ra2 » Fri Apr 05, 2019 11:23 am

Hi everyone,

I'm running into some kind of issue with WiFi. I'm trying to transfer files over a socket connection and although they transfer successfully, they do it at underwhelmingly low speed. For example, it takes between 15 and 25 ms to transmit 1000 bytes, which makes for a speed of 60 ko/s at best. But I intend to transfer huge files and i know higher speeds can be achieved.

What am i doing wrong?

Here's my simplified code:

Code: Select all

/* initialise WiFi & socket*/

static char write_buffer[1000] = "ddddd..." //contains only d
while (1) {

            response_available = 0;

            struct sockaddr_in6 sourceAddr; // Large enough for both IPv4 or IPv6
            uint addrLen = sizeof(sourceAddr);
            int sock = accept(listen_sock, (struct sockaddr *)&sourceAddr, &addrLen);
            if (sock < 0) {
                ESP_LOGE(SOCK_TAG, "Unable to accept connection: errno %d", errno);
                break;
            }
            ESP_LOGI(SOCK_TAG, "Socket accepted");
            int i = 0;

            while (1) {
                int len = recv(sock, rx_buffer, sizeof(rx_buffer) - 1, 0);
                // Error occured during receiving
                if (len < 0) {
                    ESP_LOGE(SOCK_TAG, "recv failed: errno %d", errno);
                    break;
                }
                // Connection closed
                else if (len == 0) {
                    ESP_LOGI(SOCK_TAG, "Connection closed");
                    break;
                }
                // Data received
                else {
                    // Get the sender's ip address as string
                    if (sourceAddr.sin6_family == PF_INET) {
                        inet_ntoa_r(((struct sockaddr_in *)&sourceAddr)->sin_addr.s_addr, addr_str, sizeof(addr_str) - 1);
                    } else if (sourceAddr.sin6_family == PF_INET6) {
                        inet6_ntoa_r(sourceAddr.sin6_addr, addr_str, sizeof(addr_str) - 1);
                        rx_buffer[len] = 0; // Null-terminate whatever we received and treat like a string
                    }

                    ESP_LOGI(SOCK_TAG, "Received %d bytes from %s on socket", len, addr_str);

		    // This sends 200 times 1000 bytes and then stops
                    ESP_LOGI(SOCK_TAG, "Sending %d bytes... (i = %d)", write_len, i);
                    if(i > 200) {
                        send(sock, "{\"data\":\"dddd\"}", 15, 0);
                    } else {
                        send(sock, write_buffer, write_len, 0);
                    }

                }
            }

            close(sock);
        }
The client just sends messages and expect 1000 bytes long responses. If it's shorter, then it closes the connection. I also measure time on this client.

Thank you for your answers

Who is online

Users browsing this forum: Baidu [Spider], Bing [Bot], Google [Bot] and 80 guests