recv() without waiting...

This is a discussion on recv() without waiting... within the Networking/Device Communication forums, part of the General Programming Boards category; recv() just waits until the server sends something. Is there any way to use recv() (or a similar function) to ...

  1. #1
    eam
    eam is offline
    Registered User eam's Avatar
    Join Date
    Oct 2003
    Posts
    53

    recv() without waiting...

    recv() just waits until the server sends something. Is there any way to use recv() (or a similar function) to get text if its sent but continue on if its not?

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    What you do is to use select() to see if the socket is avialable to read from, if it is you do the read, if not you move on.

  3. #3
    eam
    eam is offline
    Registered User eam's Avatar
    Join Date
    Oct 2003
    Posts
    53
    Do you know where I could find a decent select() example?

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Yes in fact I do.

    Since I want you to work for it I'll give you a couple of hints instead of flat out telling you:
    Networking/Device Communication, search, Beej, select

  5. #5
    eam
    eam is offline
    Registered User eam's Avatar
    Join Date
    Oct 2003
    Posts
    53
    Aww Beej's example is the example I found to be "undecent." One is too simplified (stdin! how do I do this with sockets?) and the other is too complex...

    Thanks anyway.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    http://cboard.cprogramming.com/search.php
    Search: Key Word(s): select, recv Showing results 1 to 20 of 20 Search took 0.32 seconds.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    eam
    eam is offline
    Registered User eam's Avatar
    Join Date
    Oct 2003
    Posts
    53
    It still waits... What'd I do wrong?

    Code:
    fd_set readfds;
    struct timeval count;
    FD_ZERO(&readfds);
    FD_SET(sockfd, &readfds);
    select(0, &readfds, 0, 0, &count);
    if (FD_ISSET(sockfd, &readfds))
    {	
         if ((bytes=recv(sockfd, buffer2, 200, 0)) == -1) {
              return 0;
         }
    }

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    > What'd I do wrong?
    You didn't initialise count
    You didn't check the return result
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Politics&Cpp geek Da-Nuka's Avatar
    Join Date
    Oct 2004
    Posts
    104
    Im no expert here, but might WSAAsyncSelect fulfil your requirements?

    http://msdn.microsoft.com/library/en...ncselect_2.asp

    What it does: It makes all the WINSOCK functions which usually waits for such things as you desribed, before moving on. This often hang your programs. This function makes such functions return if nothing are found, and when they succseed(or something else, which you can pass as a parameter), they send your program a WM_SOMETHING instead

    Looks to me like this is exactly what you need?

    Moore info about this can also be found at the botttom of this page:
    http://www.hal-pc.org/~johnnie2/winsock.html

    A sweet tutorial..
    Last edited by Da-Nuka; 02-28-2005 at 08:54 AM.

  10. #10
    Registered User
    Join Date
    Dec 2004
    Posts
    95
    Yes, WSAAsyncSelect or other related functions are probably more appropriate for a Windows app.

  11. #11
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Of course no where has it been said if this is for a Window's app. select() is a better solution if you want it to be cross platform.

    Aww Beej's example is the example I found to be "undecent." One is too simplified (stdin! how do I do this with sockets?) and the other is too complex...
    Thats the beauty of it all, you do it exactly the same way.

    You need to initalize your count with the maxiumn timeout value. After the call it's value can be how much time was left (or is it how long did it wait) or the same value. You can't be exactly sure so each time you have to reset count.

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  13. #13
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Code:
    select(0, &readfds, 0, 0, &count);
    If you are running this on a non-windows machine, the above line will not work. The first parameter needs to be sockfd+1. If it is a windows machine, then the first value passed to select() is ignored, so it doesn't matter.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. recv() returns 0 the second time
    By Overlord in forum Networking/Device Communication
    Replies: 7
    Last Post: 07-10-2009, 04:09 AM
  2. Question about recv
    By carrotcake1029 in forum Networking/Device Communication
    Replies: 2
    Last Post: 02-26-2009, 01:10 PM
  3. Query related to recv()?
    By dp_76 in forum Networking/Device Communication
    Replies: 4
    Last Post: 05-16-2005, 07:25 AM
  4. recv()
    By afisher in forum Networking/Device Communication
    Replies: 3
    Last Post: 03-24-2004, 04:32 PM
  5. non blocking recv
    By rohit in forum Linux Programming
    Replies: 4
    Last Post: 03-05-2002, 08:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21