Main.cs :  » Network-Clients » SharpPcap » MultipleFiltersOnDevice » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Network Clients » SharpPcap 
SharpPcap » MultipleFiltersOnDevice » Main.cs
using System;
using SharpPcap;
using PacketDotNet;

namespace MultipleFiltersOnDevice{
    /// <summary>
    /// Example that shows how to apply multiple filters to the same device
    /// </summary>
    class MainClass
    {
        public static void Main(string[] args)
        {
            // Print SharpPcap version
            string ver = SharpPcap.Version.VersionString;
            Console.WriteLine("SharpPcap {0}, MultipleFiltersOnDevice", ver);

            // If no devices were found print an error
            if(LivePcapDeviceList.Instance.Count < 1)
            {
                Console.WriteLine("No devices were found on this machine");
                return;
            }

            Console.WriteLine();
            Console.WriteLine("The following devices are available on this machine:");
            Console.WriteLine("----------------------------------------------------");
            Console.WriteLine();

            int i = 0;

            // Print out the devices
            foreach(LivePcapDevice dev in LivePcapDeviceList.Instance)
            {
                /* Description */
                Console.WriteLine("{0}) {1} {2}", i, dev.Name, dev.Description);
                i++;
            }

            Console.WriteLine();
            Console.Write("-- Please choose a device to capture: ");
            i = int.Parse( Console.ReadLine() );

            int readTimeoutMilliseconds = 1000;

            var device1 = LivePcapDeviceList.Instance[i];
            var device2 = LivePcapDeviceList.New()[i]; // NOTE: the call to New()

            // Register our handler function to the 'packet arrival' event
            device1.OnPacketArrival += 
                        new PacketArrivalEventHandler( device_OnPacketArrival );
            device2.OnPacketArrival += 
                        new PacketArrivalEventHandler( device_OnPacketArrival );

            // Open the devices for capturing
            device1.Open(DeviceMode.Promiscuous, readTimeoutMilliseconds);
            device2.Open(DeviceMode.Promiscuous, readTimeoutMilliseconds);

            // set the filters
            device1.SetFilter("tcp port 80"); // http
            device2.SetFilter("udp port 53"); // dns

            Console.WriteLine();
            Console.WriteLine("-- Listening on {0}, hit 'Enter' to stop...",
                device1.Description);

            // Start the capturing process
            device1.StartCapture();
            device2.StartCapture();

            // Wait for 'Enter' from the user.
            Console.ReadLine();

            // Stop the capturing process
            device1.StopCapture();
            device2.StopCapture();

            Console.WriteLine("-- Capture stopped.");

            // Print out the device statistics
            Console.WriteLine("device1 {0}", device1.Statistics().ToString());
            Console.WriteLine("device2 {0}", device2.Statistics().ToString());

            // Close the pcap device
            device1.Close();
            device2.Close();
        }

        /// <summary>
        /// Prints the time and length of each received packet
        /// </summary>
        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);
            var p = Packet.ParsePacket(e.Packet);
            Console.WriteLine(p.ToString());
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.