AnsweredAssumed Answered

About LwIP: Socket communication with Localhost (127.0.0.1)

Question asked by Kappa.Hari on Jan 13, 2017
Latest reply on Jan 13, 2017 by Kappa.Hari

Hello All,

 

I am working on LwIP application with STM32F429 Nucleo Kit.

I am able to establish the connection and communicate with external client/server without any issues.

But now, I need it to be tested with socket communication within the same board using the same system IP or Localhost using IP address127.0.0.1. I tried with different options (1. starting both threads at same time, 2. starting client thread after kicking the server thread, as shown below) but no success.

 

Now, I got a fundamental doubt that, is LwIP with FreeRTOS can support with this feature or not. If yes, can someone suggest how to make it work please? A quick response is highly appreciated.

 

Below is the code snippet I am using for my testing:

Both of these are stuck at bold highlighted lines and timed out after 20seconds. Further debugging through the source code is failed at taking the semaphore at:

     TCPIP_APIMSG(&API_MSG_VAR_REF(msg), lwip_netconn_do_connect, err);

in function

     netconn_connect(struct netconn *conn, const ip_addr_t *addr, u16_t port)

 

Thank you!

Hari

 

------------------------------

#define SENDER_PORT_NUM 1000

#define SERVER_PORT_NUM 1000
#define SERVER_IP_ADDRESS "127.0.0.1"

char data_buffer1[80];
char data_buffer2[80];

 

void OSAL_ServerSocket(void *pvParameters)
{
    int _socket_fd;
    struct sockaddr_in client;
    int clientFd;
    socklen_t len = sizeof(client);
    BaseType_t taskStatus;
   
    vTaskDelay(1000);
    printSerial (PRN_CYAN "Server Socket - " PRN_NORMAL "Enter\n\r");
    _socket_fd = lwip_socket(PF_INET, SOCK_STREAM, 0);
    if(_socket_fd >= 0)
    {
        printSerial (PRN_CYAN "Server Socket - " PRN_NORMAL "Server Socket created\n\r");
        struct sockaddr_in myaddr;
        memset((char*)&myaddr, 0, sizeof(myaddr));
        memset(&myaddr, 0, sizeof(struct sockaddr_in));
        myaddr.sin_family = AF_INET;
        myaddr.sin_port = htons(SENDER_PORT_NUM);
        myaddr.sin_addr.s_addr = inet_addr(SERVER_IP_ADDRESS);//htonl(INADDR_ANY);

        //Now bind to the port and listen
        if((lwip_bind(_socket_fd, (struct sockaddr*)&myaddr, sizeof(struct sockaddr)) < 0) ||
            (lwip_listen(_socket_fd, 5) < 0))
        {
            printSerial(PRN_CYAN "Server Socket - " PRN_RED "Error: " PRN_NORMAL "Failed to Bind\n\r");
            lwip_close(_socket_fd);
        }
        else
        {
            printSerial(PRN_CYAN "Server Socket - " PRN_NORMAL "Bind and listening to port (%d)\n\r", SENDER_PORT_NUM);
        }
    }
   
    taskStatus = xTaskCreate (OSAL_ClientSocket, "OSAL LWIP Client", configMINIMAL_STACK_SIZE*2, (void*)NULL, osPriorityNormal+1, NULL);
    if (pdPASS == taskStatus) {    printSerial(PRN_NORMAL "[" PRN_GREEN " ok " PRN_NORMAL "] Task - 'OSAL LWIP Client' created\n\r");  }
    else                      {    printSerial(PRN_NORMAL "[" PRN_RED   "fail" PRN_NORMAL "] Task - 'OSAL LWIP Client' created\n\r");  }

    while(1)
    {
        printSerial(PRN_CYAN "Server Socket - " PRN_NORMAL "Before accept\n\r");
        clientFd = lwip_accept(_socket_fd, (struct sockaddr*)&client, &len); /////////////////////////////////
        printSerial(PRN_CYAN "Server Socket - " PRN_NORMAL "After accept\n\r");
        if(clientFd < 0)
        {
            printSerial(PRN_CYAN "Server Socket - " PRN_RED "Error: " PRN_NORMAL "accept failed\n");
            lwip_close(_socket_fd);
        }
        else
        {
            printSerial(PRN_CYAN "Server Socket - " PRN_NORMAL "accepted the request\n\r");
            break;
        }
    }

    while(1)
    {
        printSerial(PRN_CYAN "Server Socket - " PRN_NORMAL "Waiting at Server Loop\r");
        vTaskDelay(1000);
    }
}

void OSAL_ClientSocket(void *pvParameters)
{
    printSerial(PRN_MAGENTA "Client Socket - " PRN_NORMAL "Enter\n\r");
    vTaskDelay(2000);
    struct sockaddr_in server;

    printSerial(PRN_MAGENTA "Client Socket - " PRN_NORMAL "Before Create\n\r");
    int sock_client_fd = lwip_socket(PF_INET, SOCK_STREAM, 0);
    if(sock_client_fd < 0)
    {
        printSerial(PRN_MAGENTA "Client Socket - " PRN_RED "Error: " PRN_NORMAL "SocketCreation\n\r");
    }
    {
        printSerial(PRN_MAGENTA "Client Socket - " PRN_NORMAL "Created\n\r");
    }

    memset((char*)&server, 0, sizeof(server));
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = inet_addr(SERVER_IP_ADDRESS);
    if (server.sin_addr.s_addr == INADDR_NONE)
    {
        //Couldnt translate the ip.. return failure
        printSerial(PRN_MAGENTA "Client Socket - " PRN_RED "Error: " PRN_NORMAL "IP Translation: ");
    }
    server.sin_port = htons(SERVER_PORT_NUM);
    printSerial(PRN_MAGENTA "Client Socket - " PRN_NORMAL "Before Connect\n\r");
    if(lwip_connect(sock_client_fd, (struct sockaddr*)&server, sizeof(server)) < 0) ////////////////////////////////////////////
    {
        printSerial(PRN_MAGENTA "Client Socket - " PRN_RED "Error: " PRN_NORMAL "Connect\n\r");
    }
    else
    {
        printSerial(PRN_MAGENTA "Client Socket - " PRN_NORMAL "Connected\n\r");
        while(1)
        {
            printSerial(PRN_MAGENTA "Client Socket - " PRN_NORMAL "Waiting at Client Loop\n\r");
            vTaskDelay(1000);
        }
    }
   
    while(1)
    {
        vTaskDelay(100);
    }
}

Outcomes