Problem in receiving data with non blocking socket RRS feed

  • Question

  • Dear all,


    I am writing a ethernet application in which I have to receive data from a non blocking socket.

    The receive of information is proper with blocking socket.

    But I have to use non blocking socket to receive the data.

    When I will create the non blocking socket after adding some debug prints I am able to get proper data.

    Some timing delay is needed with non blocking socket?

    Can I use any other function in place of recv?



    With Thanks





    Tuesday, April 5, 2011 4:35 AM

All replies

  • Questions:

    Why do you have to use a non-blocking socket?

    What does the code look like that doesn't behave as expected?

    Do you know what "non-blocking" means?

    It sounds to me like you have something like:

    int count = recv( socket, buf, bytecount, 0 )

    and then you just continue with the processing.  It should be obvious, if you know what a non-blocking socket is that, if the data has not arrived when you make the recv() call, you get no data and the call returns immediately (returning zero, of course).  That's the definition of non-blocking: the call does not wait (block) if the function cannot complete; it simply returns immediately.

    Paul T.

    Tuesday, April 5, 2011 2:52 PM
  • Dear Paul sir,


    First sorry for my late reply.

    Thank you for your reply.


    Yes I understood the meaning of non blocking socket.

    Ans for your Questions.


    1)Because I want to wait server continuosly for the data from client.

    2)Now the code is behaving as expected for a non blocking socket.



    Sir,I am writing a client server application using ethernet interface.

    Server will wait for a perticular command from client and it responces for the same.

    As I want to make my server socket to wait continuosly so made it as non-blocking.

    So that if data is not arrived it should not block or close.

    I have made client socket as blocking.






    Sunday, April 10, 2011 8:22 AM
  • The problem with using this non-blocking scheme is that you have to periodically return to the recv() call.  Since, presumably, your code is trying to do other stuff, this makes the code messy, tangled.  I prefer to move the communication code into its own thread, where the code is straightforward and you don't have to waste processor cycles checking the result of your non-blocking recv(); you use a blocking socket and when recv() returns, you either have data that you can promptly process or there is an error.  When you work this way, it's a lot easier to read the code, too.

    Paul T.

    Thursday, April 28, 2011 5:18 PM
  • When you dont use a non blocking socket you need to poll for the data periodically eating up the processor time.


    As explained by Paul, create a thread which runs continuously blocks on new data process it if new data is arrived and/or again blocks on new data arrival.



    Senior Design Engineer T.E.S Electronic Solutions (Bangalore-India)
    • Proposed as answer by Misbah Khan Monday, May 2, 2011 9:26 AM
    Monday, May 2, 2011 6:06 AM
  • Ok I will try the same sir.


    With Thanks



    Monday, May 2, 2011 9:21 AM