views:

470

answers:

2

Error: Unable to read data from the transport connection: A blocking operation was interrupted by a call to WSACancelBlockingCall

Situation

  1. There is a TCP Server
  2. My web application connects to this TCP Server

    Using the below code:

    TcpClientInfo = new TcpClient();
    _result = TcpClientInfo.BeginConnect(<serverAddress>,<portNumber>, null, null);
    bool success = _result.AsyncWaitHandle.WaitOne(20000, true);
    
    
    if (!success)
    {
        TcpClientInfo.Close();
        throw new Exception("Connection Timeout: Failed to establish connection.");
    }
    
    
    NetworkStreamInfo = TcpClientInfo.GetStream();
    NetworkStreamInfo.ReadTimeout = 20000;
    
  3. 2 Users use the same application from two different location to access information from this server at the SAME TIME

  4. Server takes around 2sec to reply
  5. Both Connect
  6. But One of the user gets above error "Unable to read data from the transport connection: A blocking operation was interrupted by a call to WSACancelBlockingCall" when trying to read data from stream

How can I resolve this issue?

  1. Use a better way of connecting to the server
  2. Can't because it's a server issue
    • if a server issue, how should the server handle request to avoid this problem
A: 

Just a thought.

If your server takes 2seconds to response, shouldn't the Timeout values be 2000, instead of 20000 (which is 20 seconds)? First argument for AsyncWaitHandle.WaitOne() is in milliseconds.

If you are waiting 20 seconds, may be your server is disconnecting you for being idle?

nsr81
I am making sure that once a valid response is received the TCP connection is disconnected. A new TCP connection is created for every request.
soldieraman
A: 

This looks Windows-specific to me, which isn't my strong point, but...

You don't show us the server code, only the client code. I can only assume, then, that your server code accepts a socket connection, does its magic, sends something back, and closes the client connection. If this is your case, then that's the problem.

The accept() call is a blocking one that waits for the next client connection attempt and binds to it. There may be a queue of connection attempts created and administered by the OS, but it can still only accept one connection at a time.

If you want to be able to handle multiple simultaneous requests, you have to change your server to call accept(), and when a new connection comes in, launch a worker thread/process to handle the request and go back to the top of the loop where the accept() is. So the main loop hands off the actual work to another thread/process so it can get back to the business of waiting for the next connection attempt.

Real server applications are more complex than this. They launch a bunch of "worker bee" threads/processes in a pool and reuse them for future requests. Web servers do this, for instance.

If my assumptions about your server code are wrong, please enlighten us as to what it looks like.

dj_segfault
Thank you for that. I did end up creating a server like you mentioned and got different errors. The error was due reuse of the Same TCP Object from a persistent Object in the application.
soldieraman