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.

TCP send() on server socket

Other Parts Discussed in Thread: TM4C1294NCPDT

Hi,

TM4C1294NCPDT running the TCPEcho example from TiRTOS examples. Runs nicely and is easy to understand.

I'm new to threads and semaphores etc. How do I use the send() from elsewhere in my code?

The recv() blocks the thread. I can set this to unblocking, but then the send() may get called with length 0, and what makes the thread wait?

It must be a common problem, can you point me at an example please.

Thanks

Richard

  • Hello Richard,

    As far as I remember, both the TCP "send()" and "recv()" functions block a thread. Normally in a server implementation, you receive something from the client, process it and then send data back to the client. So you could use the same thread to "recv" and "send".

    From the TCP Echo example, you could modify the "tcpWorker" task such that after recv() is executed, processing of the data is done in the same task and then send() is called. Alternatively the processing of the data could be done by another task, in which case the "tcpWorker" task will wait after recv() is executed (with a semaphore), for the processing of the other task to complete (which would be indicated by releasing the semaphore). The "tcpWorker" would then call send().

    Does you server need to send() irrespective to recv()? If so, could you explain the flow?

    Alternatively a different tasks could be used to "send" and "recv", but this would require inter task communication (to share the same socket and other resources along with when to send and when to recv), which could be complicated.

    Thanks,
    Sai
  • Richard Bland said:
    The recv() blocks the thread. I can set this to unblocking, but then the send() may get called with length 0, and what makes the thread wait?

    Using the BSD socket API under Unix, it is possible to have a single thread perform IO on multiple sockets by setting the sockets to non-blocking and then using the select() or poll() for one or more sockets to be readable (or read error pending) or  writable (or send error pending).

    Unlike Unix TI-RTOS doesn't have built-in support for file descriptors. However, the TI Network Developer's Kit does have a File Descriptor Environment and BSD Sockets Compatibility API Layer to give the equivalent of the select() and poll() functions. See the Sockets and Stream IO API chapter of www.ti.com/lit/pdf/spru524‎

  • Thank you for your answers both of you.

    Through further searches I found the fdShare(SOCKET) function which enables me to use the same socket (file descriptor) from two separate threads. So I create a receive thread (as in the TCPEcho example) and a transmit thread which has fdShare(SOCKET) and a semaphore to tell it when something is ready to transmit.

    This post was the most helpful e2e.ti.com/.../537329

    Best regards
    Richard