CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: recv() hang

  1. #1
    Join Date
    Apr 2009
    Posts
    21

    recv() hang

    i have server and client application running on different system. server is just sending data and client is receiving. when server side app's network disconnected(not client side N/W down) then client app's recv() hang waiting for data(recv() is blocking fun and i dont want it to be non-blocking) so my app hang. can anyone tell me how to come out from blocking recv()

  2. #2
    Join Date
    Oct 2005
    Location
    Bangalore
    Posts
    167

    Re: recv() hang

    At the time of closing the server send the closing status to all connected client.

    In the client side, simple close the socket.

  3. #3
    Join Date
    Apr 2009
    Posts
    21

    Re: recv() hang

    server is not closing but server network is closing and at that time server does not send any status, tell me another thing

  4. #4
    Join Date
    Feb 2005
    Posts
    2,160

    Re: recv() hang

    It's hard to say what might be going on without seeing any code. You say you want recv() to block for some reason. Would it be acceptable to use select on your recv() calls? That might be all it takes. Try something like this: replace your recv() call with this call:

    Code:
    #define _MY_TIMEOUT_ 5 //block for up to 5 seconds for each select()
    
    int w_recv(unsigned s,void *ph,int len,int flags)
    {
      fd_set r,e;
      struct timeval tval;
      int i,retval,done=0,total=0;
    
      while(!done){
        FD_ZERO(&r);FD_ZERO(&e);
        FD_SET(s,&r);FD_SET(s,&e);
        tval.tv_sec=_MY_TIMEOUT_;
        tval.tv_usec=0;
        retval=select(s+1,&r,NULL,&e,&tval);
        if(retval==0){
          //Timeout elapsed with no activity
          return 0;
        }
        if(retval==-1){
          //Socket error with select().
          return -1;
        }
        if(FD_ISSET(s,&e)){
          //some other TCP/IP error triggered select()
          return -1;
        }
        if(FD_ISSET(s,&r)){
          //data is available on the socket  Gather it up
          i=recv(s,((char *)ph)+total,len-total,flags);
          if(i==-1){
            //Socket error with recv().
            return -1;
          }
          if(i==0){
            //something is wrong.  select trigged but there is no data
            return 0;
          }
          //loop until all requested data is received
          total+=i;
          if(total==len){
            done=1;
          }
        }
      }//done
      return total;
    }

  5. #5
    Join Date
    Apr 2009
    Posts
    21

    Re: recv() hang

    i will try it and reply accordingly on monday

  6. #6
    Join Date
    Apr 2009
    Posts
    21

    Re: recv() hang

    i try select func but with time 0 bcoz i dont want to block select func for 5 second bcoz i m getting data continusly, when i used this select always returning -1 and i m also getting data continusly.

  7. #7
    Join Date
    Nov 2006
    Posts
    6

    Re: recv() hang

    setsockopt and SO_KEEPALIVE

  8. #8
    Join Date
    Apr 2009
    Posts
    21

    Re: recv() hang

    would u plz tell me about this flag n i think this flag used in tcp/ip connection, but i m not using tcp/ip connection

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured