asynchronous socket operations - question

A

a.kostrzewa

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
 
D

Daniel Bass

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

message 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
 
G

Girish Bharadwaj

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

a.kostrzewa

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

IAsyncResult oResult = oSocket.BeginReceive(...);
oWH = m_oResult .AsyncWaitHandle;
// oEventsHandler - my class object
oEventsHandler.WaitForGroupEQ(Timeout.Infinite ,nGroupId,out nEvent);
int nNumberOfBytesRead = ReadEnd();

But I can't break this operation.

Best regards,
Olek
 
R

Rich Blum

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

IAsyncResult oResult = oSocket.BeginReceive(...);
oWH = m_oResult .AsyncWaitHandle;
// oEventsHandler - my class object
oEventsHandler.WaitForGroupEQ(Timeout.Infinite ,nGroupId,out nEvent);
int nNumberOfBytesRead = ReadEnd();

But I can't break this operation.
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/WileyTitle/productCd-0471433012.html
 
A

a.kostrzewa

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top