1

I am developing a client server application using sockets in C#, It is a multi Threading application for sure, the strange thing is the following:

The connection is closed from one thread when the remote client asks, in this exact moment the write operation throws this exception:

unable to write data to transport connection: a blocking operation was interrupted by a call to WSACancelblockingcall

Actually I am writing 64 byte each 20 minutes, but the strange thing is that the above exception is thrown each time the connection is closed, that means the write operation is taking very very long time to complete to the extend that the operation is always stopped by the connection close, is this a problem in the network stack?, The more strange thing is that this problem happens only with one client, and still happening for more than a week,so it is not temporary.

Any suggestions? is this a hardware problem? is this a TCP protocol problem? any one had a similar problem and had found a solution?

EDIT:

pseudo code to illustrate the issue:

First thread:

while(true)
{
     if (some condition)
             close connection;
}

second thread:

while(true)
{
      sleep for 20 minutes
      write(array of 64 bytes) //blocks for a very long period of time
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
ammcom
  • 992
  • 1
  • 7
  • 24
  • http://stackoverflow.com/questions/9587738/unable-to-read-data-from-the-transport-connection-a-blocking-operation-was-inte –  Jul 30 '16 at 13:57
  • my problem is 'write' not 'read' , check the question more accurately please – ammcom Jul 30 '16 at 14:01
  • Read it again : "If you call a .Close() on any of you readers or writers to the underlying stream. and try to use that reader or writer afterwards, then you will get this error." –  Jul 30 '16 at 14:02
  • Are you also synchronizing your socket between first and second thread ? And ensure that during read/write, you socket is still open and not closed ? –  Jul 30 '16 at 14:04
  • I am syncing the threads for sure, more over I am checking if the socket is still connected before writing – ammcom Jul 30 '16 at 14:10
  • My problem is that the write is blocking for a long period, as I can say the same moment the connection closes the write throws the exception – ammcom Jul 30 '16 at 14:14

1 Answers1

0

Actually I have found the problem:

The write method of NetwrokStream blocks until it can successfully write data to the underlying socket buffer (although many people disagree with this but the experiment proves that it is correct). When the buffer gets full the write method blocks until some data is sent and there is a room for the data to be written to the buffer, but when the network connection is slow and has issues or when the reader reads data slowly the buffer gets full and write method blocks for a long period of time.

ammcom
  • 992
  • 1
  • 7
  • 24