Sockets windows7中的pcapdotnet如何在不指定设备或ip为0.0.0.0的情况下嗅探端口?
我有两个软件在一台计算机上通过8888端口一起工作,我想知道它们是如何工作的。如果我能找到另一种方法,比如用软件来做这项工作,那真是太好了:) 我下载了pcapdotnet并在其上尝试了示例代码 它可以在本地网络上获取所有消息,但不能为我 我使用netstat-a获取“TCP 0.0.0.0:8888 ZC01N00278:0侦听” 对于这个0.0.0.0我真的很困惑 所以我禁用了我所有的网络设备(这导致我的pcap无法工作,因为它至少需要一个设备),但它仍然存在。我想这两个软件在没有以太网的情况下相互通信,是真的吗 我是socket的新手,用哪种方式可以在这个端口获取数据包? 下面是代码,主要来自教程示例Sockets windows7中的pcapdotnet如何在不指定设备或ip为0.0.0.0的情况下嗅探端口?,sockets,pcap,Sockets,Pcap,我有两个软件在一台计算机上通过8888端口一起工作,我想知道它们是如何工作的。如果我能找到另一种方法,比如用软件来做这项工作,那真是太好了:) 我下载了pcapdotnet并在其上尝试了示例代码 它可以在本地网络上获取所有消息,但不能为我 我使用netstat-a获取“TCP 0.0.0.0:8888 ZC01N00278:0侦听” 对于这个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();
}
}
}
使用系统;
使用System.Collections.Generic;
使用PcapDotNet.Core;
使用PcapDotNet.Packets;
使用PcapDotNet.Packets.IpV4;
使用PcapDotNet.Packets.Transport;
使用System.IO;
名称空间pcap_test1
{
班级计划
{
静态StreamWriter sw;
静态void Main(字符串[]参数)
{
sw=新的StreamWriter(@“C:\sunxin\pcap.txt”);
//从本地计算机检索设备列表
IList allDevices=LivePacketDevice.AllLocalMachine;
如果(allDevices.Count==0)
{
WriteLine(“没有找到接口!确保安装了WinPcap。”);
返回;
}
//打印列表
for(int i=0;i!=allDevices.Count;++i)
{
LivePacketDevice设备=所有设备[i];
Console.Write((i+1)+“+”设备名称);
如果(device.Description!=null)
Console.WriteLine(“+device.Description+”);
其他的
Console.WriteLine((无可用描述));
}
int deviceIndex=0;
做
{
Console.WriteLine(“输入接口号(1-”+allDevices.Count+)”);
字符串deviceIndexString=Console.ReadLine();
if(!int.TryParse(deviceIndexString,out deviceIndex)||
deviceIndex<1 | | deviceIndex>allDevices.Count)
{
deviceIndex=0;
}
}而(deviceIndex==0);
//使用选定的适配器
PacketDevice selectedDevice=所有设备[deviceIndex-1];
//打开设备
使用(PacketCommunicator)=
选择Device.Open(65536,//要捕获的数据包部分
//65536保证在所有链路层上捕获整个数据包
PacketDeviceOpenAttributes.Promiscuous,//Promiscuous模式
1000))//读取超时
{
//检查链路层。为了简单起见,我们只支持以太网。
if(communicator.DataLink.Kind!=DataLinkKind.Ethernet)
{
WriteLine(“此程序仅在以太网网络上工作。”);
返回;
}
//编译过滤器
使用(BerkeleyPacketFilter=communicator.CreateFilter(“端口8888”))
{
//设置过滤器
通信器。设置过滤器(过滤器);
}
Console.WriteLine(“监听”+所选设备描述+”);
//开始捕获
communicator.ReceivePackets(0,PacketHandler);
}
}
//libpcap为每个传入数据包调用的回调函数
专用静态void PacketHandler(数据包)
{
//打印数据包的时间戳和长度
Console.WriteLine(packet.Timestamp.ToString(“yyyy-MM-dd-hh:MM:ss.fff”)+“长度:”+packet.Ethernet);
sw.WriteLine(packet.Timestamp.ToString(“yyyy-MM-dd-hh:MM:ss.fff”)+packet.Ethernet);
IpV4数据报ip=packet.Ethernet.IpV4;
UdpDatagram udp=ip.udp;
对于(int i=ip.HeaderLength;i”+ip.Destination+“:”+udp.DestinationPort);
//sw.WriteLine(ip.Source+“:”+udp.SourcePort+“->”+ip.Destination+“:”+udp.DestinationPort);
sw.Flush();
}
}
}
Wireshark的wiki告诉我们(Pcap.Net使用WinPcap)。它建议使用。谢谢,rawcap真的很好用,我同时下载wireshark。