stop Socket.BeginReceive before comlpete

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

    #1

    stop Socket.BeginReceive before comlpete

    Hi,
    I am using sync and async operations on the same socket.
    generally I want the socket to wait on BeginReceive and to not block the
    object thread.
    but in some cases I want to stop the BeginReceive in the middle - Don't
    accept any data from it , and using regular Receive (I don't want the data
    will come to the BeginReceive byte buffer , instead of other buffer)
    then when I comlete some operaion , to return and call to the BeginReceive
    again.
    How can I do it ?
    I try to hold the AsynCallBack , and the IAsyncResult and call EndInvoke but
    it make exceptions:
    Fail: The async result object is null or of an unexpected type. at
    System.Runtime. Remoting.Proxie s.RealProxy.End InvokeHelper(Me ssage reqMsg,
    Boolean bProxyCase)

    at System.Runtime. Remoting.Proxie s.RemotingProxy .Invoke(Object NotUsed,
    MessageData& msgData)

    at System.AsyncCal lback.EndInvoke (IAsyncResult result)

    Thanks


  • William Stacey [MVP]

    #2
    Re: stop Socket.BeginRec eive before comlpete

    Man that seems like a very difficult and dangerous road to hoe. I would
    pick one or the other and stick with it. If you pick async, you can still
    turn that into a blocking call by wraping an reset even around it. I might
    refactor your code to just use BeginReceives and figure a way to just use
    that.

    --
    William Stacey [MVP]

    "semedao" <semedao@commun ity.nospamwrote in message
    news:OCdPB8dzGH A.2300@TK2MSFTN GP05.phx.gbl...
    | Hi,
    | I am using sync and async operations on the same socket.
    | generally I want the socket to wait on BeginReceive and to not block the
    | object thread.
    | but in some cases I want to stop the BeginReceive in the middle - Don't
    | accept any data from it , and using regular Receive (I don't want the data
    | will come to the BeginReceive byte buffer , instead of other buffer)
    | then when I comlete some operaion , to return and call to the BeginReceive
    | again.
    | How can I do it ?
    | I try to hold the AsynCallBack , and the IAsyncResult and call EndInvoke
    but
    | it make exceptions:
    | Fail: The async result object is null or of an unexpected type. at
    | System.Runtime. Remoting.Proxie s.RealProxy.End InvokeHelper(Me ssage reqMsg,
    | Boolean bProxyCase)
    |
    | at System.Runtime. Remoting.Proxie s.RemotingProxy .Invoke(Object NotUsed,
    | MessageData& msgData)
    |
    | at System.AsyncCal lback.EndInvoke (IAsyncResult result)
    |
    | Thanks
    |
    |


    Comment

    • James

      #3
      Re: stop Socket.BeginRec eive before comlpete

      Hi,
      You could try sync on another thread. but stick to either async or sync.

      James
      Our website audits serve as a comprehensive evaluation of your website's performance, usability, and overall effectiveness.


      "semedao" <semedao@commun ity.nospamwrote in message
      news:OCdPB8dzGH A.2300@TK2MSFTN GP05.phx.gbl...
      Hi,
      I am using sync and async operations on the same socket.
      generally I want the socket to wait on BeginReceive and to not block the
      object thread.
      but in some cases I want to stop the BeginReceive in the middle - Don't
      accept any data from it , and using regular Receive (I don't want the data
      will come to the BeginReceive byte buffer , instead of other buffer)
      then when I comlete some operaion , to return and call to the BeginReceive
      again.
      How can I do it ?
      I try to hold the AsynCallBack , and the IAsyncResult and call EndInvoke
      but it make exceptions:
      Fail: The async result object is null or of an unexpected type. at
      System.Runtime. Remoting.Proxie s.RealProxy.End InvokeHelper(Me ssage reqMsg,
      Boolean bProxyCase)
      >
      at System.Runtime. Remoting.Proxie s.RemotingProxy .Invoke(Object NotUsed,
      MessageData& msgData)
      >
      at System.AsyncCal lback.EndInvoke (IAsyncResult result)
      >
      Thanks
      >
      >

      Comment

      • semedao

        #4
        Re: stop Socket.BeginRec eive before comlpete

        ok , I move everything to sync programming , but I still remain with the
        problem:
        How to stop / abort call to the Receive method after I already call it ?
        and without call close / shutdown etc , I want the socket to remain open for
        my use.

        I try to put it on a new thread , but then I can't call sleep on the thread
        that call the receive , and the suspend/resume thread method are obsolete
        so what I can do ?

        "semedao" <semedao@commun ity.nospamwrote in message
        news:OCdPB8dzGH A.2300@TK2MSFTN GP05.phx.gbl...
        Hi,
        I am using sync and async operations on the same socket.
        generally I want the socket to wait on BeginReceive and to not block the
        object thread.
        but in some cases I want to stop the BeginReceive in the middle - Don't
        accept any data from it , and using regular Receive (I don't want the data
        will come to the BeginReceive byte buffer , instead of other buffer)
        then when I comlete some operaion , to return and call to the BeginReceive
        again.
        How can I do it ?
        I try to hold the AsynCallBack , and the IAsyncResult and call EndInvoke
        but it make exceptions:
        Fail: The async result object is null or of an unexpected type. at
        System.Runtime. Remoting.Proxie s.RealProxy.End InvokeHelper(Me ssage reqMsg,
        Boolean bProxyCase)
        >
        at System.Runtime. Remoting.Proxie s.RemotingProxy .Invoke(Object NotUsed,
        MessageData& msgData)
        >
        at System.AsyncCal lback.EndInvoke (IAsyncResult result)
        >
        Thanks
        >
        >

        Comment

        • Peter Huang [MSFT]

          #5
          Re: stop Socket.BeginRec eive before comlpete

          Hi Semedao,

          It seems that you are working on a complex solution, so if you can
          elaborate your concrete scenario detailed, we may understand it better.

          Based on my knowledge, once we start a blocked Receive call, we can not
          stop it, except ShutDown the socket.
          For your scenario, I understand you want to control the Receive procedure
          more acurately.
          So I think you may try to decrease the receive buffer and received count,
          so the Received call will work in a more responsive mode.
          e.g. every time we just receive one byte, and check to see if we need to do
          another receive or just block on an synchronous variable.

          public int Receive (
          byte[] buffer,
          int offset,
          int size,
          SocketFlags socketFlags
          )
          Socket.Receive Method (Byte[], Int32, Int32, SocketFlags, SocketError)
          http://msdn2.microsoft.com/en-us/library/ms145156.aspx

          Here is the pseudocode for an idea.
          NOTE: all the code is pseudocode.
          Thread t = new Thread(ThreadPr oc);

          void ThreadProc()
          {
          while(true)
          {
          event.WaitOne() ; //Event is a synchronous object.
          s.Receive(buf,0 ,1,SocketFlags. None); // Here we receive 1 byte per time,
          but we can adjust it based on my concrete scenario.
          //If we want to do another receive immediately
          //event.Set();
          //Otherwise, we did not nothing, but we must have another method in another
          thread to set the event according to the concrete scenario.
          }
          }


          Best regards,

          Peter Huang

          Microsoft Online Community Support
          =============== =============== =============== =====
          When responding to posts, please "Reply to Group" via your newsreader so
          that others may learn and benefit from your issue.
          =============== =============== =============== =====
          This posting is provided "AS IS" with no warranties, and confers no rights.

          Comment

          • semedao

            #6
            Re: stop Socket.BeginRec eive before comlpete

            Hi peter,
            I try to implement P2P hole punching.
            so I must hole live connection to the server
            from one side wait for server "push" me some messages
            but when I enter inside operation with the server , I want to stop during
            this operation to listen for server messages , than , when I finish the
            operation , I return back to the receive mode.

            what i decide to do now is to use other thread for the receive mode , but
            because I can't suspend it , I call abort and catch the exception... I think
            it's ugly solution , but I can't find other for now.

            receiving one byte will not solve the problem.. , only if I will tell the
            server to send 1 byte for signaling...
            I afraid that it will be more complex solution , mix the server with the
            client problems...
            thanks
            ""Peter Huang" [MSFT]" <v-phuang@online.m icrosoft.comwro te in message
            news:HOaBbd%23z GHA.228@TK2MSFT NGXA01.phx.gbl. ..
            Hi Semedao,
            >
            It seems that you are working on a complex solution, so if you can
            elaborate your concrete scenario detailed, we may understand it better.
            >
            Based on my knowledge, once we start a blocked Receive call, we can not
            stop it, except ShutDown the socket.
            For your scenario, I understand you want to control the Receive procedure
            more acurately.
            So I think you may try to decrease the receive buffer and received count,
            so the Received call will work in a more responsive mode.
            e.g. every time we just receive one byte, and check to see if we need to
            do
            another receive or just block on an synchronous variable.
            >
            public int Receive (
            byte[] buffer,
            int offset,
            int size,
            SocketFlags socketFlags
            )
            Socket.Receive Method (Byte[], Int32, Int32, SocketFlags, SocketError)
            http://msdn2.microsoft.com/en-us/library/ms145156.aspx
            >
            Here is the pseudocode for an idea.
            NOTE: all the code is pseudocode.
            Thread t = new Thread(ThreadPr oc);
            >
            void ThreadProc()
            {
            while(true)
            {
            event.WaitOne() ; //Event is a synchronous object.
            s.Receive(buf,0 ,1,SocketFlags. None); // Here we receive 1 byte per time,
            but we can adjust it based on my concrete scenario.
            //If we want to do another receive immediately
            //event.Set();
            //Otherwise, we did not nothing, but we must have another method in
            another
            thread to set the event according to the concrete scenario.
            }
            }
            >
            >
            Best regards,
            >
            Peter Huang
            >
            Microsoft Online Community Support
            =============== =============== =============== =====
            When responding to posts, please "Reply to Group" via your newsreader so
            that others may learn and benefit from your issue.
            =============== =============== =============== =====
            This posting is provided "AS IS" with no warranties, and confers no
            rights.
            >

            Comment

            • Peter Huang [MSFT]

              #7
              Re: stop Socket.BeginRec eive before comlpete

              Hi Semedao,

              Thanks for your information.
              So far I still have some confusion.
              It seems that you are useing TCP hole punching.
              Here is a document for your reference.
              Peer-to-Peer Communication Across Network Address Translators


              From the document, it seems that we need two sockets on one port for TCP
              hole punching.

              Also from your description, you will also need the socket to listen to the
              Server.(Here I understand you have two clients(doing P2P), and a Server who
              coordinate the two clients).
              Can you enlaborator how did you listen to the Server? or did you just want
              to receive data from Server?

              Also based on my knowledge, assume we established the TCP connection
              between the two clients without the Server, the following communication
              should be between client1 and client2.
              e.g.
              Assume the Socket serversocket the socket that communicate with client
              after listen/accept.

              Socket serversocket;
              Thread1()
              {
              serversocket.Re ceive();//it will block for incoming data.
              }
              Thread2()
              {
              serversocket.Se nd();//based on my test, even if serversocket is blocked on
              receive, it still can send data.
              }

              So I just wonder why you need to interrupte the Thread1 that the
              serversocket.Re ceive?

              Can you show some code about you stop the thread1 that call socket.Receive
              and use that socket to listen to the Server messge?

              If you have any concern, you may send an Email to me via removing "online"
              from my email address.
              Thanks!



              Best regards,

              Peter Huang

              Microsoft Online Community Support
              =============== =============== =============== =====
              When responding to posts, please "Reply to Group" via your newsreader so
              that others may learn and benefit from your issue.
              =============== =============== =============== =====
              This posting is provided "AS IS" with no warranties, and confers no rights.

              Comment

              • semedao

                #8
                Re: stop Socket.BeginRec eive before comlpete

                Hi , I try to replay only foryou , but I got:
                "Returned mail: Host unknown"

                ""Peter Huang" [MSFT]" <v-phuang@online.m icrosoft.comwro te in message
                news:%23YsjOiL0 GHA.5212@TK2MSF TNGXA01.phx.gbl ...
                Hi Semedao,
                >
                Thanks for your information.
                So far I still have some confusion.
                It seems that you are useing TCP hole punching.
                Here is a document for your reference.
                Peer-to-Peer Communication Across Network Address Translators

                >
                From the document, it seems that we need two sockets on one port for TCP
                hole punching.
                >
                Also from your description, you will also need the socket to listen to the
                Server.(Here I understand you have two clients(doing P2P), and a Server
                who
                coordinate the two clients).
                Can you enlaborator how did you listen to the Server? or did you just want
                to receive data from Server?
                >
                Also based on my knowledge, assume we established the TCP connection
                between the two clients without the Server, the following communication
                should be between client1 and client2.
                e.g.
                Assume the Socket serversocket the socket that communicate with client
                after listen/accept.
                >
                Socket serversocket;
                Thread1()
                {
                serversocket.Re ceive();//it will block for incoming data.
                }
                Thread2()
                {
                serversocket.Se nd();//based on my test, even if serversocket is blocked on
                receive, it still can send data.
                }
                >
                So I just wonder why you need to interrupte the Thread1 that the
                serversocket.Re ceive?
                >
                Can you show some code about you stop the thread1 that call socket.Receive
                and use that socket to listen to the Server messge?
                >
                If you have any concern, you may send an Email to me via removing "online"
                from my email address.
                Thanks!
                >
                >
                >
                Best regards,
                >
                Peter Huang
                >
                Microsoft Online Community Support
                =============== =============== =============== =====
                When responding to posts, please "Reply to Group" via your newsreader so
                that others may learn and benefit from your issue.
                =============== =============== =============== =====
                This posting is provided "AS IS" with no warranties, and confers no
                rights.
                >

                Comment

                • Peter Huang [MSFT]

                  #9
                  Re: stop Socket.BeginRec eive before comlpete

                  Hi Semedao,

                  If you have any concern to expose the detailed information.
                  You can Send a Email to me directly.
                  NOTE: the My Email Address in the newsgroup is not valid, you need to
                  remove the "online" from my Email Address, and you can reach me.

                  Thanks.



                  Best regards,

                  Peter Huang

                  Microsoft Online Community Support
                  =============== =============== =============== =====
                  When responding to posts, please "Reply to Group" via your newsreader so
                  that others may learn and benefit from your issue.
                  =============== =============== =============== =====
                  This posting is provided "AS IS" with no warranties, and confers no rights.

                  Comment

                  • Peter Huang [MSFT]

                    #10
                    Re: stop Socket.BeginRec eive before comlpete

                    Hi Semedao,

                    I did not receive your mail so far.
                    If you have any concern, please feel free to let me know.

                    Best regards,

                    Peter Huang

                    Microsoft Online Community Support
                    =============== =============== =============== =====
                    When responding to posts, please "Reply to Group" via your newsreader so
                    that others may learn and benefit from your issue.
                    =============== =============== =============== =====
                    This posting is provided "AS IS" with no warranties, and confers no rights.

                    Comment

                    Working...