端口被困在Time_Wait
我在C#中有一个TCP隧道。我需要打开和关闭隧道,这是我在服务器和客户端之间的应用程序。我正在使用它关闭数据连接以测试另一个应用程序。我必须使用特定的端口。
在第二个,第三个,第n个连接,取决于我等待重新连接多长时间,我收到一个10048错误代码 - “地址已经在使用”绑定我的套接字时。关闭套接字时,我确实执行ShutDown.Both和Close以期清除端口,但是当我在命令提示符下执行netstat时,我仍然发现在TIME_WAIT中保存的端口。我也设置插座没有逗留。最后,我尝试创建一个循环来检查端口的状态,但它以无限循环结束。我认为这是4分钟的TIME_WAIT规则。
我有一个函数来显示一个nestat查询,我发现当我运行它并检查,直到端口从ESTABLISHED进入TIME_WAIT我可以绑定,但是当我使用来自此查询的相同数据绑定到循环时状态达到TIME_WAIT,我得到10048.我的按钮点击是否允许我绑定一个短暂的时间?在TIME_WAIT和ESTABLISHED之间是否有一个状态我正在循环中而不是当我按下按钮点击时?我读过TIME_WAIT会阻止我完全绑定,但这似乎不是真的。任何人都能解释一下吗?
我向代码爱好者道歉。不认为这会改变任何事情。我只是需要更好地了解港口国家。
public bool CheckAvailablePorts()
{
int temp=0;
bool availPort= true;
m_config = new AppConfig();
if (!m_config.initialize())
{
System.Diagnostics.Debug.WriteLine("Error loading configuration file. Exiting...");
return false;
}
else
{
//checking through all the ports that have been set to connect on
foreach (ProxyConfig cfg in m_config.m_proxyConfigs)
{
availPort = true;
temp = cfg.localEP.Port;
DataView dv = FindEstablishedSockets();//returns netstat query
foreach (DataRowView rowView in dv)
{
DataRow row = rowView.Row;
if ((Convert.ToInt32(row["Local Port"].ToString()) == temp) && (row["Status"].ToString().Equals("Established")))
{
System.Diagnostics.Debug.WriteLine("Port: " + temp + " is still locked");
availPort = false;
break;
}
}
}
return availPort;
}
}
//snippet out of a bigger function which checks for availability and then sleeps if false and runs again
bool temp = false;
while (!temp)
{
temp = monitor.CheckAvailablePorts();
System.Threading.Thread.Sleep(2000);
}
System.Threading.Thread.Sleep(3000);
monitor.startApplication(); //starts all the binding
没有找到相关结果
已邀请:
3 个回复
澜悍景哭苟
,直到客户端套接字断开连接。
贡炮逗握惫
妊辽剁茧