本文介绍了如何断开处于Tcplistener模式的服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!



我有一台服务器正在端口上运行并正在等待连接。

连接部分工作正常但是当我点击断开连接时按钮,它实际上正在断开连接。

这是我的代码的一部分

Hi,
I have a server which is running on a port and is waiting for connections.
The connect part is working fine but when i click on "Disconnect" button, it is actually disconnecting.
Here is the part of my code

private void btnDisconnect_Click(object sender, EventArgs e)
        {
            if (btnDisconnect.Enabled == true)
            {
                btnDisconnect.BackColor = System.Drawing.Color.DarkBlue;
                btnDisconnect.ForeColor = System.Drawing.Color.AntiqueWhite;
                Listener l = new Listener(ipAddr);
                try
                {

                    txtLog.AppendText("Stopping listener");
                    l.disConnect();
                    txtLog.AppendText("stopped");
                }
                catch (Exception)
                {
                    txtLog.AppendText("Unhandled Listener Close");
                }
            }
        }





这是听众类



Here is the listener class

class Listener
    {
        private IPAddress ipAddress;
        private TcpClient tcpClient;
        private TcpListener tcpListener;
        private Thread thrListener;
        bool ServRunning = false;
        // The event and its argument will notify the form when a user has connected, disconnected, send message, etc.
        public static event StatusChangedEventHandler StatusChanged;
        private static StatusChangedEventArgs e;
        public Listener(IPAddress address)
        {
            ipAddress = address;
        }
        public static void OnStatusChanged(StatusChangedEventArgs e)
        {
            StatusChangedEventHandler statusHandler = StatusChanged;
            if (statusHandler != null)
            {
                // Invoke the delegate
                statusHandler(null, e);
            }
        }
        public void startListening()
        {

            // Get the IP of the first network device, however this can prove unreliable on certain configurations
            IPAddress ipaLocal = ipAddress;

            // Create the TCP listener object using the IP of the server and the specified port
            tcpListener = new TcpListener(ipaLocal, 13);

            // Start the TCP listener and listen for connections
            tcpListener.Start();

            // The while loop will check for true in this before checking for connections
            ServRunning = true;

            // Start the new tread that hosts the listener
            thrListener = new Thread(KeepListening);
            thrListener.Start();
        }

        private void KeepListening()
        {
            // While the server is running
            while (ServRunning == true)
            {
                // Accept a pending connection
                tcpClient = tcpListener.AcceptTcpClient();
                e = new StatusChangedEventArgs("Connected");
                OnStatusChanged(e);
                bool m = true;
                while (m)
                {
                    byte[] bytes = new byte[2560];
                    NetworkStream stream = tcpClient.GetStream();
                    stream.Read(bytes, 0, bytes.Length);
                    string mstrMessage = Encoding.ASCII.GetString(bytes, 0, bytes.Length);
                    //MessageBox.Show(mstrMessage);
                    e = new StatusChangedEventArgs(mstrMessage);
                    OnStatusChanged(e);
                    //SocketHelper helper = new SocketHelper();
                    //helper.processMsg(tcpClient, stream, bytes);
                    XmlDocument myxmlDoc = new XmlDocument();
                    myxmlDoc.LoadXml(mstrMessage);
                    string message = myxmlDoc.SelectSingleNode("Connect/@message").InnerText;
                    message = Regex.Replace(message, @"\t|\n|\r", "");
                    //MessageBox.Show(message);
                    if (message.Equals("send"))
                    {
                        DriveInfo1 d = new DriveInfo1();
                        string mstrDriveResponse = d.getDrive();
                        byte[]  bytesSent = Encoding.ASCII.GetBytes(mstrDriveResponse);
                        stream.Write(bytesSent, 0, bytesSent.Length);
                        //MessageBox.Show("Received Drive data \n" + mstrDriveResponse);
                        string mstrBuildResponse = d.Installed();
                        byte[] byteSent = Encoding.ASCII.GetBytes(mstrBuildResponse);
                        stream.Write(byteSent, 0, byteSent.Length);
                        //MessageBox.Show("Received Build data \n" + mstrBuildResponse);
                    }
                    else
                    {
                        string mstrResponse = @"<?xml version=""1.0"" encoding=""utf-8""?><Response message=""Unable to parse xml"">";
                        byte[] bytesSent = Encoding.ASCII.GetBytes(mstrResponse);
                        stream.Write(bytesSent, 0, bytesSent.Length);
                    }
                    m = false;
                    stream.Close();
                }
            }
        }
        public void disConnect()
        {
            tcpClient.Close();
            tcpListener.Stop();
        }
    }





请帮助纠正错误!!

谢谢:)



Please help to rectify the error!!
Thanks :)

推荐答案

public void disConnect()
{
    if(tcpClient !=null)
    {
        tcpClient.GetStream().Close();
        tcpClient.Close();
        tcpListener.Stop();
    }
}





阅读此KB以获取更多信息:

[]


这篇关于如何断开处于Tcplistener模式的服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 21:44