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

时间:2016-04-07 22:31:14

标签: c# .net callback

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;
            }

        }
     */

}

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

0 个答案:

没有答案