Asynchronous connection not working after first failure

132 Views Asked by At

I am dealing with quite annoying issue, and I could not find any solution for this.

I am calling WebRequest Connection, working under Unity C#;

IAsyncResult startingTheCall =  webRequest.BeginGetRequestStream(new      AsyncCallback(GetRequestStreamCallback), parameters);

It sends call to server running on Windows; All works fine. But, if server is not turned off, connection waits for the response for eternity. So solve this, I add timeout:

ThreadPool.RegisterWaitForSingleObject(startingTheCall.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallbackOfFirstStream), parameters, 10000, true);

Timeout works fine too; The problem is, if I ever trigger the time out (by shutting the server down), then enabled server again, BeginGetRequestStream will never reach server no matter what, until I restart the application.

I thought, that maybe on failure, I am not cleaning connections properly. So I did set up this cleaning routine inside timeout:

        ArrayList unbox = (ArrayList)state;
        HttpWebRequest request = (HttpWebRequest)unbox[1];
        IAsyncResult asynchronousResult = (IAsyncResult)unbox[6];
        Stream postStream = request.EndGetRequestStream(asynchronousResult);
        postStream.Close();
        request.Abort();

I abort the request, close the stream. Still, after 1st failure, the server will never sent responses or get the stream messages, until I restart the application. Like it is completely blocked.

Anyone ever had this behaviour?

0

There are 0 best solutions below