.Net Socket closing too early

  • Thread starter Thread starter Jeremy S. Richardson
  • Start date Start date
J

Jeremy S. Richardson

Background:
I have a Legacy C++ application that sends binary data over a TCP
socket. Currently it's talking to a Perl script under Linux.
However, there's a need to port the receving application to Windows.
I rewrote the Perl script receiver as a .Net application in C#.

Problem:
The C# receiver fails to read the last few thousand bytes of data on 1
out of 10 transfers. There is a "socket closed by remote host"
exception on NetworkStream.EndRead().

Other Info:
I can confirm that the Legacy sender application sends all the data
before closing the socket. I'm transferring 1-10MB, and the failure
always happens when there are fewer bytes to transfer than the size of
buffer being used in the receiving application: with an 8K buffer, the
failure will happen with < 8K of data left.

The issue is not consistent. I haven't found any pattern to it.
Sometimes data will go through, but when I send the data again, it
fails. It also never fails this way with the old Perl script receiver
under Linux.

.... The socket shouldn't close on the receiver's end with data pending
to be read. Any ideas?

-Jeremy
 
Normally I would do it this way (in VB.NET):

Dim ns As NetworkStream
Dim numberRead As Integer
Dim buffer(1024) As Byte

'Open a prepare to read stream

Do
numberRead = ns.Read(buffer, 0, buffer.Length)
'process buffer
Loop While ns.DataAvailable

Note the fact that I am checking DataAvailable after the read, not
before the read. This is what I had to do to get around this problem.

HTH

David
 
The best way to debug this is to run a network sniffer like Netmon or
one of the third party products. And see who is closing the
connection.

It seems to me that the sender is rudely closing the connection. A
network sniff should compare if that is the case.

feroze
===========================
This posting is provided as-is. It provides no guarantees and confers
no rights.
 
The best way to debug this is to run a network sniffer like Netmon or
one of the third party products. And see who is closing the
connection.

It seems to me that the sender is rudely closing the connection. A
network sniff should compare if that is the case.

feroze
===========================
This posting is provided as-is. It provides no guarantees and confers
no rights.
 
Back
Top