UDP Socket Set Timeout










25















I am trying to set a 100ms timeout on a UDP Socket. I am using C. I have posted relavent pieces of my code below. I am not sure why this is not timing out, but just hangs when it doesn't receive a segment. Does this only work on sockets that are not bound using the bind() method?



#define TIMEOUT_MS 100 /* Seconds between retransmits */

if ((rcv_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
DieWithError("socket() failed");

if ((rcv_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
DieWithError("socket() failed");

//set timer for recv_socket
static int timeout = TIMEOUT_MS;
setsockopt(rcv_sock, SOL_SOCKET, SO_RCVTIMEO,(char*)&timeout,sizeof(timeout));

if(recvfrom(rcv_sock, ackBuffer,sizeof(ackBuffer), 0,
(struct sockaddr *) &servAddr2, &fromSize) < 0)
//timeout reached
printf("Timout reached. Resending segment %dn", seq_num);
num_timeouts++;










share|improve this question

















  • 2





    It looks like you are not checking the return value from setsockopt to see if it returned an error. It probably is. SO_RCVTIMEO is documented on both Linux and MacOS to take a struct timeval, but you are passing an int. Try passing a struct timeval instead. Also, why are you casting &timeout to char *? It's not a char * at all.

    – Celada
    Nov 25 '12 at 2:52















25















I am trying to set a 100ms timeout on a UDP Socket. I am using C. I have posted relavent pieces of my code below. I am not sure why this is not timing out, but just hangs when it doesn't receive a segment. Does this only work on sockets that are not bound using the bind() method?



#define TIMEOUT_MS 100 /* Seconds between retransmits */

if ((rcv_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
DieWithError("socket() failed");

if ((rcv_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
DieWithError("socket() failed");

//set timer for recv_socket
static int timeout = TIMEOUT_MS;
setsockopt(rcv_sock, SOL_SOCKET, SO_RCVTIMEO,(char*)&timeout,sizeof(timeout));

if(recvfrom(rcv_sock, ackBuffer,sizeof(ackBuffer), 0,
(struct sockaddr *) &servAddr2, &fromSize) < 0)
//timeout reached
printf("Timout reached. Resending segment %dn", seq_num);
num_timeouts++;










share|improve this question

















  • 2





    It looks like you are not checking the return value from setsockopt to see if it returned an error. It probably is. SO_RCVTIMEO is documented on both Linux and MacOS to take a struct timeval, but you are passing an int. Try passing a struct timeval instead. Also, why are you casting &timeout to char *? It's not a char * at all.

    – Celada
    Nov 25 '12 at 2:52













25












25








25


12






I am trying to set a 100ms timeout on a UDP Socket. I am using C. I have posted relavent pieces of my code below. I am not sure why this is not timing out, but just hangs when it doesn't receive a segment. Does this only work on sockets that are not bound using the bind() method?



#define TIMEOUT_MS 100 /* Seconds between retransmits */

if ((rcv_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
DieWithError("socket() failed");

if ((rcv_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
DieWithError("socket() failed");

//set timer for recv_socket
static int timeout = TIMEOUT_MS;
setsockopt(rcv_sock, SOL_SOCKET, SO_RCVTIMEO,(char*)&timeout,sizeof(timeout));

if(recvfrom(rcv_sock, ackBuffer,sizeof(ackBuffer), 0,
(struct sockaddr *) &servAddr2, &fromSize) < 0)
//timeout reached
printf("Timout reached. Resending segment %dn", seq_num);
num_timeouts++;










share|improve this question














I am trying to set a 100ms timeout on a UDP Socket. I am using C. I have posted relavent pieces of my code below. I am not sure why this is not timing out, but just hangs when it doesn't receive a segment. Does this only work on sockets that are not bound using the bind() method?



#define TIMEOUT_MS 100 /* Seconds between retransmits */

if ((rcv_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
DieWithError("socket() failed");

if ((rcv_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
DieWithError("socket() failed");

//set timer for recv_socket
static int timeout = TIMEOUT_MS;
setsockopt(rcv_sock, SOL_SOCKET, SO_RCVTIMEO,(char*)&timeout,sizeof(timeout));

if(recvfrom(rcv_sock, ackBuffer,sizeof(ackBuffer), 0,
(struct sockaddr *) &servAddr2, &fromSize) < 0)
//timeout reached
printf("Timout reached. Resending segment %dn", seq_num);
num_timeouts++;







c sockets udp






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 25 '12 at 2:21









rharrison33rharrison33

62841330




62841330







  • 2





    It looks like you are not checking the return value from setsockopt to see if it returned an error. It probably is. SO_RCVTIMEO is documented on both Linux and MacOS to take a struct timeval, but you are passing an int. Try passing a struct timeval instead. Also, why are you casting &timeout to char *? It's not a char * at all.

    – Celada
    Nov 25 '12 at 2:52












  • 2





    It looks like you are not checking the return value from setsockopt to see if it returned an error. It probably is. SO_RCVTIMEO is documented on both Linux and MacOS to take a struct timeval, but you are passing an int. Try passing a struct timeval instead. Also, why are you casting &timeout to char *? It's not a char * at all.

    – Celada
    Nov 25 '12 at 2:52







2




2





It looks like you are not checking the return value from setsockopt to see if it returned an error. It probably is. SO_RCVTIMEO is documented on both Linux and MacOS to take a struct timeval, but you are passing an int. Try passing a struct timeval instead. Also, why are you casting &timeout to char *? It's not a char * at all.

– Celada
Nov 25 '12 at 2:52





It looks like you are not checking the return value from setsockopt to see if it returned an error. It probably is. SO_RCVTIMEO is documented on both Linux and MacOS to take a struct timeval, but you are passing an int. Try passing a struct timeval instead. Also, why are you casting &timeout to char *? It's not a char * at all.

– Celada
Nov 25 '12 at 2:52












2 Answers
2






active

oldest

votes


















45














The SO_RCVTIMEO option expects a struct timeval defined in sys/time.h, not an integer like you're passing to it. The timeval struct has as field for seconds and a field for microseconds. To set the timeout to 100ms, the following should do the trick:



struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 100000;
if (setsockopt(rcv_sock, SOL_SOCKET, SO_RCVTIMEO,&tv,sizeof(tv)) < 0)
perror("Error");






share|improve this answer
































    0














    I have the same problem. I tried to adopt the solution you suggested, using the timeval struct. But it did not seem to work.



    I have read on the Microsoft documentation and the time should be a DWORD with the number of milliseconds, but there is also another thing to do, If the socket is created using the WSASocket function, then the dwFlags parameter must have the WSA_FLAG_OVERLAPPED attribute set for the timeout to function properly.
    Otherwise the timeout never takes effect.






    share|improve this answer
























      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f13547721%2fudp-socket-set-timeout%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      45














      The SO_RCVTIMEO option expects a struct timeval defined in sys/time.h, not an integer like you're passing to it. The timeval struct has as field for seconds and a field for microseconds. To set the timeout to 100ms, the following should do the trick:



      struct timeval tv;
      tv.tv_sec = 0;
      tv.tv_usec = 100000;
      if (setsockopt(rcv_sock, SOL_SOCKET, SO_RCVTIMEO,&tv,sizeof(tv)) < 0)
      perror("Error");






      share|improve this answer





























        45














        The SO_RCVTIMEO option expects a struct timeval defined in sys/time.h, not an integer like you're passing to it. The timeval struct has as field for seconds and a field for microseconds. To set the timeout to 100ms, the following should do the trick:



        struct timeval tv;
        tv.tv_sec = 0;
        tv.tv_usec = 100000;
        if (setsockopt(rcv_sock, SOL_SOCKET, SO_RCVTIMEO,&tv,sizeof(tv)) < 0)
        perror("Error");






        share|improve this answer



























          45












          45








          45







          The SO_RCVTIMEO option expects a struct timeval defined in sys/time.h, not an integer like you're passing to it. The timeval struct has as field for seconds and a field for microseconds. To set the timeout to 100ms, the following should do the trick:



          struct timeval tv;
          tv.tv_sec = 0;
          tv.tv_usec = 100000;
          if (setsockopt(rcv_sock, SOL_SOCKET, SO_RCVTIMEO,&tv,sizeof(tv)) < 0)
          perror("Error");






          share|improve this answer















          The SO_RCVTIMEO option expects a struct timeval defined in sys/time.h, not an integer like you're passing to it. The timeval struct has as field for seconds and a field for microseconds. To set the timeout to 100ms, the following should do the trick:



          struct timeval tv;
          tv.tv_sec = 0;
          tv.tv_usec = 100000;
          if (setsockopt(rcv_sock, SOL_SOCKET, SO_RCVTIMEO,&tv,sizeof(tv)) < 0)
          perror("Error");







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Oct 11 '14 at 12:33









          Hi I'm Frogatto

          19.8k85794




          19.8k85794










          answered Nov 25 '12 at 2:55









          NealNeal

          3,15042728




          3,15042728























              0














              I have the same problem. I tried to adopt the solution you suggested, using the timeval struct. But it did not seem to work.



              I have read on the Microsoft documentation and the time should be a DWORD with the number of milliseconds, but there is also another thing to do, If the socket is created using the WSASocket function, then the dwFlags parameter must have the WSA_FLAG_OVERLAPPED attribute set for the timeout to function properly.
              Otherwise the timeout never takes effect.






              share|improve this answer





























                0














                I have the same problem. I tried to adopt the solution you suggested, using the timeval struct. But it did not seem to work.



                I have read on the Microsoft documentation and the time should be a DWORD with the number of milliseconds, but there is also another thing to do, If the socket is created using the WSASocket function, then the dwFlags parameter must have the WSA_FLAG_OVERLAPPED attribute set for the timeout to function properly.
                Otherwise the timeout never takes effect.






                share|improve this answer



























                  0












                  0








                  0







                  I have the same problem. I tried to adopt the solution you suggested, using the timeval struct. But it did not seem to work.



                  I have read on the Microsoft documentation and the time should be a DWORD with the number of milliseconds, but there is also another thing to do, If the socket is created using the WSASocket function, then the dwFlags parameter must have the WSA_FLAG_OVERLAPPED attribute set for the timeout to function properly.
                  Otherwise the timeout never takes effect.






                  share|improve this answer















                  I have the same problem. I tried to adopt the solution you suggested, using the timeval struct. But it did not seem to work.



                  I have read on the Microsoft documentation and the time should be a DWORD with the number of milliseconds, but there is also another thing to do, If the socket is created using the WSASocket function, then the dwFlags parameter must have the WSA_FLAG_OVERLAPPED attribute set for the timeout to function properly.
                  Otherwise the timeout never takes effect.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Mar 18 '18 at 1:26









                  Stephen Rauch

                  28.4k153457




                  28.4k153457










                  answered Mar 18 '18 at 1:08









                  user9510357user9510357

                  1




                  1



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f13547721%2fudp-socket-set-timeout%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      這個網誌中的熱門文章

                      How to read a connectionString WITH PROVIDER in .NET Core?

                      In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

                      Museum of Modern and Contemporary Art of Trento and Rovereto