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.

RTOS/DK-TM4C129X: TCP server socket not working using NDK when web server task is created static

Part Number: DK-TM4C129X

Tool/software: TI-RTOS

Hi,

I am using the DK-TM4C129 and TI RTOS and NDK for web server app. I started out using tcpecho and I was able to make it work.

I modified the tcpecho code as shown below and I was able to make it work with the web server task created dynamically. However, when I tried to do it static using a semaphore_post on the hook and semaphore_pend on the web_server_task, I keep on getting an error. The socket function returns a -1.

If you continue to scroll down, I also added the code where task is static. I traced the program. The hook gets called and then the web_server_task runs the rest of the code after pend. I don't understand why the socket keeps returning a -1 error. By the way, here, web_server_task was created using config file. If I delete the task in the config file and switch back to dynamic creation of the task, the code works fine. Any help would be very much appreciated
Thanks
Aj
void enet_open_hook(void)
{
    Task_Handle taskHandle;
    Task_Params taskParams;
    Error_Block eb;
    /* Make sure Error_Block is initialized */
    Error_init(&eb);
    /*
     *  Create the Task that farms out incoming TCP connections.
     *  arg0 will be the port that this task listens to.
     */
    Task_Params_init(&taskParams);
    taskParams.stackSize = 1024;
    taskParams.priority = 1;
    //taskParams.arg0 = TCPPORT;
    taskHandle = Task_create((Task_FuncPtr)web_server_task, &taskParams, &eb);
    if (taskHandle == NULL) {
        System_printf("netOpenHook: Failed to create tcpHandler Task\n");
    }
    System_flush();
}//end void enet_open_hook(void)
/***********************************************/
void web_server_task(void)
{
    uint8_t val_array[10];
    int  bytesRcvd;
    int  bytesSent;
    char buffer[256];
    int                server;
    int                clientfd;
    int                status;
    int                optval;
    int                optlen = sizeof(optval);
    struct sockaddr_in localAddr;
    struct sockaddr_in clientAddr;
    socklen_t          addrlen = sizeof(clientAddr);

    write_UART("\n\n\rRunning web server task...");
    server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    write_UART("\n\rServer socket = ");
    long_to_ascii(server, val_array);
    write_UART(val_array);
    if(server <= 0)
    {
        write_UART("\n\rMaster socket failed");
    }
    memset(&localAddr, 0, sizeof(localAddr));
    localAddr.sin_family = AF_INET;
    localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    localAddr.sin_port = htons(80);
    status = bind(server, (struct sockaddr *)&localAddr, sizeof(localAddr));
    if (status < 0)
    {
        write_UART("\n\rError bind");
    }
    status = listen(server, 3);
    if (status < 0)
    {
        write_UART("\n\rError listen");
    }
    optval = 1;
    if (setsockopt(server, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen) < 0)
    {
        write_UART("\n\rError setsockopt");
    }

    while ((clientfd = accept(server, (struct sockaddr *)&clientAddr, &addrlen)) != -1)
    {
        write_UART("\n\rClientfd socket = ");
        long_to_ascii(clientfd, val_array);
        write_UART(val_array);
        while ((bytesRcvd = recv(clientfd, buffer, 256, 0)) > 0)
        {
            bytesSent = send(clientfd, buffer, bytesRcvd, 0);
            if (bytesSent < 0 || bytesSent != bytesRcvd)
            {
                write_UART("\n\rError byte send/rcv");
            }
        }
    }//end while(1)
}//end void web_server_task(void)
void enet_open_hook(void)
{
   
      Sempahore_post(server_sem);
}//end void enet_open_hook(void)
/***********************************************/
void web_server_task(void)
{
    uint8_t val_array[10];
    int  bytesRcvd;
    int  bytesSent;
    char buffer[256];
    int                server;
    int                clientfd;
    int                status;
    int                optval;
    int                optlen = sizeof(optval);
    struct sockaddr_in localAddr;
    struct sockaddr_in clientAddr;
    socklen_t          addrlen = sizeof(clientAddr);
    Semaphore_pend(server_sem, BIOS_WAIT_FOREVER);
    write_UART("\n\n\rRunning web server task...");
    server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    write_UART("\n\rServer socket = ");
    long_to_ascii(server, val_array);
    write_UART(val_array);
    if(server <= 0)
    {
        write_UART("\n\rMaster socket failed");
    }
    memset(&localAddr, 0, sizeof(localAddr));
    localAddr.sin_family = AF_INET;
    localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    localAddr.sin_port = htons(80);
    status = bind(server, (struct sockaddr *)&localAddr, sizeof(localAddr));
    if (status < 0)
    {
        write_UART("\n\rError bind");
    }
    status = listen(server, 3);
    if (status < 0)
    {
        write_UART("\n\rError listen");
    }
    optval = 1;
    if (setsockopt(server, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen) < 0)
    {
        write_UART("\n\rError setsockopt");
    }

    while ((clientfd = accept(server, (struct sockaddr *)&clientAddr, &addrlen)) != -1)
    {
        write_UART("\n\rClientfd socket = ");
        long_to_ascii(clientfd, val_array);
        write_UART(val_array);
        while ((bytesRcvd = recv(clientfd, buffer, 256, 0)) > 0)
        {
            bytesSent = send(clientfd, buffer, bytesRcvd, 0);
            if (bytesSent < 0 || bytesSent != bytesRcvd)
            {
                write_UART("\n\rError byte send/rcv");
            }
        }
    }//end while(1)
}//end void web_server_task(void)
  • Below is the snippet of your code. You wrote Sempahore_post() instead of Semaphore_post(). Try to fix this first and see if this fixes the problem.

    void enet_open_hook(void)
    {

    Sempahore_post(server_sem);
    }//end void enet_open_hook(void)
  • Hi Charles,

    Thanks for the response. I made the correction and I'm still experiencing the problem..

    By the way, on a might not exactly be related note, I printed out the socket number while I was running the tcpecho sample program,. Is it normal to get a socket number (socket function return value) that is huge... like 537048868?

    I've done socket programming in other microcontrollers like the Synergy S7 and usually the socket function returns a value between 32 to 64. I believe the same goes when doing this on windows(not 100% sure abt this). Any thoughts on this?

    Thanks

    AJ 

  • I finally figured this out. I had to add

    fdOpenSession(Task_self()) after Semaphore_pend and just before I run the bsd code.

    Why is this the case? I don't understand it. Also, I'm still boggled by the huge socket id returned by socket function.

    AJ