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.

socket() returns -1 and errno is -1

Hi TI:

I recently imported the UDP example and was able to get it running with a UDP server.

Per the example, the config file contained:

    Global.networkOpenHook = "&netOpenHook";

And I added the following:

    Global.networkIPAddrHook = '&mynetworkIPAddrHook';

As I understand it, netOpenHook() dynamically configured Task echoFxn().  Task echoFxn() handled UDP I/O.    

I included mynetworkIPAddrHook() to show me when the IP was assigned from the router and set an IP Ready Flag.

Changes.. 

I moved the dynamic allocation of Task echoFxn() to main() from netOpenHook();

netOpenHook() is essentially a shell with a small entry/exit message.

I used an "IP Ready Flag" in mynetworkIPAddrHook() to 'hold' in echoFxn() until the IP was ready (IP Ready Flag).

Results...

The echoFxn() Task holds at the top of the echoFxn() Task until the IP Ready Flag is set.

Once the IP Ready Flag is set, the echoFxn() Task continues with a call to socket().

     The return value of the socket() call is -1.

    When inspecting the errno, errno is also -1.  This was very unexpected.   I did not think errno could be -1.

Did I do something illegal by dynamically allocating Tasks echoFxn in main()?

Is the IP stack fully setup when I call socket() in echoFxn?

Other?

Any thoughts would be greatly appreciated.  

Thanks

Rick

  • Hello Rick,

    Is this the TI RTOS UDP example that you are using?

    Regards
    Amit
  • Hi Amit:

    Yes it is. Certainly, with some minor modifications.

    It might help you to know that I'm using a TM4C129 launchpad.

    Thanks

    Rick
  • Hello Rick

    Moving it to the TI RTOS forum.

    Regards
    Amit
  • Hi Rick,
    I was able to reproduce the same failure you are seeing when the task is created in main(). I'm looking into it and hopefully will have an explanation tomorrow.
    Best regards,
    Janet
  • Hi Rick,

    I got this to work with a couple of changes:

    1. In your .cfg file change Global.autoOpenCloseFD from true to false:

    Global.autoOpenCloseFD = false;

    2.  In your echo task, you need to call fdOpenSession(Task_self()) after the flag has been set, and fdCloseSession(Task_self()) before exiting:

    /*
     *  ======== echoFxn ========
     *  Echoes UDP messages.
     *
     */
    Void echoFxn(UArg arg0, UArg arg1)
    {
        int                bytesRcvd;
        int                bytesSent;
        int                status;
        int                server;
        fd_set             readSet;
        struct sockaddr_in localAddr;
        struct sockaddr_in clientAddr;
        socklen_t          addrlen;
        char               buffer[UDPPACKETSIZE];

        while (!netOpenFlag) {
        }
        fdOpenSession(Task_self());

        server = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
             ...

    shutdown:
        fdCloseSession(Task_self());
        if (server > 0) {
            close(server);
        }
    }

    The problem was that with autoOpenCloseFD, fdOpenSession() is called when the task is created, but only if the task is created in task context.  Since the task is being created in main(), fdOpenSession() is not called.  There is a check in the NDK function ti_ndk_config_global_taskCreateHook()  to only call fdOpenSession() in Task context.

    Best regards,

        Janet

  • Hi Janet:

    Thank you for the timely feedback and answer.

    I will go back through the documentation to see what I missed.

    Again, thank you.

    Rick