通过TCP/IP聆听数据,然后以XML的形式写入数据

发布于 2025-02-05 13:38:01 字数 994 浏览 1 评论 0原文

我们的客户有一台使用以太网TCP/IP协议和数据传输协议的透析机是XML。它自动连接到我们的 计算机系统通过使用配置的IP地址和端口号,并每60秒向我们的系统发送未经请求的数据消息。客户端以XML数据流发送数据。现在,我需要将数据存储在XML文件中。

对于上述要求,我将TCP/IP侦听器(.EXE)作为Destop应用程序开发。以下是代码:

        server = new TcpListener(localAddr, port);
        server.Start();
        while (true)
        {
            string FileName = "NiproMI" + DateTime.Now.ToString().Replace(" ","_") + ".xml";
            NiproEventsLog.WriteLog(FileName);
            NiproEventsLog.WriteLog("Waiting for a connection... ");
            TcpClient client = server.AcceptTcpClient();
            NiproEventsLog.WriteLog("Connected!");
            NetworkStream stream = client.GetStream();
            StreamReader sr = new StreamReader(stream);
            string fileContent = sr.ReadToEnd();
            File.WriteAllText(Path.Combine(GetAppConfigValues.GetAppConfigValue("XmlFilePath"), FileName), fileContent);
            client.Close();
        }
            

这是正确的方式还是有更好的方法? 请帮助。

Our Client have a dialysis machine which use Ethernet TCP/IP Protocol and data transfer protocol is XML.It automatically connects to the our
computer system by using the configured IP address and port number and sends unsolicited data messages every 60 seconds to our system. Client send data as XML Data Stream. Now I need to store data in XML file.

For above requirement I developed a TCP/IP listener(.exe) as destop app. Below is code:

        server = new TcpListener(localAddr, port);
        server.Start();
        while (true)
        {
            string FileName = "NiproMI" + DateTime.Now.ToString().Replace(" ","_") + ".xml";
            NiproEventsLog.WriteLog(FileName);
            NiproEventsLog.WriteLog("Waiting for a connection... ");
            TcpClient client = server.AcceptTcpClient();
            NiproEventsLog.WriteLog("Connected!");
            NetworkStream stream = client.GetStream();
            StreamReader sr = new StreamReader(stream);
            string fileContent = sr.ReadToEnd();
            File.WriteAllText(Path.Combine(GetAppConfigValues.GetAppConfigValue("XmlFilePath"), FileName), fileContent);
            client.Close();
        }
            

Is this right way or is there is any better way?
kindly help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文