473,394 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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.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
9 7290
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: TP-Software | last post by:
Hi, This code doesn't seem to work it always says "there is more data" and also this method is only called once private void AsyncReadCallBack(IAsyncResult asyncResult) {
2
by: dream machine | last post by:
Hi all , with BegeinReceive I can build async method of Socket Class that Receive the data from the Socket Client . My question is , if I have this code that create 3 Receive Async Call : ...
0
by: J Brad | last post by:
Hi every body I wrote a Asynch Server using ManualResetEvent.Reset(), ManualResetEvent.Set(), ManualResetEvent.WaitOne() events I'm receiving a Message (Example: "Hello Word") without Terminator...
0
by: J Brad | last post by:
Hello I wrote a Asynch Server using ManualResetEvent.Reset(), ManualResetEvent.Set(), ManualResetEvent.WaitOne() events I'm receiving a Message (Example: "Hello Word") without Terminator (No Char...
6
by: Steve Richter | last post by:
I dont get the point of socket.BeginReceive and socket.EndReceive. As I understand it, BeginReceive will start a 2nd thread, call the ReceiveCallback delegate in the 2nd thread, then block until...
0
by: Shoveler | last post by:
I've got an odd problem here that I've been beating my head on for days. I've written a class that uses a pre-established connection for communication, meaning I can use this class for a server or...
6
by: Pat B | last post by:
Hi, I'm writing my own implementation of the Gnutella P2P protocol using C#. I have implemented it using BeginReceive and EndReceive calls so as not to block when waiting for data from the...
0
by: george585 | last post by:
Hello! I am new to network programming, and understand just basics. Using some sample code, and having read documentation, I managed to create a simple app in C# and VB.NET. The application is...
21
by: puzzlecracker | last post by:
Problem: I send a lot of requests to the application (running on a different box, of course), and I receive back responses from the app . Below: socket corresponds to Socket socket=new...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.