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.

NDK socket creation error

Other Parts Discussed in Thread: EK-TM4C1294XL, CC3100

Hi All, 

Would you please check what could caused socket error for following code ?

=====

sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sockfd == -1) {
Log_info1("Error: socket not created. err=%d", errno);
goto shutdown;
}

=====

log is as follows

=====

#000001 [ 0.000 ] INFO: (../main.c:163) Starting main t=1477615038.
#000002 [ 0.000 ] INFO: (../main.c:191) Starting the BIOS.
#000003 [ 0.002 ] INFO: (../MainTask.c:178) create Main Task t=1477615038
#000004 [ 0.003 ] INFO: (../NetworkTask.c:449) create Network Task IP addr=0xe00a8c0 t=1477615038
#000005 [ 0.003 ] INFO: (../DebugTask.c:359) Debug Task start t=1477615038.
#000006 [ 1.003 ] INFO: (../socket_utils.c:137) Error: socket not created. err=-1

=====

What does it mean errno = -1 ?

My development platform is ek-tm4c1294xl, and NDK version is 2.16.01.14

Thanks in advance.

Best Regards,

Hae Ryong

  • Hello Hae,

    The error code of -1 is for SOCKET_ERROR which means socket creation failure. 

    Also many socket APIs return -1 for failure. You should call fdError() in the fail loop to get the error code which will tell exact error code

    The error code mapping is given in the - ti/ndk/inc/serrno.h file

    Also the snippet you shared is not enough for finding error cause.
    Can you please shared whole program to check if any obvious issue?

  • Hello Prasad, 

    Thanks for your feedback.

    Would you please check following snippet is correct for using fdError() ?

    =================

        sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
        if (sockfd == -1) {
            err = fdError();
        	Log_info1("Error: socket not created. err=%d", err);
            goto shutdown;
        }

    =================

    as I have tested, still I have get err = -1 from the log

    please take a look at the following code 

    =================

    int getNTPTime(void)
    {
        int                sockfd;
        struct sockaddr_in localAddr;
        struct sockaddr_in ntpServerAddr;
        socklen_t          addrlen;
        //int                bytesRcvd;
        int                status;
        uint8_t				dataBuf[MAX_BUF_SIZE];
        uint32_t 			elapsedSec;
        int err;
    
        sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
        if (sockfd == -1) {
        	err = fdError();
        	Log_info1("Error: socket not created. err=%d", err);
            goto shutdown;
        }
    
        memset(dataBuf, 0, sizeof(dataBuf));
        dataBuf[0] = '\x1b';
    
        addrlen = sizeof(struct sockaddr_in);
        ntpServerAddr.sin_family = AF_INET;
        ntpServerAddr.sin_port = 123;
        ntpServerAddr.sin_addr.s_addr = 0x744EE9D3;
    
    	sendto(sockfd, dataBuf, sizeof(dataBuf), 0, (struct sockaddr*)&ntpServerAddr, addrlen);
    
        localAddr.sin_family = AF_INET;
        localAddr.sin_port = 0;
        localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    
        status = bind(sockfd, (struct sockaddr *)&localAddr, addrlen);
        if (status == -1) {
        	Log_info0("Error: bind failed.");
            goto shutdown;
        }
    
        addrlen = sizeof(ntpServerAddr);
        recvfrom(sockfd, dataBuf, MAX_BUF_SIZE, 0, (struct sockaddr*)&ntpServerAddr, &addrlen);
        if((dataBuf[0] & 0x7) != 4)    /* expect only server response */
        {
            /* MODE is not server, abort */
        	Log_info0(" Device is expecting response from server only!");
        	goto shutdown;
        }
        else
        {
            elapsedSec = dataBuf[40];
            elapsedSec <<= 8;
            elapsedSec += dataBuf[41];
            elapsedSec <<= 8;
            elapsedSec += dataBuf[42];
            elapsedSec <<= 8;
            elapsedSec += dataBuf[43];
    
            elapsedSec -= TIME2013;
    
            /* correct the time zone */
            elapsedSec += (GMT_TIME_ZONE_HR * SEC_IN_HOUR);
            elapsedSec += (GMT_TIME_ZONE_MIN * SEC_IN_MIN);
    
            Log_info1("NTP elapsedSec = %d", elapsedSec);
        }
    
        close(sockfd);
        return 0;
    
    shutdown:
    	if (sockfd > 0) {
    		close(sockfd);
    	}
    	return 1;
    }
    ===============

    This code is modified from get_time example of CC3100_SDK.

    Thanks for your feedback.

    Best Regards,

    Hae Ryong

  • Hello Hae,

    The error checking looks correct. One thing missing from your application is fdOpenSession. Can you please add that and try?

    Add below at the beginning of function

    // Allocate the file environment for this task
    fdOpenSession( TaskSelf() );

    Also you can refer to below for UDP application reference.

    http://e2e.ti.com/cfs-file.ashx/__key/communityserver-discussions-components-files/355/3857.udpSendRecv.c

    http://e2e.ti.com/cfs-file.ashx/__key/CommunityServer-Discussions-Components-Files/355/7587.temp.zip

  • yes, it works.

    Thanks for your prompt help