sponsored links



No announcement yet.

TCP Socket - Non-Blocking Receive ???

  • Filter
  • Time
  • Show
Clear All
new posts

  • TCP Socket - Non-Blocking Receive ???

    I have a Client TCP Socket application that makes a connection to a server.
    The application is just connecting to the server port, sends some data and following the data, the server returns some result.
    The Client application is using blocking socket and it's working great.
    Unfortunately, sometimes, the TCP connection is lost and thus the Client application stays "frozen" on the recv call.

    So, my idea was to set to non-blocking the recv call.
    Now, the result received should be 128 characters or finished by a specific character.

    c dou reclen = 128 or ch = x'03' or ch = x'08'
    c eval rc = recv(sock: %addr(ch): 1: 0)

    Is the non-blocking solution on the recv the good solution to have an error recovery and to recreate the socket ?

    If yes, I'm a little bit lost how to imlement it. I went through Scott document and his examples... but I don't understand it very well.
    I read the following: "Non-blocking sockets can also be used in conjunction with the select() API. In fact, if you reach a point where you actually WANT to wait for data on a socket that was previously marked as "non-blocking", you could simulate a blocking recv() just by calling select() first, followed by recv()."

    So should I call in the following order:
    - the fcntl() API to set the socket in non-blocking
    - the select() API to wait for data
    - the recv() to receive the data
    - the fcntl() API to reset the socket in blocking mode ?

    What I don't understand is should I include the select in the DOU loop ?

    Do you have a small and easy example of just a client program in non-blocking mode that send and recv a data ?

    Thanks in advance for your support.

    Best regards,


  • #2
    Which document is "Scott document"? Do you mean the two documents here? https://www.scottklement.com/rpg/socktimeout/

    The second link on that page ("Timing Out Sockets") has an example of using recv() with a non-blocking socket.

    As for your question ". . . good solution to have an error recovery and recreate the socket?" -- this entirely depends on your application and how it works. Some applications are designed with a recovery mechanism involving reconnecting, etc, and some are not. You have to know your application...


    • #3
      Hello Scott,

      No, it wasn't those 2 documents, I was refering to your documentation about TCP Socket Programming.

      But those 2 documents are really what I was looking for... You are amazing !!!

      Thanks very much one more time for your support !!!

      Best regards,