C# - TcpClient - 无法建立连接,因为目标机器主动拒绝它
C# - TcpClient - No connection could be made because the target machine actively refused it
您好,我目前每分钟都在使用 Zebra 打印机创建一个新连接,它可以正常工作 5 到 8 个小时。之后我得到了 "No connection could be made because the target machine actively refused it" 错误。一旦出现此错误,就无法重新连接打印机,直到我完全关闭并启动 Zebra 打印机。
我没有正确关闭我的 tcpClient steam,或者在大约 300 次重新连接后无法导致错误出现的原因
我的代码:
//CLASS MEMBERS
private TcpClient client = new TcpClient();
private Timer TimZebraTimeOut;
private int ZebraTimeOutTime = 60000;
private bool NewConnectionNeeded = true;
//NORMALY CALLED IN CONSTRUCTOR
TimZebraTimeOut = new Timer(TimZebraTimeOutCallback, null,this.ZebraTimeOutTime, Timeout.Infinite);
//CLASS METHODS
private void CreateNewConnectionIfComTimeOut
{
try
{
if (this.NewConnectionNeeded ) // If time is out Create new connection with Zebra Printer
{
//Close last connexion open
client.Client.Close();
//Create a new one
client = new TcpClient();
// Add Read/Write TimeOut
client.SendTimeout = 10000;
client.ReceiveTimeout = 10000;
//Create TCP Connection
client.Connect(this.config.IpAddress, this.config.Port);
// Connection with PrinterEstablished
this.NewConnectionNeeded = false;
this.iLog.Info("CommunicationWithZebraPrinter : New connection With Labeler Established "); // If New connection not caused by timeout
}
}
catch (Exception exception)
{
// Write exception in Log
this.iLog.Error("CommunicationWithZebraPrinter : Can't establish a connection with printer: " + exception.Message.ToString());
}
}
// Zebra Communication Timer interrupt callback function
private void TimZebraTimeOutCallback(Object state)
{
// Flag That ZebraCom have timeout
this.NewConnectionNeeded= true;
//Restart Zebra Com TimeOut
TimZebraTimeOut.Change(this.ZebraTimeOutTime, Timeout.Infinite);
}
非常感谢
丹尼尔
Netstat 视图:
建立新连接时,3 个本地端口递增。
我的问题是 Zebra 打印机默认有 300 秒的连接超时,我每分钟都在创建一个新连接。在短暂的持续时间(6 小时)后,我的打印机连接缓冲区溢出。我只是将打印机超时更改为 1 分钟并轮询打印机以检测断开连接。
您好,我目前每分钟都在使用 Zebra 打印机创建一个新连接,它可以正常工作 5 到 8 个小时。之后我得到了 "No connection could be made because the target machine actively refused it" 错误。一旦出现此错误,就无法重新连接打印机,直到我完全关闭并启动 Zebra 打印机。
我没有正确关闭我的 tcpClient steam,或者在大约 300 次重新连接后无法导致错误出现的原因
我的代码:
//CLASS MEMBERS
private TcpClient client = new TcpClient();
private Timer TimZebraTimeOut;
private int ZebraTimeOutTime = 60000;
private bool NewConnectionNeeded = true;
//NORMALY CALLED IN CONSTRUCTOR
TimZebraTimeOut = new Timer(TimZebraTimeOutCallback, null,this.ZebraTimeOutTime, Timeout.Infinite);
//CLASS METHODS
private void CreateNewConnectionIfComTimeOut
{
try
{
if (this.NewConnectionNeeded ) // If time is out Create new connection with Zebra Printer
{
//Close last connexion open
client.Client.Close();
//Create a new one
client = new TcpClient();
// Add Read/Write TimeOut
client.SendTimeout = 10000;
client.ReceiveTimeout = 10000;
//Create TCP Connection
client.Connect(this.config.IpAddress, this.config.Port);
// Connection with PrinterEstablished
this.NewConnectionNeeded = false;
this.iLog.Info("CommunicationWithZebraPrinter : New connection With Labeler Established "); // If New connection not caused by timeout
}
}
catch (Exception exception)
{
// Write exception in Log
this.iLog.Error("CommunicationWithZebraPrinter : Can't establish a connection with printer: " + exception.Message.ToString());
}
}
// Zebra Communication Timer interrupt callback function
private void TimZebraTimeOutCallback(Object state)
{
// Flag That ZebraCom have timeout
this.NewConnectionNeeded= true;
//Restart Zebra Com TimeOut
TimZebraTimeOut.Change(this.ZebraTimeOutTime, Timeout.Infinite);
}
非常感谢
丹尼尔
Netstat 视图:
建立新连接时,3 个本地端口递增。
我的问题是 Zebra 打印机默认有 300 秒的连接超时,我每分钟都在创建一个新连接。在短暂的持续时间(6 小时)后,我的打印机连接缓冲区溢出。我只是将打印机超时更改为 1 分钟并轮询打印机以检测断开连接。