CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jun 2002
    Posts
    18

    Socket related query

    in my client/server socket application

    after establishing the connection bitween client socket and server socket.

    server socket waits for client to send some data. it stays there untill client sends some thing like

    //iReceived = recv (clntInfo->lSocket, clntInfo->rcvbuff, 100, 0);

    it will not step to next statement.

    what i have to do is ... i have to wait for 1 min and if client doesen't send anything in this time period i have to close the socket.
    how to do this... i am programming in C
    its very urgent.

  2. #2
    Join Date
    Sep 2002
    Location
    Belarus - Tirol, Austria
    Posts
    647
    use select() function.
    It's last parameter is a struct timeval and U can set it for maximum time U need to wait.
    Read about it in doc's U have. If U haven't doc's write me ( here or pm ) and I ' ll provide U some code.
    "UNIX is simple; it just takes a genius to understand its simplicity!"

  3. #3
    Join Date
    Jun 2002
    Posts
    18
    yes i need the code pls send as soon as possible

  4. #4
    Join Date
    Jun 2002
    Posts
    18
    yes i need the code

  5. #5
    Join Date
    Sep 2002
    Location
    Belarus - Tirol, Austria
    Posts
    647
    I have post U private message with some code.
    "UNIX is simple; it just takes a genius to understand its simplicity!"

  6. #6
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Originally posted by dimm_coder
    use select() function.
    It's last parameter is a struct timeval and U can set it for maximum time U need to wait.
    Read about it in doc's U have. If U haven't doc's write me ( here or pm ) and I ' ll provide U some code.
    The timeout you pass to select is only the timeout for the select function itself. It does not influence 'recv()' directly. Of course if you use 'select()' to determine whether there is a socket available for receive you indirectly influence 'recv()' as well.

    Nevertheless if you want to set the timeout for the 'recv()' function itself you can set a socket options...
    Code:
    int iOptval = 2000;
    
    if(setsockopt(Socket, SOL_SOCKET, SO_RCVTIMEO, (char *) &iOptval, sizeof(int)))
      // Error -> call 'WSAGetLastError()'

  7. #7
    Join Date
    Sep 2002
    Location
    Belarus - Tirol, Austria
    Posts
    647
    Andreas, of caurse, I mean that U mast call recv() next.

    I mean next:

    //
    // that only part from my code.. without some adaptance
    //
    fd_set fdRead;
    int nRet;
    char buf[1024];

    // sock - it's your socket with client
    // U may also add another to

    FD_ZERO( &fdRead );
    FD_SET( sock, &fdRead );

    struct timeval; // TIMEVAL for Win to
    timeval.tv_sec = 60; // for U

    // wait not more than 60 sec.
    nRet = select( 0, &fdRead, NULL, NULL, &timeval );
    if ( SOCKET_ERROR == nRet )
    {
    break;
    }
    if ( nRet > 0 )
    {
    // if client have send data
    if ( FD_ISSET( sock, &fdRead ))
    {
    nRet = recv( sock, buf, sizeof(buf), 0 );

    if ( nRet == 0 || SOCKET_ERROR == nRet )
    {
    bIsError = true;
    }

    // process your data
    }
    else
    { // client haven't send data
    }
    }
    Last edited by dimm_coder; October 10th, 2002 at 10:55 AM.
    "UNIX is simple; it just takes a genius to understand its simplicity!"

  8. #8
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Originally posted by dimm_coder
    Andreas, of caurse, I mean that U mast call recv() next.
    Yes..that is the common way of doing it...I did it too all the time...

    Just wanted to point out that there are different possibilities....

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