473,804 Members | 3,461 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

stop Socket.BeginRec eive 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
Sep 1 '06 #1
9 7333
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*****@commun ity.nospamwrote in message
news:OC******** ******@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
|
|
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*****@commun ity.nospamwrote in message
news:OC******** ******@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


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*****@commun ity.nospamwrote in message
news:OC******** ******@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


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(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.

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.m icrosoft.comwro te in message
news:HO******** *******@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.

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.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.

Sep 5 '06 #7
Hi , I try to replay only foryou , but I got:
"Returned mail: Host unknown"

""Peter Huang" [MSFT]" <v-******@online.m icrosoft.comwro te in message
news:%2******** ********@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
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.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.

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
10484
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
9236
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 : <code> mySocket.BeginReceive(param1,param2,param3.....); mySocket.BeginReceive(param1,param2,param3......); mySocket.BeginReceive(param1,param2,param3......);
0
1738
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 (No Char or String Terminator like <EOF>) So When I Receive my Message ((Example: "Hello Word") ), My seconde call "handler.BeginReceive()" will not call the ReadCallback() delegate Is there any way to control the end of reading of the message...
0
1280
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 or String Terminator like <EOF>) So When I Receive my Message ((Example: "Hello Word") ), My seconde call "handler.BeginReceive()" will not call the ReadCallback() delegate Is there any way to control the end of reading of the message (without...
6
13153
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 the socket.EndReceive method called in the 2nd thread receives some data from the socket. If the 1st thread will block until the 2nd thread receives some data from the socket, what is the point of starting up the 2nd thread? I am aware I can...
0
2061
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 a client. After receiving a Socket that has been connected to a RemoteEP, it begins 2 threads by calling out Begin(). My Send Thread pauses until the class Parent wants to send data. Receive Thread keeps checking for incoming data. My...
6
7008
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 supernode. Everything I have written works fine sending and receiving uncompressed data. But now I want to implement compression using the deflate algorithm as the Gnutella protocol accepts: Accept-Encoding: deflate Content-Encoding: deflate in the...
0
3589
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 supposed to do the following: monitor ALL INCOMING TCP traffic on the local computer, and save certain parts of it as files - not log files though, but actual files that are sent to the computer as part of http or ftp. Basically if a user browse a page...
21
5674
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 Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
0
9705
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9576
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10567
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10323
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10310
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7613
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6847
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4291
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3809
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.