C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old February 18th, 2003, 02:58 PM
mrcookie mrcookie is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 3 mrcookie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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
Attached Files
File Type: zip gumbo.zip (965 Bytes, 464 views)

Reply With Quote
  #2  
Old February 18th, 2003, 03:39 PM
Analyser's Avatar
Analyser Analyser is offline
*bounce*
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2002
Location: Delft, The Netherlands
Posts: 510 Analyser User rank is Corporal (100 - 500 Reputation Level)Analyser User rank is Corporal (100 - 500 Reputation Level)Analyser User rank is Corporal (100 - 500 Reputation Level)Analyser User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 20 h 37 m 7 sec
Reputation Power: 9
Send a message via ICQ to Analyser
Ok, I added a timeout handler for you. Enjoy
Attached Files
File Type: zip gumbo.zip (1.2 KB, 1123 views)
__________________
"A poor programmer is he who blames his tools."
http://analyser.oli.tudelft.nl/

Reply With Quote
  #3  
Old February 18th, 2003, 05:08 PM
mrcookie mrcookie is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 3 mrcookie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thank you!!

You have made my day =)

Reply With Quote
  #4  
Old February 18th, 2003, 09:15 PM
andy3109's Avatar
andy3109 andy3109 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 421 andy3109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 6
Send a message via AIM to andy3109
Why don't I have half of those libraries? I have the full version of MSVC++ too..
__________________
hmmm...

Reply With Quote
  #5  
Old February 19th, 2003, 02:19 AM
Analyser's Avatar
Analyser Analyser is offline
*bounce*
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2002
Location: Delft, The Netherlands
Posts: 510 Analyser User rank is Corporal (100 - 500 Reputation Level)Analyser User rank is Corporal (100 - 500 Reputation Level)Analyser User rank is Corporal (100 - 500 Reputation Level)Analyser User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 20 h 37 m 7 sec
Reputation Power: 9
Send a message via ICQ to Analyser
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.

Reply With Quote
  #6  
Old February 19th, 2003, 02:28 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,803 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 11 h 56 m 57 sec
Reputation Power: 437
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.

Reply With Quote
  #7  
Old February 19th, 2003, 02:47 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,803 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 11 h 56 m 57 sec
Reputation Power: 437
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  */

Reply With Quote
  #8  
Old February 19th, 2003, 03:46 PM
mrcookie mrcookie is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 3 mrcookie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > recvfrom() never ending


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway