Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESP8266 SOCK UDP 无法设置本地端口号 (GIT8266O-412) #841

Open
wivin001 opened this issue Mar 3, 2020 · 0 comments
Open

ESP8266 SOCK UDP 无法设置本地端口号 (GIT8266O-412) #841

wivin001 opened this issue Mar 3, 2020 · 0 comments

Comments

@wivin001
Copy link

wivin001 commented Mar 3, 2020

INSTRUCTIONS

----------------------------- Delete above -----------------------------

Environment

  • Development Kit:
  • IDF version (git rev-parse --short HEAD to get the commit id.): 4c38ff3
  • Development Env: Make
  • Operating System: Ubuntu18.0
  • Power Supply: USB

Problem Description

代码来自example下面的udp_client.c的代码
while (1) {

        int err = sendto(sock, payload, strlen(payload), 0, (struct sockaddr *)&destAddr, sizeof(destAddr));
        if (err < 0) {
            ESP_LOGE(TAG, "Error occured during sending: errno %d", errno);
            break;
        }
        ESP_LOGI(TAG, "Message sent");

        struct sockaddr_in sourceAddr; // Large enough for both IPv4 or IPv6
        socklen_t socklen = sizeof(sourceAddr);
        sourceAddr.sin_addr.s_addr = inet_addr("255.255.255.255");
        sourceAddr.sin_family = AF_INET;
        sourceAddr.sin_port = htonl(5560);//这边设置为本地端口,但是并没有用
       // int32_t res = bind(sock,(struct sockaddr *)&sourceAddr,sizeof(sourceAddr));
        // if(res != 0){
        //     printf("bind error\n");
        // }
        int len = recvfrom(sock, rx_buffer, sizeof(rx_buffer) - 1, 0, (struct sockaddr *)&sourceAddr, &socklen);

        // Error occured during receiving
        if (len < 0) {
            ESP_LOGE(TAG, "recvfrom failed: errno %d", errno);
            break;
        }
        // Data received
        else {
            rx_buffer[len] = 0; // Null-terminate whatever we received and treat like a string
            ESP_LOGI(TAG, "Received %d bytes from %s:", len, addr_str);
            ESP_LOGI(TAG, "%s", rx_buffer);
        }

        vTaskDelay(50 / portTICK_PERIOD_MS);
    }
@github-actions github-actions bot changed the title ESP8266 SOCK UDP 无法设置本地端口号 ESP8266 SOCK UDP 无法设置本地端口号 (GIT8266O-412) Mar 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant