How to capture TCP packets in C#
.net, c#, networking
Solution
Goto: http://www.winpcap.org/install/default.htm
Download and install: Installer for Windows
Re-Run your application.
No more: "Unable to load DLL 'wpcap': The specified module could not be found. (Exception from HRESULT: 0x8007007E)"
Problem
I am trying to read packets that are sent from the client to the server. However, I am receiving an error message: Unable to load DLL 'wpcap': The specified module could not be found. (Exception from HRESULT: 0x8007007E)" Could someone please point out how I can fix this error? My code: ``` using System; using System.Collections.Generic; using System.Linq; using System.Text; using SharpPcap; using SharpPcap.AirPcap; using PacketDotNet; namespace ConsoleApplication2MB { class Program { static void Main(string[] args) { //Extract the device list CaptureDeviceList devices = CaptureDeviceList.Instance; if (devices.Count < 1) { Console.WriteLine("No devices were found on this machine"); return; } Console.WriteLine("\nThe following devices are available on this machine:"); Console.WriteLine("----------------------------------------------------\n"); Console.WriteLine("Available AirPcap devices:"); for (var i = 0; i < devices.Count; i++) { Console.WriteLine("[{0}] - {1}", i, devices[i].ToString()); } Console.WriteLine(); Console.Write("Please choose a device to capture: "); var devIndex = int.Parse(Console.ReadLine()); var device = devices[devIndex]; device.Open(DeviceMode.Promiscuous); string filter = "ip and tcp"; device.Filter = filter; device.OnPacketArrival += new PacketArrivalEventHandler(device_OnPacketArrival); device.StartCapture(); //Console.Write("Please press enter to exit..."); //Console.ReadLine(); } private static void device_OnPacketArrival(object sender, CaptureEventArgs e) { var time = e.Packet.Timeval.Date; var len = e.Packet.Data.Length; Console.WriteLine("{0}:{1}:{2},{3} Len={4}", time.Hour, time.Minute, time.Second, time.Millisecond, len); Console.WriteLine(e.Packet.ToString()); var packet = PacketDotNet.Packet.ParsePacket(e.Packet.LinkLayerType, e.Packet.Data); var tcpPacket = PacketDotNet.TcpPacket.GetEncapsulated(packet); if (tcpPacket != null) { var ipPacket = (PacketDotNet.IpPacket)tcpPacket.ParentPacket; System.Net.IPAddress srcIp = ipPacket.SourceAddress; System.Net.IPAddress dstIp = ipPacket.DestinationAddress; int srcPort = tcpPacket.SourcePort; int dstPort = tcpPacket.DestinationPort; Console.WriteLine("{0}:{1}:{2},{3} Len={4} {5}:{6} -> {7}:{8}", time.Hour, time.Minute, time.Second, time.Millisecond, len, srcIp, srcPort, dstIp, dstPort); } } } } ```