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

Thread: example of SO_RCVTIMEO using setsockopt()

  1. #1
    Join Date
    Jun 2005
    Posts
    35

    example of SO_RCVTIMEO using setsockopt()

    Hi,

    I want to implement a timeout using SO_RCVTIMEO using setsockopt().
    Does anyone have some example on this in C/VC?.

    I want to do this for the recv() command.

    The receive timeout is 20 secs.

    Thanks

  2. #2
    Join Date
    Mar 2005
    Posts
    137

    Re: example of SO_RCVTIMEO using setsockopt()

    Code:
    struct timeval tv;
    
    tv.tv_sec = 30;  /* 30 Secs Timeout */
    
    setsockopt(sockid, SOL_SOCKET, SO_RCVTIMEO,(struct timeval *)&tv,sizeof(struct timeval));
    Thanks,

  3. #3
    Join Date
    Nov 2007
    Posts
    1

    Re: example of SO_RCVTIMEO using setsockopt()

    This works, except "recv" returns after 500ms regardless of the value on my system...

    Axl

  4. #4
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: example of SO_RCVTIMEO using setsockopt()

    Maybe you have actually received data.

    Mike

    PS: Start a new thread. This one's more than two years old

  5. #5
    Join Date
    May 2009
    Posts
    140

    Re: example of SO_RCVTIMEO using setsockopt()

    Quote Originally Posted by nkhambal View Post
    Code:
    struct timeval tv;
    
    tv.tv_sec = 30;  /* 30 Secs Timeout */
    
    setsockopt(sockid, SOL_SOCKET, SO_RCVTIMEO,(struct timeval *)&tv,sizeof(struct timeval));
    Thanks,
    doesn't work, says can't converth 4th parameter from 'timeval *' to 'const char'

  6. #6
    Join Date
    Jul 2006
    Posts
    1

    Re: example of SO_RCVTIMEO using setsockopt()

    Quote Originally Posted by Owyn View Post
    doesn't work, says can't converth 4th parameter from 'timeval *' to 'const char'
    Change to:
    setsockopt(sockid, SOL_SOCKET, SO_RCVTIMEO,(char *)&tv,sizeof(struct timeval));

  7. #7
    Join Date
    Feb 2002
    Posts
    4,640

    Re: example of SO_RCVTIMEO using setsockopt()

    Heh. Initial post was 2005. Then nothing, until 2007. Then nothing, until 2009. Then nothing until yesterday. Well, at least the responses are getting "quicker"!

    Viggy

  8. #8
    Join Date
    Jul 2010
    Posts
    1

    Re: example of SO_RCVTIMEO using setsockopt()

    first... THANX...

    It works for me, but 'tv.tv_sec' seams to be in miliseconds not in seconds. If i put 5000 i got a 5 sec. timeout.

  9. #9
    Join Date
    Aug 2009
    Posts
    1

    Re: example of SO_RCVTIMEO using setsockopt()

    Are you sure the struct timeval should be used as a parameter?
    I suppose timeout should be rather passed as int (or unsigned int), in milliseconds:

    int nTimeout = 30000; // 30 seconds
    setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, (const char*)&nTimeout, sizeof(int));

    Unfortunately, this is not specified in setsockopt's documentation.

  10. #10
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: example of SO_RCVTIMEO using setsockopt()

    The timing of posts to this thread is hysterical.

  11. #11
    Join Date
    Apr 2011
    Posts
    1

    Re: example of SO_RCVTIMEO using setsockopt()

    WINDOWS:
    Timeout value is a DWORD in milliseconds, address passed to setsockopt() is const char *

    LINUX:
    Timeout value is a struct timeval, address passed to setsockopt() is const void *

  12. #12
    Join Date
    Sep 2009
    Posts
    1

    Re: example of SO_RCVTIMEO using setsockopt()

    Quote Originally Posted by grant.s View Post
    WINDOWS:
    Timeout value is a DWORD in milliseconds, address passed to setsockopt() is const char *

    LINUX:
    Timeout value is a struct timeval, address passed to setsockopt() is const void *

    Thank you!

    I need to find a good cheat-sheet reference for all the diffs between BSD and winsock.

    -Ed

  13. #13
    Join Date
    Nov 2016
    Posts
    1

    Re: example of SO_RCVTIMEO using setsockopt()

    vcdeveloper, I think we are close to finding a solution to your issue.
    Is it acceptable for a 21 second timeout instead of the 20 seconds you requested?

  14. #14
    Join Date
    Apr 2016
    Posts
    5

    Re: example of SO_RCVTIMEO using setsockopt()

    By various reasons I would like to implement timeout on reading and writing to socket in a server but fail to get it running and therefore kindly ask for some insight into wherein the problem may reside.

    In order to set the timeout on the read and write to the socket I'm trying to use of the functions setsocketopt() and getsocketopt(). However I must be doing something wrong as the return value indicates that a problem have occurred and perror outputs "Invalid argument". Strangely enough the error does not always occur at the first usage of setsocketopt() and getsocketopt(), which puzzles me bit.

  15. #15
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,783

    Re: example of SO_RCVTIMEO using setsockopt()

    Quote Originally Posted by essay View Post
    ...
    In order to set the timeout on the read and write to the socket I'm trying to use of the functions setsocketopt() and getsocketopt(). However I must be doing something wrong as the return value indicates that a problem have occurred and perror outputs "Invalid argument".
    You might want to post your code so we could see how you did it...
    Victor Nijegorodov

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)