2

我在一台计算机上通过端口 8888 有 2 个软件一起工作,我想知道它们是如何工作的。如果我能得到另一种方式,比如软件来完成这项工作,那就太好了:)

我下载 pcapdotnet 并尝试http://pcapdotnet.codeplex.com/wikipage?title=Pcap.Net%20User%20Guide&referringTitle=Home上的示例代码 它可以获取本地网络上的所有消息,但不适用于我。

我使用 netstat -a get this " TCP 0.0.0.0:8888 ZC01N00278:0 LISTENING" 我真的对这个 0.0.0.0 感到困惑。

所以我禁用了我所有的网络设备(这导致我的 pcap 无法工作,因为它需要至少一个设备),但它仍然存在。我想这两个软件在没有以太网的情况下相互通信,是真的吗?

我是套接字的新手,我可以通过什么方式在这个端口获取数据包?这是代码,主要来自教程示例。

using System;
using System.Collections.Generic;
using PcapDotNet.Core;
using PcapDotNet.Packets;
using PcapDotNet.Packets.IpV4;
using PcapDotNet.Packets.Transport;
using System.IO;

namespace pcap_test1
{
    class Program
    {
        static StreamWriter sw;
        static void Main(string[] args)
        {
            sw = new StreamWriter(@"C:\sunxin\pcap.txt");
            // Retrieve the device list from the local machine
            IList<LivePacketDevice> allDevices = LivePacketDevice.AllLocalMachine;

            if (allDevices.Count == 0)
            {
                Console.WriteLine("No interfaces found! Make sure WinPcap is installed.");
                return;
            }

            // Print the list
            for (int i = 0; i != allDevices.Count; ++i)
            {
                LivePacketDevice device = allDevices[i];
                Console.Write((i + 1) + ". " + device.Name);
                if (device.Description != null)
                    Console.WriteLine(" (" + device.Description + ")");
                else
                    Console.WriteLine(" (No description available)");
            }

            int deviceIndex = 0;
            do
            {
                Console.WriteLine("Enter the interface number (1-" + allDevices.Count + "):");
                string deviceIndexString = Console.ReadLine();
                if (!int.TryParse(deviceIndexString, out deviceIndex) ||
                    deviceIndex < 1 || deviceIndex > allDevices.Count)
                {
                    deviceIndex = 0;
                }
            } while (deviceIndex == 0);

            // Take the selected adapter
            PacketDevice selectedDevice = allDevices[deviceIndex - 1];

            // Open the device
            using (PacketCommunicator communicator =
                selectedDevice.Open(65536,                                  // portion of the packet to capture
                // 65536 guarantees that the whole packet will be captured on all the link layers
                                    PacketDeviceOpenAttributes.Promiscuous, // promiscuous mode
                                    1000))                                  // read timeout
            {
                // Check the link layer. We support only Ethernet for simplicity.
                if (communicator.DataLink.Kind != DataLinkKind.Ethernet)
                {
                    Console.WriteLine("This program works only on Ethernet networks.");
                    return;
                }

                // Compile the filter
                using (BerkeleyPacketFilter filter = communicator.CreateFilter("port 8888"))
                {
                    // Set the filter
                    communicator.SetFilter(filter);
                }

                Console.WriteLine("Listening on " + selectedDevice.Description + "...");

                // start the capture
                communicator.ReceivePackets(0, PacketHandler);
            }
        }

        // Callback function invoked by libpcap for every incoming packet
        private static void PacketHandler(Packet packet)
        {
            // print timestamp and length of the packet
            Console.WriteLine(packet.Timestamp.ToString("yyyy-MM-dd hh:mm:ss.fff") + " length:" + packet.Ethernet);
            sw.WriteLine(packet.Timestamp.ToString("yyyy-MM-dd hh:mm:ss.fff") +  packet.Ethernet);

            IpV4Datagram ip = packet.Ethernet.IpV4;
            UdpDatagram udp = ip.Udp;
            for (int i = ip.HeaderLength; i < packet.Length; ++i)
            {
                Console.Write(Convert.ToChar(packet.Buffer[i]));
                sw.Write(Convert.ToChar(packet.Buffer[i]));
            }
            Console.WriteLine();
            sw.WriteLine();
            // print ip addresses and udp ports
            //Console.WriteLine(ip.Source + ":" + udp.SourcePort + " -> " + ip.Destination + ":" + udp.DestinationPort);
            //sw.WriteLine(ip.Source + ":" + udp.SourcePort + " -> " + ip.Destination + ":" + udp.DestinationPort);
            sw.Flush();
        }
    }
}
4

1 回答 1

0

Wireshark 的 wiki 告诉WinPcap 无法在 Windows 的同一台计算机上捕获端点之间的数据包(Pcap.Net 使用 WinPcap)。它建议使用RawCap

于 2013-09-25T21:11:16.630 回答