asynchronous socket operations - question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • a.kostrzewa

    #1

    asynchronous socket operations - question

    I have a question about C#. How can I stop asynchronous read/write operation
    ( BeginReceive() / BeginSend() ) if timeout occurs?
    The Socket class doesn't make any cancel method available.
    I used CancelIo(HANDLE hFile) method (declared in Winbase.h) in C++.

    Thanks for help.
    Olek


  • Daniel Bass

    #2
    Re: asynchronous socket operations - question

    You could always spawn a new thread which then calls the synchronous
    version, into which you can place a timeout parameter.

    "a.kostrzew a" <aX.kostrzewaX@ remove_this_and _X.uposX.comX.p lX> wrote in
    message news:bmg8b5$m1i $1@nemesis.news .tpi.pl...
    I have a question about C#. How can I stop asynchronous read/write operation
    ( BeginReceive() / BeginSend() ) if timeout occurs?
    The Socket class doesn't make any cancel method available.
    I used CancelIo(HANDLE hFile) method (declared in Winbase.h) in C++.

    Thanks for help.
    Olek



    Comment

    • Girish Bharadwaj

      #3
      Re: asynchronous socket operations - question

      I might be wrong here.. but, can you call EndSend() to complete the
      operation?

      --
      Girish Bharadwaj
      "Daniel Bass" <I'm really @ sick of spam> wrote in message
      news:O8p3vskkDH A.2160@TK2MSFTN GP10.phx.gbl...[color=blue]
      > You could always spawn a new thread which then calls the synchronous
      > version, into which you can place a timeout parameter.
      >
      > "a.kostrzew a" <aX.kostrzewaX@ remove_this_and _X.uposX.comX.p lX> wrote in
      > message news:bmg8b5$m1i $1@nemesis.news .tpi.pl...
      > I have a question about C#. How can I stop asynchronous read/write[/color]
      operation[color=blue]
      > ( BeginReceive() / BeginSend() ) if timeout occurs?
      > The Socket class doesn't make any cancel method available.
      > I used CancelIo(HANDLE hFile) method (declared in Winbase.h) in C++.
      >
      > Thanks for help.
      > Olek
      >
      >
      >[/color]


      Comment

      • a.kostrzewa

        #4
        Re: asynchronous socket operations - question

        Yes, of course.
        I call asynchronous read operation in this way:

        IAsyncResult oResult = oSocket.BeginRe ceive(...);
        oWH = m_oResult .AsyncWaitHandl e;
        // oEventsHandler - my class object
        oEventsHandler. WaitForGroupEQ( Timeout.Infinit e ,nGroupId,out nEvent);
        int nNumberOfBytesR ead = ReadEnd();

        But I can't break this operation.

        Best regards,
        Olek

        U¿ytkownik "Girish Bharadwaj" <girishb.at.mvp s.dot.org> napisa³ w wiadomo¶ci
        news:OMZXDKlkDH A.2456@TK2MSFTN GP09.phx.gbl...[color=blue]
        > I might be wrong here.. but, can you call EndSend() to complete the
        > operation?
        >
        > --
        > Girish Bharadwaj
        > "Daniel Bass" <I'm really @ sick of spam> wrote in message
        > news:O8p3vskkDH A.2160@TK2MSFTN GP10.phx.gbl...[color=green]
        > > You could always spawn a new thread which then calls the synchronous
        > > version, into which you can place a timeout parameter.
        > >
        > > "a.kostrzew a" <aX.kostrzewaX@ remove_this_and _X.uposX.comX.p lX> wrote in
        > > message news:bmg8b5$m1i $1@nemesis.news .tpi.pl...
        > > I have a question about C#. How can I stop asynchronous read/write[/color]
        > operation[color=green]
        > > ( BeginReceive() / BeginSend() ) if timeout occurs?
        > > The Socket class doesn't make any cancel method available.
        > > I used CancelIo(HANDLE hFile) method (declared in Winbase.h) in C++.
        > >
        > > Thanks for help.
        > > Olek
        > >
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • Rich Blum

          #5
          Re: asynchronous socket operations - question

          "a.kostrzew a" <aX.kostrzewaX@ remove_this_and _X.uposX.comX.p lX> wrote in message news:<bmip02$n4 m$1@atlantis.ne ws.tpi.pl>...[color=blue]
          > Yes, of course.
          > I call asynchronous read operation in this way:
          >
          > IAsyncResult oResult = oSocket.BeginRe ceive(...);
          > oWH = m_oResult .AsyncWaitHandl e;
          > // oEventsHandler - my class object
          > oEventsHandler. WaitForGroupEQ( Timeout.Infinit e ,nGroupId,out nEvent);
          > int nNumberOfBytesR ead = ReadEnd();
          >
          > But I can't break this operation.
          >[/color]
          Olek -

          From what I can tell from your code it looks like you are blocking
          the main thread until the EndReceive() callback method fires. This
          somewhat defeats the purpose of using an asynchronous socket call.
          Instead of waiting indefinitely for an answer, why don't you put a
          reasonable timeout time in the WaitForGroupEQ method. When the timeout
          is reached you know there wasn't a response. If you then Close() the
          Socket object, the EndReceive() method will fire, and throw an
          Exception. You should catch the Exception in a try-catch block and
          handle it accordingly.

          Alternatively, you can use the blocking Receive() method, and set
          the ReceiveTimeout Socket option property to a reasonable value. The
          Receive() method will throw an Exception if the timeout is reached
          before data is received. Hope this helps solve your problem.

          Rich Blum - Author
          "C# Network Programming" (Sybex)

          "Network Performance Open Source Toolkit" (Wiley)

          Comment

          • a.kostrzewa

            #6
            Re: asynchronous socket operations - question

            I'm grateful for help.
            But in Yours solution I can't send any message after timeout on receive
            (socket is closed!).
            This way I would like to avoid Close() method.
            Unfortunely I can't use alternatively method - blocking Receive().

            Olek

            Uzytkownik "Rich Blum" <rich_blum@juno .com> napisal w wiadomosci
            news:ccdac0da.0 310150615.79120 c9b@posting.goo gle.com...[color=blue]
            > "a.kostrzew a" <aX.kostrzewaX@ remove_this_and _X.uposX.comX.p lX> wrote in[/color]
            message news:<bmip02$n4 m$1@atlantis.ne ws.tpi.pl>...[color=blue][color=green]
            > > Yes, of course.
            > > I call asynchronous read operation in this way:
            > >
            > > IAsyncResult oResult = oSocket.BeginRe ceive(...);
            > > oWH = m_oResult .AsyncWaitHandl e;
            > > // oEventsHandler - my class object
            > > oEventsHandler. WaitForGroupEQ( Timeout.Infinit e ,nGroupId,out nEvent);
            > > int nNumberOfBytesR ead = ReadEnd();
            > >
            > > But I can't break this operation.
            > >[/color]
            > Olek -
            >
            > From what I can tell from your code it looks like you are blocking
            > the main thread until the EndReceive() callback method fires. This
            > somewhat defeats the purpose of using an asynchronous socket call.
            > Instead of waiting indefinitely for an answer, why don't you put a
            > reasonable timeout time in the WaitForGroupEQ method. When the timeout
            > is reached you know there wasn't a response. If you then Close() the
            > Socket object, the EndReceive() method will fire, and throw an
            > Exception. You should catch the Exception in a try-catch block and
            > handle it accordingly.
            >
            > Alternatively, you can use the blocking Receive() method, and set
            > the ReceiveTimeout Socket option property to a reasonable value. The
            > Receive() method will throw an Exception if the timeout is reached
            > before data is received. Hope this helps solve your problem.
            >
            > Rich Blum - Author
            > "C# Network Programming" (Sybex)
            > http://www.sybex.com/sybexbooks.nsf/Booklist/4176
            > "Network Performance Open Source Toolkit" (Wiley)
            > http://www.wiley.com/WileyCDA/WileyT...471433012.html[/color]


            Comment

            Working...