This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

MCU-PLUS-SDK-AM243X: Re-open socket causes bind to fail with EADDRINUSE

Part Number: MCU-PLUS-SDK-AM243X

When using the LWIP posix-compatible socket API if I close the socket and then re-open a new socket and attempt to bind it to the same port, bind will fail with error EADDRINUSE(98). Is there any way to make sure to release the address/port when closing a socket?

This does not seem to happen with the netconn API. I am able to repeatedly close and open a netconn connection and bind it to the same port.

I have attached sample code adapted from one of the lwip examples. I am using MCU+SDK AM243x 8.6.0.45

/*
 * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 * OF SUCH DAMAGE.
 *
 * This file is part of the lwIP TCP/IP stack.
 *
 * Author: Adam Dunkels <adam@sics.se>
 *
 */


/* ========================================================================== */
/*                             Include Files                                  */
/* ========================================================================== */
#include <string.h>
#include <stdio.h>
/*
#include "lwip/opt.h"
#include "lwip/api.h"
*/
#include "lwip/sys.h"
#include <kernel/dpl/ClockP.h>
#include "enet_apputils.h"
#include <sys/socket.h>

// These are defined here because netinet/in.h does not exist in freertos
#define SOMAXCONN       128
#define IPPROTO_IPV4    4
#include <errno.h>
#include <netdb.h>


/* ========================================================================== */
/*                           Macros & Typedefs                                */
/* ========================================================================== */

#define MAX_MESSAGE_SIZE (256)
#define APP_SERVER_PORT  (8889)

static const uint8_t APP_CLIENT_TX_MSG1[] = "Greetings from Texas Instruments!";

/* ========================================================================== */
/*                         Structure Declarations                             */
/* ========================================================================== */

/* ========================================================================== */
/*                          Function Declarations                             */
/* ========================================================================== */

static void AppTcp_ServerTask(void *arg);

/* ========================================================================== */
/*                            Global Variables                                */
/* ========================================================================== */

/* ========================================================================== */
/*                          Function Definitions                              */
/* ========================================================================== */

static void AppTcp_ServerTask(void *arg)
{
    struct netconn *pConn = NULL, *pClientConn = NULL;
    err_t err;
    LWIP_UNUSED_ARG(arg);

    int server_fd, new_socket, ret, i, size;
    struct sockaddr_in address;
    int addrlen = sizeof(address);
    static char buffer[MAX_MESSAGE_SIZE] = {0};

    while (1)
    {
        server_fd = socket(AF_INET, SOCK_STREAM, 0);

        LWIP_ERROR("tcpecho: invalid socket\r\n", (server_fd >= 0), return);

        address.sin_family = AF_INET;
        address.sin_addr.s_addr = INADDR_ANY;
        address.sin_port = htons(APP_SERVER_PORT);

        /* This fails on the second iteration */
        ret = bind(server_fd, (struct sockaddr *)&address, sizeof(address));

        LWIP_ERROR("tcpecho: bind failed\r\n", (ret >= 0), return);

        /* Tell connection to go into listening mode. */
        listen(server_fd, 3);

        for (i = 0; i < 2; i++)
        {
            /* Grab new connection. */
            new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen);

            LWIP_ERROR("tcpecho: accept failed\r\n", (new_socket >= 0), return);

            printf("accepted new connection %d\r\n", new_socket);

            ret = recv(new_socket, buffer, MAX_MESSAGE_SIZE, 0);

            LWIP_ERROR("tcpecho: recv failed\r\n", (ret >= 0), return);
            size = ret;

            ret = send(new_socket, buffer, size, 0);
            LWIP_ERROR("tcpecho: send failed\r\n", (ret >= 0), return);

            /* Close connection and discard connection identifier. */
            close(new_socket);
        }

        close(server_fd);
    }
}

void AppTcp_startServer_socket()
{
    sys_thread_new("AppTcp_ServerTask_socket", AppTcp_ServerTask, NULL, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO);
}
/*-----------------------------------------------------------------------------------*/