473,320 Members | 1,982 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,320 software developers and data experts.

asynchronous socket operations - question

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
Nov 15 '05 #1
5 5615
You could always spawn a new thread which then calls the synchronous
version, into which you can place a timeout parameter.

"a.kostrzewa" <aX.kostrzewaX@remove_this_and_X.uposX.comX.plX> wrote in
message news:bm**********@nemesis.news.tpi.pl...
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

Nov 15 '05 #2
I might be wrong here.. but, can you call EndSend() to complete the
operation?

--
Girish Bharadwaj
"Daniel Bass" <I'm really @ sick of spam> wrote in message
news:O8**************@TK2MSFTNGP10.phx.gbl...
You could always spawn a new thread which then calls the synchronous
version, into which you can place a timeout parameter.

"a.kostrzewa" <aX.kostrzewaX@remove_this_and_X.uposX.comX.plX> wrote in
message news:bm**********@nemesis.news.tpi.pl...
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

Nov 15 '05 #3
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

Użytkownik "Girish Bharadwaj" <girishb.at.mvps.dot.org> napisał w wiadomo¶ci
news:OM**************@TK2MSFTNGP09.phx.gbl...
I might be wrong here.. but, can you call EndSend() to complete the
operation?

--
Girish Bharadwaj
"Daniel Bass" <I'm really @ sick of spam> wrote in message
news:O8**************@TK2MSFTNGP10.phx.gbl...
You could always spawn a new thread which then calls the synchronous
version, into which you can place a timeout parameter.

"a.kostrzewa" <aX.kostrzewaX@remove_this_and_X.uposX.comX.plX> wrote in
message news:bm**********@nemesis.news.tpi.pl...
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


Nov 15 '05 #4
"a.kostrzewa" <aX.kostrzewaX@remove_this_and_X.uposX.comX.plX> wrote in message news:<bm**********@atlantis.news.tpi.pl>...
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/WileyT...471433012.html
Nov 15 '05 #5
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

Uzytkownik "Rich Blum" <ri*******@juno.com> napisal w wiadomosci
news:cc**************************@posting.google.c om...
"a.kostrzewa" <aX.kostrzewaX@remove_this_and_X.uposX.comX.plX> wrote in

message news:<bm**********@atlantis.news.tpi.pl>...
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/WileyT...471433012.html

Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jacob | last post by:
I'm writing a class that communicates with a server using the TcpClient class. Most of the methods I've written are intended to be used synchronously and will block until they are completed. But...
3
by: Matthew King | last post by:
Hi all I've written a asynchronous socket client class, but i've found that in order to consume it I have to use events, and cannot simply for example SocketClient client = new...
4
by: Matthew Groch | last post by:
Hi all, I've got a server that handles a relatively high number of concurrent transactions (on the magnitude of 1000's per second). Client applications establish socket connections with the...
0
by: Richard | last post by:
Hi, I'm suffering a socket race condition - I think. The code works fine at full speed on a single CPU machine. However when I run full speed on a 2 Zeon machine the socket drops data on an...
2
by: Ronodev.Sen | last post by:
the way my program needs to go is -- 1) open a socket and listen on it 2) moment a client connects to the socket - process some data (by sending it to another machine), get the result and send...
2
by: Macca | last post by:
My app has an asynchronous socket server. It will have 20 clients connected to the server. Each client sends data every 500 millisecondsThe Connections once established will not be closed unless...
0
by: Macca | last post by:
Hi, I am writing an asychronous socket server to handle 20+ simulataneous connections. I have used the example in MSDN as a base. The code is shown at end of question. Each connection has a...
1
by: DaTurk | last post by:
Hi, Lets see, for arguements sake lets just say that I have a server, which site waiting to receive connections, it then has an array of sockets that are connected to it. It's receiving all of...
4
by: Engineerik | last post by:
I am trying to create a socket server which will listen for connections from multiple clients and call subroutines in a Fortran DLL and pass the results back to the client. The asynchronous socket...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.