3

I have a System.Net socket that's spontaneously disconnecting, especially if I query its state TWICE in the Visual Studio 2010 debugger.

It's instantiated like this:

 _TCPConn = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

Later I connect and set up a loop to watch its "Connected" property . . .

     _TCPConn.Connect(this.IPAddress, REMOTE_TCP_PORT);

      // loop to figure out why / when we're disconnecting ...
      int loops;
      bool bState = false;
      for (loops = 0; loops < 100; loops++)
      {
          bState = _TCPConn.Connected;
          if (!_TCPConn.Connected) 
          {
              break;
          }
          Thread.Sleep(1000);
      }

Immediately after the Connect call its Connected property is true in the debugger and a network sniffer shows the successful connection with the host.

But if I examine the Connected property a second time in the debugger it shows as false.

If I don't set any breakpoints in the loop it gets all the way through the loop (100 seconds) and when it falls out the Connected property is true the first time I look at in the debugger and false the next time.

If I let it loop for 10 or 15 seconds and breakpoint, it will be true the first time I look in the debugger and false the second time I look at it and it will then fall out of the loop at that point, proving that it's not just an artifact of the debugger - it really is set to false.

When it disconnects there is no associated activity on the network sniffer, i.e., it's not being disconnected by the host.

This is a single-threaded application. I started looking at this because later on when I tried to use my socket I was often finding it disconnected and was trying to figure out why.

user316117
  • 7,971
  • 20
  • 83
  • 158
  • So you've proven that the problem you detected earlier is real. You still haven't found what's causing the disconnect. Something environmental, start with things like anti-malware and firewall. – Hans Passant Jan 04 '13 at 20:38
  • 1
    I still think it's something I'm doing. System.Net Sockets are widely used for all sorts of things so if there was a firewall or antimalware spontaneously disconnecting sockets I would expect to see lots of things going haywire. FWIW the program I'm writing is a replacement for an ancient VB6 program that uses Winsock and it runs just fine connecting to the same IP address and port. Another datapoint that says it's my code is that I can reproduce it on both Win7 and XP PC's running different firewall and antimalware. – user316117 Jan 04 '13 at 20:57
  • I thought about it being the debugger but remember that what made me look at this in the debugger in the first place was that it had spontaneously disconnected. Still, I'm putting this code on a thumbdrive and taking it home with me tonight to try it on a slightly different debugger I have at home to rule that out. – user316117 Jan 04 '13 at 21:02
  • Have you tried pining the socket occasionally to see if it still disconnects? Interesting post here: http://stackoverflow.com/questions/11712425/do-tcp-sockets-automatically-close-after-some-time-if-no-data-is-sent. Though it does mention a large timeout period so it's likely not this - worth a look still. – Tony Day Jan 07 '13 at 14:00
  • What are you connecting to? Can you read or write data? – usr Jan 07 '13 at 14:02

1 Answers1

-1

The problem appears to be a bug in the Visual Studio 2010 Express debugger.

As I mentioned in a comment above, this weird behavior of getting a spontaneous disconnect on the SECOND touch in the debugger was occurring on two separate PC's - an XP machine and a Win 7 PC. To add more detail, the XP machine was connecting to a piece of industrial equipment and the Win7 PC was connecting to the localhost. Both PC's were running Visual C# 2010 Express.

So I then tried it with the full pro version of Visual Studio 2010 and encountered no such behavior.

The original disconnect problem that caused me to start looking in the debugger in the first place was then easy to find once the debugger was no longer disconnecting me. I think this was one of the weirdest problems I've ever seen in 30 years of programming.

user316117
  • 7,971
  • 20
  • 83
  • 158