通过 TCP 读取 xml 数据

发布于 2024-09-11 22:16:18 字数 697 浏览 6 评论 0原文

我正在开发一个应用程序,它监听 TCP 以获取来自其他设备的一些 xml 数据。 我正在使用嗅探 C# 代码,我可以嗅探所有数据包。我的问题是,在每个数据包中我都可以在每个数据包中找到一段数据。 像这样:

1 packet from ip41 data:< 
2 packet from ip41 data:?xml versi 
3 packet from ip41 data:on="1.0" 
1 packet from ip35 data:< ?xml
4 packet from ip41 data:encoding="UTF-8 

真实的数据看起来像这样:

<?xml version="1.0" encoding="UTF-8"?><alarm><datetime>2010-07-18T11:14:22Z</datetime><textch><textchid>020</textchid></textch><rule>DIR-020</rule><text>020-DIR-Intersection3_Magles_TCS6</text></alarm> 

我希望能够像真实数据一样获取字符串中的数据,而不是碎片。 .net 中是否有方法或库可以做到这一点?

i'm developing an application that is listening to tcp to get some xml data coming from other devices.
i'm use sniffing c# code, and i can sniff all the packets. my problem is that in every packet i can find a Piece of the data in every packet.
like this:

1 packet from ip41 data:< 
2 packet from ip41 data:?xml versi 
3 packet from ip41 data:on="1.0" 
1 packet from ip35 data:< ?xml
4 packet from ip41 data:encoding="UTF-8 

the real data looks like this:

<?xml version="1.0" encoding="UTF-8"?><alarm><datetime>2010-07-18T11:14:22Z</datetime><textch><textchid>020</textchid></textch><rule>DIR-020</rule><text>020-DIR-Intersection3_Magles_TCS6</text></alarm> 

i want to be able to get the data in a string like the real data, not in pieces.
is there is a method or a library in .net that can do that?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

输什么也不输骨气 2024-09-18 22:16:18

您是在嗅探,还是只想连接到设备并获取数据?如果是后者,您可以使用 TcpClient 类来完成您需要的操作。

using System.Net.Sockets;

TcpClient tcp = new TcpClient(AddressFamily.InterNetwork);
tcp.Connect(IPAddress.Parse("192.168.0.1"), 12345);

然后 tcp.GetStream() 将为您提供一些可以输入到您选择的 XML 解析器中的内容。

编辑:这是一个稍微更详细的示例。

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

namespace ConsoleApplication1 {

    class XMLBlaster {
        Thread myThread;

        public XMLBlaster() {
            myThread = new Thread(Start);
        }

        public void Begin() {
            myThread.Start();
        }

        //This will listen for a connection on port 12345, and send a tiny XML document
        //to the first person to connect.
        protected void Start() {
            TcpListener tcp = new TcpListener(IPAddress.Any, 12345);
            tcp.Start(1);

            TcpClient client = tcp.AcceptTcpClient();

            StreamWriter data = new StreamWriter(client.GetStream());

            data.Write("<myxmldocument><node1><node2></node2></node1></myxmldocument>");

            data.Close();
            client.Close();
        }
    }

    class Program {


        static void Main(string[] args) {

            //this sets up the server we will be reading
            XMLBlaster server = new XMLBlaster();
            server.Begin();


            //this is the important bit

            //First, create the client
            TcpClient tcp = new TcpClient(AddressFamily.InterNetwork);

            //Next, connect to the server. You probably will want to use the prober settings here
            tcp.Connect(IPAddress.Loopback, 12345);

            //Since byte manipulation is ugly, let's turn it into strings
            StreamReader data_in = new StreamReader(tcp.GetStream());

            //And, just read everything the server has to say
            Console.WriteLine(data_in.ReadToEnd());

            //when we're done, close up shop.
            data_in.Close();
            tcp.Close();

            //this is just to pause the console so you can see what's going on.
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey(false);
        }
    }
}

请注意,这忽略了您需要遵循的任何协议的问题(例如,如果您通过 HTTP(端口 80)进行通信,则在获取数据之前与服务器通信需要做很多工作(并且,还有另一个类这可以正确地做到这一点;))

Are you sniffing, or do you just want to connect to the device and grab the data? If the latter, you can use the TcpClient class to do what you need.

using System.Net.Sockets;

TcpClient tcp = new TcpClient(AddressFamily.InterNetwork);
tcp.Connect(IPAddress.Parse("192.168.0.1"), 12345);

And then tcp.GetStream() will get you something you can feed into your XML parser of choice.

Edit: Here's a slightly more detailed sample.

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

namespace ConsoleApplication1 {

    class XMLBlaster {
        Thread myThread;

        public XMLBlaster() {
            myThread = new Thread(Start);
        }

        public void Begin() {
            myThread.Start();
        }

        //This will listen for a connection on port 12345, and send a tiny XML document
        //to the first person to connect.
        protected void Start() {
            TcpListener tcp = new TcpListener(IPAddress.Any, 12345);
            tcp.Start(1);

            TcpClient client = tcp.AcceptTcpClient();

            StreamWriter data = new StreamWriter(client.GetStream());

            data.Write("<myxmldocument><node1><node2></node2></node1></myxmldocument>");

            data.Close();
            client.Close();
        }
    }

    class Program {


        static void Main(string[] args) {

            //this sets up the server we will be reading
            XMLBlaster server = new XMLBlaster();
            server.Begin();


            //this is the important bit

            //First, create the client
            TcpClient tcp = new TcpClient(AddressFamily.InterNetwork);

            //Next, connect to the server. You probably will want to use the prober settings here
            tcp.Connect(IPAddress.Loopback, 12345);

            //Since byte manipulation is ugly, let's turn it into strings
            StreamReader data_in = new StreamReader(tcp.GetStream());

            //And, just read everything the server has to say
            Console.WriteLine(data_in.ReadToEnd());

            //when we're done, close up shop.
            data_in.Close();
            tcp.Close();

            //this is just to pause the console so you can see what's going on.
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey(false);
        }
    }
}

Note that this ignores the problem of any protocols you need to follow (for example, if you're communicating via HTTP (port 80), there's a lot of work involved in talking to the server before getting the data (and, there's another class that does this properly ;))

我通过线程监视一个端口来做到这一点......并按序列号组装它们。
感谢你的帮助

i did it by monitoring one port by thread..and Assembly them by sequence number.
thanks for all your help

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文