从TCP服务器C#将数据发送到文本文件

时间:2018-12-12 14:14:07

标签: c# tcp uwp

下面是我拥有的TCP服务器程序,该程序从客户端打印实时数据。

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Net;
 using System.Net.Sockets;
 using System.IO;


public class EchoServer {
public static void Main() {
    TcpListener listener = null;
    byte[] datalength = new byte[4];

    try
    {
        listener = new TcpListener(IPAddress.Parse("127.0.0.1"), 1234);
        listener.Start();
        Console.WriteLine("TCP Server Has Started....");
        while (true)
        {
            TcpClient client = listener.AcceptTcpClient();
            StreamReader reader = new StreamReader(client.GetStream());
            StreamWriter writer = new StreamWriter(client.GetStream());
            string s = string.Empty;
            string b = string.Empty;
            s = reader.ReadLine();

            {
                Console.WriteLine(DateTime.Now.ToString());
                Console.WriteLine("From client -> Heart Rates : " + s);


            }
            reader.Close();
            writer.Close();
            client.Close();
        }
    }

    catch (Exception e)
    {
        Console.WriteLine(e.Message);
        Console.WriteLine(e);
        //Console.WriteLine(e.ToString());
    }
    finally
    {
        Console.WriteLine("Listener stopped");
        if (listener != null)
        {
            listener.Stop();
        }
      }
    }
  }

我想将这些数据保存到文本文件中。

关于我应该如何做的任何建议?

我正在寻找一种方式,当我关闭服务器程序并打开一个文本文件时,我应该能够看到所有正在打印的数据。

1 个答案:

答案 0 :(得分:0)

尝试这样:

using System;
using System.IO;
using System.Net;
using System.Net.Sockets;

public class EchoServer
{
    public static void Main()
    {
        TcpListener listener = null;
        byte[] datalength = new byte[4];

        try
        {
            listener = new TcpListener(IPAddress.Parse("127.0.0.1"), 1234);
            listener.Start();
            Console.WriteLine("TCP Server Has Started....");
            while (true)
            {
                TcpClient client = listener.AcceptTcpClient();
                StreamReader reader = new StreamReader(client.GetStream());
                StreamWriter writer = new StreamWriter(client.GetStream());
                string s = string.Empty;
                string b = string.Empty;
                s = reader.ReadLine();

                {
                    string FilePath = "Sample.txt";

                    File.AppendAllText(FilePath, DateTime.Now.ToString());
                    File.AppendAllText(FilePath, "From client -> Heart Rates : " + s);
                }
                reader.Close();
                writer.Close();
                client.Close();
            }
        }

        catch (Exception e)
        {
            Console.WriteLine(e.Message);
            Console.WriteLine(e);
            //Console.WriteLine(e.ToString());
        }
        finally
        {
            Console.WriteLine("Listener stopped");
            if (listener != null)
            {
                listener.Stop();
            }
        }
    }
}