non-blocking recv() problem when webserver disconnects

Discussion in 'Linux Networking' started by Erik Larsson, Feb 25, 2004.

  1. Erik Larsson

    Erik Larsson Guest

    I'm having trouble a non-blocking revc(). If I'm correct shall recv()
    return the amount of bytes revived at particular moment, and return 0
    when EOF and –1 when error.

    My program connects to a web server and sends the "GET / HTTP1.1…."
    the the socket starts to receive data from the web server. But recv()
    NEVER returns 0 or –1. So the connection is never ended witch hangs
    the program there until the socket timeouts.

    Any ideas what can be wrong?

    Here's some code:
    FD_ZERO(&real_mask);
    FD_SET(wget_s.getSock(), &real_mask);

    tv.tv_sec = (long) 50000;
    tv.tv_usec = (long) 0;

    wget_s.set_non_blocking(true);

    struct pollfd poll_list[1];
    int t, q;

    try
    {

    poll_list[0].fd = wget_s.getSock();
    poll_list[0].events = POLLIN;
    t=0;
    q=1;
    while(t != 1)
    {
    cout << poll(poll_list,(unsigned long)1,-1) << endl;

    if(((poll_list[0].revents&POLLHUP) == POLLHUP) ||
    ((poll_list[0].revents&POLLERR) == POLLERR) ||
    ((poll_list[0].revents&POLLNVAL) == POLLNVAL))
    {
    cout << "Mysko" << endl;
    return 0;
    }

    if((poll_list[0].revents&POLLIN) == POLLIN)
    {
    wget_s.set_non_blocking(false);

    cout << wget_s.recv(wget_data) << " RECV" << endl;

    cout << wget_s.send(" ") << " SEND" << endl;

    cout <<wget_data.find("0",0) << endl;
    cout << wget_data << endl;
    }
    }

    }
    catch (SocketException &e)
    {
    cout << "Exception" << endl;
    }
     
    Erik Larsson, Feb 25, 2004
    #1
    1. Advertisements

  2. Erik Larsson

    Cameron Kerr Guest

    It sounds like its not being set non-blocking. Run it through strace to
    see what the system calls are doing.

    But what's the point? You're polling as well as non-blocking? You only
    really need one method or the other.
     
    Cameron Kerr, Feb 26, 2004
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.