The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
recvfrom() never ending
Discuss recvfrom() never ending in the C Programming forum on Dev Shed. recvfrom() never ending C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 18th, 2003, 02:58 PM
|
|
Junior Member
|
|
Join Date: Feb 2003
Posts: 3
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
recvfrom() never ending
I have some code in development for sending a dgram packet to a server and waiting for a reply. When I recvfrom, but there is no data back from the server then the program waits for ever! Is there some way I can timeout the recvfrom()?
I've included the source in a zip for anyone who's interested.
Simon M
|

February 18th, 2003, 03:39 PM
|
 |
*bounce*
|
|
Join Date: Jan 2002
Location: Delft, The Netherlands
|
|
Ok, I added a timeout handler for you. Enjoy 
__________________
"A poor programmer is he who blames his tools."
http://analyser.oli.tudelft.nl/
|

February 18th, 2003, 05:08 PM
|
|
Junior Member
|
|
Join Date: Feb 2003
Posts: 3
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Thank you!!
You have made my day =)
|

February 18th, 2003, 09:15 PM
|
 |
Contributing User
|
|
Join Date: Nov 2002
Posts: 421
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 11
|
|
|
Why don't I have half of those libraries? I have the full version of MSVC++ too..
__________________
hmmm...
|

February 19th, 2003, 02:19 AM
|
 |
*bounce*
|
|
Join Date: Jan 2002
Location: Delft, The Netherlands
|
|
Actually, those are just references to header files. -And- this is UN*X code (I test-compiled it on my Linux box). I've heard that on win32, you can simply use something like
Code:
#include <winsock.h>
to do TCP/IP socket programming. When in doubt, look here.
|

February 19th, 2003, 02:28 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
Another good getting-started reference under Visual C++ is "Transitioning from UNIX to Windows Socket Programming" by Paul O'Steen at http://cs.baylor.edu/~donahoo/pract...dowsSockets.pdf . In it, he gives instructions on converting a UNIX sockets program to a Win32 Winsock console application. Just be aware that while you can do multithreading in both UNIX and Win32, the function names are different. Also, you cannot do process forking or signalling under Windows, but Win32 has Sleep() now.
Analyzer, I'm just starting to learn UNIX programming, so I'll keep that use of SIGALRM in mind, as well as the use of sigsetjmp() and siglongjmp(). Thanks.
|

February 19th, 2003, 02:47 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
A Solution for Both Worlds
Another solution to this recvfrom() problem would be to use non-blocking sockets. This solution will work both under UNIX and under Winsock.
This example is in Winsock (hence the WSA... names):
Code:
#define RECV_TIMEOUT 10 /* timeout in seconds */
SOCKET sock; /* Socket descriptor -- int in UNIX */
unsigned long nonblocking = 1; /* Flag to make socket nonblocking */
int timeout;
/* Set the socket to nonblocking */
/* fcntl(sock, F_SETFL,O_NONBLOCK|FASYNC) under UNIX */
if (ioctlsocket(sock, FIONBIO, &nonblocking) != 0)
DieWithError("ioctlsocket() failed");
...
/* Receive a single datagram from the server */
for (timeout=RECV_TIMEOUT; timeout > 0; timeout--)
{
/* recvfrom() returns -1 if error */
if ((recvStringLen = recvfrom(sock, recvString, MAXRECVSTRING, 0, NULL, 0)) < 0)
{
if (WSAGetLastError() != WSAEWOULDBLOCK)
/* EWOULDBLOCK in UNIX */
DieWithError("recvfrom() failed");
else
{
printf("Still have not received packet...Waiting and then trying again\n");
Sleep(1000); /* Sleep for 1 second */
}
}
else /* have received datagram */
break;
}
if (!timeout)
{
printf("Timed out.\n");
exit(1);
}
/* process received datagram */
|

February 19th, 2003, 03:46 PM
|
|
Junior Member
|
|
Join Date: Feb 2003
Posts: 3
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
someone told me in comp.unix.programmer that it could be done with poll() although I couldn't find anything simple enough for me to understand about it! Just another idea into the pool
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|