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?
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?
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.
Do you know where I could find a decent select() example?
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
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.
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.
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; } }
> 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.
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 09:54 AM.
http://livebad.com/nuka
Da-Nuka
Yes, WSAAsyncSelect or other related functions are probably more appropriate for a Windows app.
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.
Thats the beauty of it all, you do it exactly the same way.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...
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.
Another example:
http://faq.cprogramming.com/cgi-bin/...=1045780608#03
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
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.Code:select(0, &readfds, 0, 0, &count);