TcpClient - 如何正确连接,维护连接并最终断开连接

时间:2017-08-18 20:54:15

标签: c# tcpclient

我有这些方法:

    private void connectToServer() {
        client = new TcpClient(SERVER_IP, PORT_NO);
        nwStream = client.GetStream();

        writer = new StreamWriter(client.GetStream());
        writer.AutoFlush = true;
        connected = true;

        getDataFromServer();
        rtb_inputField.Select();

        if (getDataTimer == null) {
            getDataTimer = new System.Timers.Timer();
            getDataTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
            getDataTimer.Interval = 1000;
            getDataTimer.Enabled = true;
        }
    }

    private void disconnectFromServer() {
        if (connected) {
            writer.WriteLine("quit");
            getDataTimer.Enabled = false;
            getDataTimer.Dispose();
            getDataTimer = null;

            Thread.Sleep(1000);     //Wait 1 second
            nwStream.Close();
            client.Close();
            rtb_outputWindow.AppendText("\n\nClient: Disconnected.");
        }
        connected = false;
    }

    private void getDataFromServer() {
        if (connected) {
            new Thread(() => {
                Thread.CurrentThread.IsBackground = true;
                byte[] bytesToRead = new byte[client.ReceiveBufferSize];
                int readData = nwStream.Read(bytesToRead, 0, client.ReceiveBufferSize);
                updateOutputWindow(Encoding.Default.GetString(bytesToRead, 0, readData));
            }).Start();
        }
    }

    private void updateOutputWindow(string text) {
        string newText = string.Empty;

        if (InvokeRequired) {
            Invoke(new MethodInvoker(delegate () {
                updateOutputWindow(text);
            }));
        }
        else {
            newText = startRTFString;
            newText += rtb_outputWindow.Rtf;
            newText += replaceAnsiColorCodes(text);

            rtb_outputWindow.Rtf = newText;
        }
    }

    private void OnTimedEvent(object source, ElapsedEventArgs e) {
        if (connected) {
            getDataFromServer();
        }
    }

这一切都有效,但有些不对劲。当我断开连接时,在最终说"客户端:断开连接之前,获取了许多新行。"我与服务器保持连接的时间越长,断开连接所需的时间就越长。

我相信计时器与此问题有关。计时器的工作是连续地从服务器请求数据,并在收到任何数据时输出。有没有更好的方法呢?也许"等待服务器发送数据" - 如果几秒钟内没有发送任何内容,我就不必为了请求而锤击服务器。

垃圾收集器是否由getDataFromServer方法中的新线程负责?或者我需要以某种方式处理它? (因此,我不会最终创建一大堆后台线程。)

1 个答案:

答案 0 :(得分:0)

您不需要计时器来侦听传入的数据。这应该自动为您处理。每次调用侦听线程任务时,它应该获取任何传入数据,然后完成任务,以便其他线程可以执行。

这里我有一个来自我的项目的听力任务样本。传入的数据是序列化的字符串。

void ListeningObjectTask()// Recieves any data sent by the other user.
        {
            while (client != null && client.Connected)// If there is another user and they are connected.
            {
                try// This suppresses exceptions thrown by connection problems, such as the user losing network signal.
                {
                    recieve = str.ReadLine();// Gets the data sent by the other user.
                    if (recieve != "")// If the user has sent data.
                    {
                        lock (recievedStrings)// Locks the buffer to add the data sent by the other user to it.
                        {
                            recievedStrings.Enqueue(recieve);
                        }
                        recieve = "";// Clears the incoming data, as it has been stored.
                    }
                }
                catch (Exception Ex)
                {
                    // Ends all the other multiplayer objects as the connection has ended and they are no longer needed.
                    Disconnect();
                    // Tells the user why they have lost connection, so that they can try to fix the problem.
                    MessageBox.Show("Network connection ended\n" + Ex);
                }
            }
        }

以下是所用对象的声明:

private TcpClient client;// Allows connections for tcp network services.
private StreamReader str; // Provides an interface to read from a stream.
private string recieve;// The incoming string recieved from the other user.
readonly Queue<string> recievedStrings = new Queue<string>();// A buffer to hold incoming data from the other user.