C
ColoradoGeiger
I have a fairly standard server application that I am using
asynchronous socket calls to make connections, send and receive data,
and all of that jazz. No blocking methods was my goal in writing this
and it's gone very well so far. But coming from the blocking world, I
want to have some timeouts on my receive routines and I after reading
a lot on MSDN and here, I get the impression that once I set one of
these Begin... methods a running and nothing connects or happens that
there is nothing I can do about canceling it. Is this really true?
For example, I make a connection, I send a packet of data, and then I
go and run .BeginRead(...). Lets say I know that my client will
respond in less than 4 seconds. If there is no response, then I can
be sure that I need to resend my packet - maybe it didn't get there,
maybe there were collisions on the technology on the other end, who
knows. All I know is that I need to retry. But I don't see any way
to detect a timeout using async.
The best method I have for this now: I use a class that contains the
socket and some other supporting data that I need to know that is all
built after the connection is made. This object is what I pass into
the .Begin... async calls and is returned to me when it completes. If
I set a timer in that class, I can manually close the socket while the
async call is still pending. I think this throws a SocketException or
something in my calling function for the .Begin... call and all is
well. Except it really isn't well because I don't have my connection
anymore, it's closed. I can't retry. I got out of my async call but I
don't have an open socket to retry with. Since the communications are
initiated by the clients, I have no direct control over when this
connection will be retried again.
Is there really no better way?
I have not included code, this is a more theory based question. If
anyone has an idea and needs to see some code, I can provide that no
problem.
Anyone have any ideas? Or should I start from scratch and start using
blocking methods which HAVE a timeout feature.
Jason
asynchronous socket calls to make connections, send and receive data,
and all of that jazz. No blocking methods was my goal in writing this
and it's gone very well so far. But coming from the blocking world, I
want to have some timeouts on my receive routines and I after reading
a lot on MSDN and here, I get the impression that once I set one of
these Begin... methods a running and nothing connects or happens that
there is nothing I can do about canceling it. Is this really true?
For example, I make a connection, I send a packet of data, and then I
go and run .BeginRead(...). Lets say I know that my client will
respond in less than 4 seconds. If there is no response, then I can
be sure that I need to resend my packet - maybe it didn't get there,
maybe there were collisions on the technology on the other end, who
knows. All I know is that I need to retry. But I don't see any way
to detect a timeout using async.
The best method I have for this now: I use a class that contains the
socket and some other supporting data that I need to know that is all
built after the connection is made. This object is what I pass into
the .Begin... async calls and is returned to me when it completes. If
I set a timer in that class, I can manually close the socket while the
async call is still pending. I think this throws a SocketException or
something in my calling function for the .Begin... call and all is
well. Except it really isn't well because I don't have my connection
anymore, it's closed. I can't retry. I got out of my async call but I
don't have an open socket to retry with. Since the communications are
initiated by the clients, I have no direct control over when this
connection will be retried again.
Is there really no better way?
I have not included code, this is a more theory based question. If
anyone has an idea and needs to see some code, I can provide that no
problem.
Anyone have any ideas? Or should I start from scratch and start using
blocking methods which HAVE a timeout feature.
Jason