1

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のメソッドまたはライブラリはありますか?

4

2 に答える 2

1

スニッフィングしていますか、それともデバイスに接続してデータを取得したいだけですか?後者の場合、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)を介して通信している場合、データを取得する前にサーバーとの通信に多くの作業が必要です(また、別のクラスがあります)。これは適切に行われます;))

于 2010-07-28T08:07:45.440 に答える
1

私はスレッドごとに1つのポートを監視することでそれを行いました..そしてそれらをシーケンス番号で組み立てました。ご助力いただきありがとうございます

于 2010-08-25T10:30:01.507 に答える