BeginSend timeout?

  • Thread starter Thread starter Bob
  • Start date Start date
B

Bob

This question has been brought up before but I have yet to find a reasonable
answer. Can someone explain to me how to create a timeout for the BeginSend
method? I have seen where people say to create a timer and then have it
close the socket when the timer expires but this is not a true socket
timeout. The timeout should be on the socket receiving a response from the
remote host not how long the entire socket operation takes. If I pass in a
large byte array to BeginSend and data is being sent at 8 bytes a second but
the data is still being acknowledged then it should never timeout even
though the entire operation is taking longer then the timeout I specified.
Only if no ACK response is received for a certain amount of time should it
timeout.

Any one have any solutions to this?

Bob
 
Hello, Bob!

B> Any one have any solutions to this?

Instead of timer you can use IAsyncResult.AsyncWaitHandle.
You will wait on this handle for specified timeout.

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
 
Isn't the point of using BeingSend() that you get a call back and don't have
to use up a thread for waiting? If I am going to have to call Wait why not
just use the blocking socket in the first place?
 
Hello, Bob!

You can wait on another thread ( ThreadPool.RegisterWaitForSingleObject(...) ).
Yes, its an overhead to spawn new thread only to wait for completion...

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
 
Thanks for the info. My only problem is that I may have up to 500
connections active at once. I think I would run in to problems if I had
that many waits queued in the thread pool all at once. Not to mention I use
the thread pool to perform processing once that data is sent or received.

Is there any way to get the status of a socket while it is in the sending
state? For example I created a second thread that has an Array of every
connection that is active. The array contains the socket and the time the
operation started. Every so often I check the array to see how long an
operation has been running. If I was able to see what was happening to the
socket I could have this thread close the socket if I determined a timeout
occurred.

I know the IOControl method has a FIONREAD method that tells you data is
available to be read but it would be nice if there was a FIONWRITE to
determine how much data still needed to be written.


Thanks of any suggestions.
 
Not sure if this will work with async ops, you can try
Socket.Poll(...)
If the socket is writable because the writing is done then the call back
will be called because the operation would have completed. I need to know
how much data has yet to be sent over the wire while its still in progress.
That is you, who control how much data to write. If internal socket buffer
is not
large enough to store the data you want to send, then EndSend/Send will
return the
number of bytes that were actually written to driver buffer. You, as a
developer,
have to check EndSend/Send return value.

Again I will only know this when the send is complete. I need to know how
much has been sent compared to how much still needs to be sent while the
operation is still in progress.
---

So far it looks like my only solution will be to only pass enough data to
the BeginSent method to fill a single packet. Then if that packet is not
sent after a cetain amount of time I know a real timeout has occured. I
don't know what kind of performance impact this will have but its seems the
only way it will work.
 
Back
Top