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.

TMDXICE110: NDK questions

Part Number: TMDXICE110

Hello.

I try to understand some code from dtask_tcp_datasrv of newservers.c file.

The first point, why setsockopt is called three times in a row? Every time socket s is called, is socket redefined again or it not? Why is this done?

Where can I see code of setsockopt function? Is it possible? Myself, I could not find it in the folders of SDK.

/* Configure our socket timeout to be 5 seconds */
to.tv_sec = 5;
to.tv_usec = 0;
setsockopt( s, SOL_SOCKET, SO_SNDTIMEO, &to, sizeof( to ) );
setsockopt( s, SOL_SOCKET, SO_RCVTIMEO, &to, sizeof( to ) );

i = 1;
setsockopt( s, IPPROTO_TCP, NDK_TCP_NOPUSH, &i, 4 );

The second point, after the socket is configured with the setsockopt function, a call is made function recv.

/* receive request for amount of data to send from client */
i = (int)recv( s, (char *)&size, sizeof(int), 0 );

I'm not so clear understand what does it do.According to NDK API Guide, function is return Value If it succeeds, the function returns the number of bytes received. But I can not understand, where to the function returns the amount of data? Into the "i" variable, or not?

It seems to me in "i" variable and the operational code of the function is returned. Like is describe in NDK API Guide:

  • EBADF: The file descriptor (socket) is invalid.
  • EINVAL: Attempt to read (or calling arguments) invalid for this socket.
  • ENOTCONN: The socket is connection oriented and not connected
  • ENOTSOCK: The file descriptor does not reference a socket.
  • ETIMEDOUT: The socket connection was dropped due to protocol layer timeout.
  • EWOULDBLOCK: The socket is specified as non-blocking, or the timeout has expired.

Where can I see the hexadecimal code EBADF, EINVAL, etc.

For example, my device, when debugging, returns the number 4 in the code when breakpoint is occore. What does 4 is mean?

  • Hi Kiselev,

    Would you mind tell me what Processor SDK are you using (Linux or RTOS) and the version number?

    Ming

  • Hi, Ming.

    I use ti-processor-sdk-rtos-am335x-evm-06.01.00.08 and RTOS bios_6_76_02_02.

    For the first point I'm found answer. Why setsockopt called repeatedly.

    Description The NDK_setsockopt() function sets option values associated with a socket. Options may exist at multiple protocol levels; they are always present at the uppermost socket level.

    When manipulating socket options, the level at which the option resides and the name of the option must be specified.

    Thus, each new call configures a certain level.

    Regarding the second point, I seem to understand why the number four returns. Because the variable 'size' is passed as a buffer. And the variable "size" is defined at the beginning of the procedure as integer. I don't quite understand why this is done?

    If I transfer the real buffer instead of the "size" variable, then the real number of bytes transferred is returned to the variable "i" and the array is filled with data.

    Now I'm trying to figure out what the following code does at the end of dtask_tcp_datasrv.

    /* divide total size of data to send into 512 byte chunks */
    count = size / DATASRV_BUFSIZE;
    remainder = size % DATASRV_BUFSIZE;

    /* divide total size of data to send into 512 byte chunks */
    count = size / DATASRV_BUFSIZE;
    remainder = size % DATASRV_BUFSIZE;

    if( i==sizeof(int) )
    {
    if (allocFailed) {
    /* couldn't get a buffer, just send value we rec'vd back */
    if ((send(s, (char *)&size, sizeof(int), 0) < 0)) {
    DbgPrintf(DBG_INFO,
    "dtask_tcp_datasrv: send returned < 0!\n");
    break;
    }
    }
    else {
    /* allocation succeeded: send DATASRV_BUFSIZE * count bytes */
    sent = 0;
    for (i = 0; i < count; i++) {
    /* send DATASRV_BUFSIZE num bytes until we get close to size */
    if ((sent = send( s, &bufer_input_data, DATASRV_BUFSIZE, 0)) < 0 ) {
    DbgPrintf(DBG_INFO,
    "dtask_tcp_datasrv: send returned < 0!\n");
    break;
    }

    totalSent += sent;
    }

    /* send the remainder (to get total sent up to 'size' */
    if (remainder > 0) {
    totalSent += send(s, (char *)&size, remainder, 0);
    }
    }
    }
    else {
    DbgPrintf(DBG_INFO, "dtask_tcp_datasrv: received invalid size\n");
    break;
    }
    }

    if (data) {
    mmFree(data);
    }

    /* Note in dtask_tcp_echo() we close the socket at this */
    /* point. Here we'll leave it open to test the daemon. */

    /* Return "1" since the socket is still open */
    return(1);

    Maybe you can help me understand?

  • Hi Kiselev,

    The dtask_tcp_datasrv is a TCP data server  daemon which keep receiving the size of the data needs to send out and then sending a packet with the required size (repeat the data which is 512 bytes canned data) out endlessly (notice the for loop). Since it is a daemon, it will run forever, unless something bad happens, like sending error.

    In the normal case, the mmFree and the return(1) should not be executed at all. In case it happens, since we do not close the socket in this function, so we should return (1).

    Ming