C Board  

Go Back   C Board > General Programming Boards > Networking/Device Communication

Reply
 
LinkBack Thread Tools Display Modes
Old 02-28-2005, 12:21 AM   #1
eam
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?
eam is offline   Reply With Quote
Old 02-28-2005, 12:36 AM   #2
& the hat of GPL slaying
 
Thantos's Avatar
 
Join Date: Sep 2001
Posts: 5,732
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.
Thantos is offline   Reply With Quote
Old 02-28-2005, 12:49 AM   #3
eam
Registered User
 
eam's Avatar
 
Join Date: Oct 2003
Posts: 53
Do you know where I could find a decent select() example?
eam is offline   Reply With Quote
Old 02-28-2005, 12:59 AM   #4
& the hat of GPL slaying
 
Thantos's Avatar
 
Join Date: Sep 2001
Posts: 5,732
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
Thantos is offline   Reply With Quote
Old 02-28-2005, 01:21 AM   #5
eam
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.
eam is offline   Reply With Quote
Old 02-28-2005, 01:27 AM   #6
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,683
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.

Salem is offline   Reply With Quote
Old 02-28-2005, 01:59 AM   #7
eam
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;
     }
}
eam is offline   Reply With Quote
Old 02-28-2005, 02:28 AM   #8
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,683
> 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.

Salem is offline   Reply With Quote
Old 02-28-2005, 09:51 AM   #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..
__________________
http://livebad.com/nuka
Da-Nuka

Last edited by Da-Nuka; 02-28-2005 at 09:54 AM.
Da-Nuka is offline   Reply With Quote
Old 02-28-2005, 11:13 AM   #10
Registered User
 
Join Date: Dec 2004
Posts: 95
Yes, WSAAsyncSelect or other related functions are probably more appropriate for a Windows app.
azteched is offline   Reply With Quote
Old 02-28-2005, 12:25 PM   #11
& the hat of GPL slaying
 
Thantos's Avatar
 
Join Date: Sep 2001
Posts: 5,732
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.

Quote:
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.
Thantos is offline   Reply With Quote
Old 02-28-2005, 06:23 PM   #12
End Of Line
 
Hammer's Avatar
 
Join Date: Apr 2002
Posts: 6,240
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]
Hammer is offline   Reply With Quote
Old 02-28-2005, 07:29 PM   #13
Registered User
 
Join Date: Sep 2004
Location: California
Posts: 3,020
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.
bithub is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
recv() returns 0 the second time Overlord Networking/Device Communication 7 07-10-2009 04:09 AM
Question about recv carrotcake1029 Networking/Device Communication 2 02-26-2009 02:10 PM
Query related to recv()? dp_76 Networking/Device Communication 4 05-16-2005 07:25 AM
recv() afisher Networking/Device Communication 3 03-24-2004 05:32 PM
non blocking recv rohit Linux Programming 4 03-05-2002 09:35 PM


All times are GMT -6. The time now is 05:33 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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