[C# 2008] TcpClient Close method does not close TCP connection
-
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
-
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
-
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.
-
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.
-
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.
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.