Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. [C# 2008] TcpClient Close method does not close TCP connection

[C# 2008] TcpClient Close method does not close TCP connection

Scheduled Pinned Locked Moved .NET (Core and Framework)
helpcsharpsysadminhardwarequestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    steve_9496613
    wrote on last edited by
    #1

    Greetings to all, my application runs in a Windows Embedded Compact 7 device and it uses Compact Framework 3.5. It establishes a TCP connection to a PLC and exchanges data with it. I open the connection in this way:

    mTcpCli = new TcpClient()
    mTcpCli.Connect(mIp, mPort);
    MBStream = mTcpCli.GetStream();

    Then I start the communication thread. If there is a communication error, I close the connection and I re-open it with the above code. To close the connection I do ALL this:

    MBStream.Close();
    MBStream.Dispose();
    mTcpCli.Client.Close();
    mTcpCli.Close();
    ((IDisposable)mTcpCli).Dispose();
    if (mTcpCli != null)
    {
    mTcpCli = null;
    }

    but it seems not to be enough... Suppose the communication error is cable disconnected, absolutely no way to communicate. When I re-open the connection the instruction:

    mTcpCli.Connect(mIp, mPort);

    should give an exception (SocketException) but it doesn't. Until the communication is interrupted, I could say that it is not a very serious problem because the application tries to communicate with the PLC, it fails and the "close-reopen connection" loop restart (with 10 seconds of pause between one retries). The true problem is when the communication error is solved: I re-connect the cable. In this situation the instruction:

    mTcpCli.Connect(mIp, mPort);

    doesn't give any exception, and should not, but it doesn't establish a connection so the application tries to communicate with the PLC, it fails and retries the connection. I takes 4-5 attempts to establish a TRUE connection, almost a minute. I looked for a solution in this and other forum and in MSDN and I found that there is a bug in the TcpClient Close method but the soution, close also the network stream, doesn't work. I tried to close and dispose all I could with no result. When I close the connection I need to be sure it is closed and I can't succesfully open it when the cable is disconnected! Anybody can help me? Thank you in advance. Steve

    L 1 Reply Last reply
    0
    • S steve_9496613

      Greetings to all, my application runs in a Windows Embedded Compact 7 device and it uses Compact Framework 3.5. It establishes a TCP connection to a PLC and exchanges data with it. I open the connection in this way:

      mTcpCli = new TcpClient()
      mTcpCli.Connect(mIp, mPort);
      MBStream = mTcpCli.GetStream();

      Then I start the communication thread. If there is a communication error, I close the connection and I re-open it with the above code. To close the connection I do ALL this:

      MBStream.Close();
      MBStream.Dispose();
      mTcpCli.Client.Close();
      mTcpCli.Close();
      ((IDisposable)mTcpCli).Dispose();
      if (mTcpCli != null)
      {
      mTcpCli = null;
      }

      but it seems not to be enough... Suppose the communication error is cable disconnected, absolutely no way to communicate. When I re-open the connection the instruction:

      mTcpCli.Connect(mIp, mPort);

      should give an exception (SocketException) but it doesn't. Until the communication is interrupted, I could say that it is not a very serious problem because the application tries to communicate with the PLC, it fails and the "close-reopen connection" loop restart (with 10 seconds of pause between one retries). The true problem is when the communication error is solved: I re-connect the cable. In this situation the instruction:

      mTcpCli.Connect(mIp, mPort);

      doesn't give any exception, and should not, but it doesn't establish a connection so the application tries to communicate with the PLC, it fails and retries the connection. I takes 4-5 attempts to establish a TRUE connection, almost a minute. I looked for a solution in this and other forum and in MSDN and I found that there is a bug in the TcpClient Close method but the soution, close also the network stream, doesn't work. I tried to close and dispose all I could with no result. When I close the connection I need to be sure it is closed and I can't succesfully open it when the cable is disconnected! Anybody can help me? Thank you in advance. Steve

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      See TcpClient.LingerState Property (System.Net.Sockets)[^].

      S 1 Reply Last reply
      0
      • L Lost User

        See TcpClient.LingerState Property (System.Net.Sockets)[^].

        S Offline
        S Offline
        steve_9496613
        wrote on last edited by
        #3

        Thank you Richard. I have modified the code to open the connection in this way:

        LingerOption lingerOption = new LingerOption(true, 0);
        mTcpCli = new TcpClient();
        mTcpCli.LingerState = lingerOption;
        mTcpCli.Connect(mIp, mPort);

        Unfortunately nothing has changed.

        L 1 Reply Last reply
        0
        • S steve_9496613

          Thank you Richard. I have modified the code to open the connection in this way:

          LingerOption lingerOption = new LingerOption(true, 0);
          mTcpCli = new TcpClient();
          mTcpCli.LingerState = lingerOption;
          mTcpCli.Connect(mIp, mPort);

          Unfortunately nothing has changed.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Sorry, that's the only feature I have ever found that helps in these cases. However, it is possible that the problem lies at the other end of the connection.

          S 1 Reply Last reply
          0
          • L Lost User

            Sorry, that's the only feature I have ever found that helps in these cases. However, it is possible that the problem lies at the other end of the connection.

            S Offline
            S Offline
            steve_9496613
            wrote on last edited by
            #5

            You've tried to help me, I can only thank you even if I have not solved the problem!! ...and now I've learned one more thing! I don't think that the problem is at the other end of the connection, ie at the PLC, for two reasons: 1- I have disconnected the cable so the PLC can do nothing; 2- there is another touch panel connected to the PLC, a standard HMI, and it works fine with disconnections and re-connections so the PLC seems to behave in the right way.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups