通过TCP/IP聆听数据,然后以XML的形式写入数据
我们的客户有一台使用以太网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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论