Thread: Avoiding program blocking while recv()

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    11

    Avoiding program blocking while recv()

    I need to send many chunks of a file through a socket ... while looping, the client need to check for incoming messages from the server.

    ex:

    Code:
    while(1)
    {
    //check for incoming message from server
    if (message) then recv();
    else then continue sending;
    }
    if I use recv() for checking for incoming message, the program is blocked.

    Is using poll() a good way to make it work ?

    Thanks

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    11
    Finally i'm tryin to use select()

    But select always return -1 .... any idea ?

    client side:
    FD_ZERO(&readset);
    FD_SET(mySocket, &readSet);
    timeVal.tv_sec = 1;
    timeVal.tv_usec = 0;

    int result = select(mySocket + 1, &readSet, NULL, NULL, &timeVal);

    server side:
    messageSize = strlen(trame);
    messageSize = htonl(messageSize);
    (nBytes = send(clientSocket, (char*)&messageSize, sizeof(messageSize), 0);

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > But select always return -1 .... any idea ?
    Maybe look at the value stored in errno, and perhaps use perror() or strerror() to get a text description of the error.

    Did you do this, and get a back socket?
    if ( fd = socket() == 0 )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to Speed Up My Program
    By purestr999 in forum C++ Programming
    Replies: 8
    Last Post: 03-23-2011, 07:23 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. recv function (blocking)
    By aurumice in forum Linux Programming
    Replies: 0
    Last Post: 03-01-2002, 01:19 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM