港口滞留在TIME_WAIT [英] Port Stuck in Time_Wait

查看:214
本文介绍了港口滞留在TIME_WAIT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个TCP隧道在C#。我需要打开和关闭隧道这是我的服务器和客户机之间的应用程序。我用这个关闭数据连接来测试另一个应用程序。我必须使用特定的端口。



在第二,第三,第n个视我等多久才能重新连接时,我收到一个10048错误代码 - 地址已在使用结合我的套接字时。当关闭套接字,我也进行ShutDown.Both并清理出港口的希望关闭,但是当我在命令提示符下做了netstat的我仍然觉得在TIME_WAIT举行的端口。我还设置了插座没有留连。最后我试图让一个循环来检查端口的状态,但它在一个有点无尽的循环结束。我想那就是4分钟TIME_WAIT规则。



我有一个函数来显示nestat查询,我发现,当我运行和检查,直到港从建立到,我可以绑定TIME_WAIT去,但是当我用的是同样的数据从该查询到一个环路上绑定时的状态到达TIME_WAIT,我得到了10048有没有被我按一下按钮允许时间一瞬间,让我绑定?有没有TIME_WAIT之间的状态,并建立我打的循环,而不是当我点击按钮呢?我读TIME_WAIT应该完全结合阻止我,但这并不看起来是真实的。任何人都可以解释一下吗?



我向你道歉代码恋人。没有想到这会虽然改变任何东西。我只是需要更好地了解端口的状态。

 公共BOOL CheckAvailablePorts()
{
INT温度= 0;
布尔availPort = TRUE;
m_config =新的AppConfig();
如果
{
System.Diagnostics.Debug.WriteLine(错误加载配置文件退出......)(m_config.initialize()!);
返回FALSE;
}
,否则
{

至所有已设置为on

的foreach(ProxyConfig CFG连接的端口//检查m_config.m_proxyConfigs)
{
availPort = TRUE;
TEMP = cfg.localEP.Port;
DataView的DV = FindEstablishedSockets(); //返回查询的netstat
的foreach(DataRowView的rowView在DV)
{
的DataRow行= rowView.Row;

如果((Convert.ToInt32(行[本地端口]的ToString())==温度)及&安培;(行[状态]的ToString()等于(。成立)))
{
System.Diagnostics.Debug.WriteLine(端口:+温度+仍然锁定);
availPort = FALSE;
中断;
}
}
}
返回availPort;
}
}

//片段哪些检查可用性更大的功能,然后,如果假睡觉,再次运行

布尔TEMP =假; (!TEMP)
,而
{
TEMP = monitor.CheckAvailablePorts();
System.Threading.Thread.Sleep(2000);
}
System.Threading.Thread.Sleep(3000);
monitor.startApplication(); //启动所有绑定


解决方案

我读TIME_WAIT应该完全结合阻止我,但这并不看起来是真实的。




有一个选项可以使用这将允许您绑定一个本地端口是在TIME_WAIT。这是非常有用的,以确保你没有重新启动前杀死了服务器后,要等待4分钟。

  INT标志= 1; 
的sockfd =插座(...);
的setsockopt(的sockfd,SOL_SOCKET,SO_REUSEADDR,&安培;标志的sizeof(标志));
绑定(...);


I have a TCP Tunnel in C#. I need to open and close the tunnel which is my app between a server and a client. I'm using this to close the data connection to test out another app. I have to use particular ports.

On the second, third, nth connection depending on how long I wait to reconnect, I receive a 10048 error code - "Address already in use" when binding my socket. When closing the sockets, I do perform ShutDown.Both and Close in hopes of clearing out the ports, but when I do a netstat in a command prompt I still find the ports held in TIME_WAIT. I've also set the sockets to no linger. Lastly I tried to make a loop to check the status of the port, but it ends in a somewhat endless loop. I'm thinking it's that 4 minute TIME_WAIT rule.

I have a function to display a nestat query and I find that when I run that and check until the port goes from ESTABLISHED and into TIME_WAIT that I can bind, but when I use the same data from this query to bind on a loop when the status reaches TIME_WAIT, I get a 10048. Is there a brief moment in time allowed by my button click that allows me to bind? Is there a state between TIME_WAIT and ESTABLISHED I'm hitting in the loop and not when I do it with button clicks? I read TIME_WAIT should stop me from binding altogether, but this does not appear to be true. Can anybody explain this?

I apologize to you code lovers. Not thinking this will change anything though. I just need a better understanding of port states.

    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

解决方案

I read TIME_WAIT should stop me from binding altogether, but this does not appear to be true.

There is an option you can use that will allow you to bind a local port that is in TIME_WAIT. This is very useful to ensure you don't have to wait 4 minutes after killing a server before restarting it.

int flag = 1;
sockfd = socket(...);
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag));
bind(...);

这篇关于港口滞留在TIME_WAIT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆