C# 我的TCP侦听器发送数据,然后关闭。我不知道';我不知道为什么?

C# 我的TCP侦听器发送数据,然后关闭。我不知道';我不知道为什么?,c#,.net,callback,C#,.net,Callback,上面的代码可以工作,但是由于某种原因,每当我调用它并通过调试跟踪它时,它似乎不止一次调用connectToConsole()。我并不是在递归地调用它。我能做什么?寻求调试帮助的问题(“为什么此代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现这些问题所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参阅:。您永远不应该有一个空的catch all语句,除非在非常罕见的情况下,您100%确定自己知道自己在做什么。否则,当您的程序不工作时,您将不知道为什么,就像在本

上面的代码可以工作,但是由于某种原因,每当我调用它并通过调试跟踪它时,它似乎不止一次调用connectToConsole()。我并不是在递归地调用它。我能做什么?

寻求调试帮助的问题(“为什么此代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现这些问题所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参阅:。您永远不应该有一个空的catch all语句,除非在非常罕见的情况下,您100%确定自己知道自己在做什么。否则,当您的程序不工作时,您将不知道为什么,就像在本例中一样。因为您不仅抑制了可能有助于您进行故障排除的信息,而且还抑制了一开始就有问题的任何直接指示,然后您就不知所措了,因为您不知道的原因导致了其他东西不起作用。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Windows.Forms;
using System.Threading;
using System.Net;
using System.Net.Sockets;

namespace BLHClient
{
    class chCon
    {
        static TcpClient clientCon;
        static NetworkStream stream;
        public static bool currentlyConnected;

        static bool rDHasLoopedOnce = false;

        static string buffer = "";

        Thread retrieveData;

        string ip = "";
        int port = 0;

        public chCon(string ipAddress, int portNumber)
        {
            ip = ipAddress;
            port = portNumber;

            connectToConsole();
        }

        public void connectToConsole()//mitigates the connection
        {
            if (ip.Trim() == "")
                ip = "127.0.0.1";

            if (port == 0)
                port = 2647;

            try
            {
                clientCon = new TcpClient(ip, port);
                stream = clientCon.GetStream();

                //sendData("#101"); //first bit of data is sent on accepted client

                retrieveData = new Thread(receiveData_Stage1);
                retrieveData.Start();
            }

            catch (Exception e) // if the connection being naughty ;)
            {
                MessageBox.Show("Exception caught:\n" + e);
            }

        }

        public void disconnectFromConsole()
        {
            try
            {
                if (clientCon.Connected || clientCon.Connected == null)
                {
                    retrieveData.Abort();

                    byte[] msg = System.Text.Encoding.ASCII.GetBytes("#103");
                    stream.Write(msg, 0, msg.Length);
                    stream.Close();
                    clientCon.Close();
                }
                else
                {
                    MessageBox.Show("Cannot disconnect from a connection that has not started.");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error thrown during disconnection - UNKNOWN");
            }
        }

        public void sendData(string data)//sends data to the server
        {
            Byte[] dataConv = System.Text.Encoding.ASCII.GetBytes(data);

            stream.Write(dataConv, 0, dataConv.Length);

        }

        public static void receiveData_Stage1() //builds a buffer through a loop
        {
            try
            {
                while (true)
                {
                    Byte[] response = new Byte[256];
                    string responseData = String.Empty;

                    // Read the first batch of the TcpServer response bytes.
                    Int32 bytes = stream.Read(response, 0, response.Length);
                    responseData = System.Text.Encoding.ASCII.GetString(response);

                    if (responseData.Trim() == "\0")
                        responseData = "";

                    if (responseData.Trim() != "")
                        buffer = buffer + responseData + "\n";

                    if (rDHasLoopedOnce == false)
                        rDHasLoopedOnce = true;
                }
            }
            catch
            {
            }
        }

        public string receiveData_Stage2() //requests the buffer through a return value string
        {
            string bufferTemp;
            bufferTemp = buffer;

            buffer = string.Empty;

            return bufferTemp;
        }

        public bool isConnected()
        {
            return clientCon.Connected;
        }

        public bool hasLoopedOnce()
        {
            return rDHasLoopedOnce;
        }
    }
    /*
    while (true)
        {
            if (Program.currentlyConnected == true)
            {
                Thread.Sleep(100);
                buffer = Program.receiveData_Stage2();
                if (buffer != "")
                    richTxtBoxConsole.AppendText(buffer + "\n");
            }
            else
            {
                guiConsoleWriteLine("Connection is not active.");
                break;
            }

        }
     */

}