如果端口进入CLOSE_WAIT状态,是否可以终止进程? [英] Is it good to kill process if port goes in CLOSE_WAIT state?

查看:511
本文介绍了如果端口进入CLOSE_WAIT状态,是否可以终止进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究TcpListener和TcpClient并且相互沟通。

我正确地关闭了与此通信相关的所有对象。如下面的代码:



I am working on TcpListener and TcpClient and communicate with each other succsessfully.
I properly closes all objects related to this communication. like in below code:

string mess = "Alert";
                string caption = "Do you want to close Server and stop Application?";
                DialogResult dlgg = MessageBox.Show(caption, mess, MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                if (dlgg == DialogResult.Yes)
                {
                    if (networkStream != null)
                    {
                        networkStream.Close();
                        networkStream.Dispose();
                    }

                    else 
                    {
                       Process currprocess = Process.GetCurrentProcess();
                       currprocess.Kill();
                    }

                    if (this.clientSocket != null)
                    {
                        clientSocket.Close();
                    }
                    else { }

                    this.serverSocket.Stop();
                    
                    msg = "Disconnecting all clients , Server succsessfully Stopped!";
                    message();
                  
                    this.Close();
                    Application.Exit();
                }
                else { }





但是有时端口没有关闭,它进入close_wait状态(因为我发现它使用netstat在cmd.exe中)所以那时我再次启动应用程序并听取相同的端口客户端连接但应用程序说客户端没有连接。并且错误说每个套接字只允许使用一次。所以如果我杀了进程对我的应用程序有好处。或者给我建议来克服这个错误。这是我需要解决的非常重要的错误。



谢谢。



But sometimes port not closes and it goes in close_wait state (as i found it in cmd.exe using "netstat") so at that time if i again start application and listen to same port client get connected but application says client not connected. and error said "only one usage of each socket is permited". so if i kill process is it good for my application.Or give me suggestions to overcome this error. this is the very important error i need to solve.

thanks.

推荐答案

参见解释Close_Wait - Jane Lewis的博客 - 站点主页 - TechNet博客 [ ^ ]。


可以从不同方面理解它是好的这个问题。



可能会发生一些不好的事情如果我杀了某个进程,那么对于操作系统或另一方的通信应用程序?

否。您的应用程序应该能够安全地处理它,因为这种中断总是会发生在现实生活中。但这取决于你如何写它。



这是一种可接受的编程习惯吗?

绝对不是。如果你使用某些进程来杀死某些困难的情况,请考虑你的项目完全失败。



我无法告诉你如何修复你的代码,因为设计可能有误。请看我过去的答案;特别是,我描述了断开连接的问题:

如何知道AA Tcp连接是否结束 [ ^ ],

套接字编程中的业余问题 [ ^ ],

来自同一端口的多个客户端号码 [ ^ ] 。



-SA
The question "is it good" can be understood in different aspects.

"Can something bad happen to an OS or to a communicating application on the other side if I kill some process?"
No. Your application should be able to handle it safely, because such disruptions can always happen in real life. But it depends on how you write it.

"Is it an acceptable programming practice?"
Absolutely not. If you use killing of some process to go out of some difficult situation, consider your project totally failed.

I cannot tell you how you can "fix" your code, because it is probably wrong by design. Please see my past answers; in particular, I described the problem of disconnection:
How Do I Get To Know If A A Tcp Connection Is Over[^],
an amateur question in socket programming[^],
Multple clients from same port Number[^].

—SA


这篇关于如果端口进入CLOSE_WAIT状态,是否可以终止进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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