|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
|||
|
|||
|
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 |
|
#2
|
||||
|
||||
|
Ok, I added a timeout handler for you. Enjoy
![]()
__________________
"A poor programmer is he who blames his tools." http://analyser.oli.tudelft.nl/ |
|
#3
|
|||
|
|||
|
Thank you!!
You have made my day =) |
|
#4
|
||||
|
||||
|
Why don't I have half of those libraries? I have the full version of MSVC++ too..
__________________
hmmm... |
|
#6
|
||||
|
||||
|
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. |
|
#7
|
||||
|
||||
|
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 */
|
|
#8
|
|||
|
|||
|
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
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > recvfrom() never ending |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|