By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,972 Members | 844 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,972 IT Pros & Developers. It's quick & easy.

stop Socket.BeginReceive before comlpete

P: n/a
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.Proxies.RealProxy.EndInvok eHelper(Message reqMsg,
Boolean bProxyCase)

at System.Runtime.Remoting.Proxies.RemotingProxy.Invo ke(Object NotUsed,
MessageData& msgData)

at System.AsyncCallback.EndInvoke(IAsyncResult result)

Thanks
Sep 1 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
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" <se*****@community.nospamwrote in message
news:OC**************@TK2MSFTNGP05.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.Proxies.RealProxy.EndInvok eHelper(Message reqMsg,
| Boolean bProxyCase)
|
| at System.Runtime.Remoting.Proxies.RemotingProxy.Invo ke(Object NotUsed,
| MessageData& msgData)
|
| at System.AsyncCallback.EndInvoke(IAsyncResult result)
|
| Thanks
|
|
Sep 1 '06 #2

P: n/a
Hi,
You could try sync on another thread. but stick to either async or sync.

James
http://www.tamarsolutions.co.uk

"semedao" <se*****@community.nospamwrote in message
news:OC**************@TK2MSFTNGP05.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.Proxies.RealProxy.EndInvok eHelper(Message reqMsg,
Boolean bProxyCase)

at System.Runtime.Remoting.Proxies.RemotingProxy.Invo ke(Object NotUsed,
MessageData& msgData)

at System.AsyncCallback.EndInvoke(IAsyncResult result)

Thanks


Sep 1 '06 #3

P: n/a
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" <se*****@community.nospamwrote in message
news:OC**************@TK2MSFTNGP05.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.Proxies.RealProxy.EndInvok eHelper(Message reqMsg,
Boolean bProxyCase)

at System.Runtime.Remoting.Proxies.RemotingProxy.Invo ke(Object NotUsed,
MessageData& msgData)

at System.AsyncCallback.EndInvoke(IAsyncResult result)

Thanks


Sep 2 '06 #4

P: n/a
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(ThreadProc);

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.

Sep 4 '06 #5

P: n/a
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-******@online.microsoft.comwrote in message
news:HO***************@TK2MSFTNGXA01.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(ThreadProc);

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.

Sep 4 '06 #6

P: n/a
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
http://www.brynosaurus.com/pub/net/p2pnat/

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.Receive();//it will block for incoming data.
}
Thread2()
{
serversocket.Send();//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.Receive?

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.

Sep 5 '06 #7

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

""Peter Huang" [MSFT]" <v-******@online.microsoft.comwrote in message
news:%2****************@TK2MSFTNGXA01.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
http://www.brynosaurus.com/pub/net/p2pnat/

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.Receive();//it will block for incoming data.
}
Thread2()
{
serversocket.Send();//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.Receive?

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.

Sep 5 '06 #8

P: n/a
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.

Sep 6 '06 #9

P: n/a
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.

Sep 8 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.