Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

RasmusFaber

By: a guest on Jan 15th, 2009  |  syntax: C#  |  size: 3.26 KB  |  views: 1,290  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using System;
  2. using System.Diagnostics;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Net;
  6. using System.Net.Sockets;
  7. using System.Reflection;
  8. using System.Text;
  9. using System.Threading;
  10.  
  11. public class NetworkTraffic
  12. {
  13.   private PerformanceCounter bytesSentPerformanceCounter;
  14.   private PerformanceCounter bytesReceivedPerformanceCounter;
  15.  
  16.   public NetworkTraffic()
  17.   {
  18.     bytesSentPerformanceCounter = new PerformanceCounter();
  19.     bytesSentPerformanceCounter.CategoryName = ".NET CLR Networking";
  20.     bytesSentPerformanceCounter.CounterName = "Bytes Sent";
  21.     bytesSentPerformanceCounter.InstanceName = GetInstanceName();
  22.     bytesSentPerformanceCounter.ReadOnly = true;
  23.  
  24.     bytesReceivedPerformanceCounter = new PerformanceCounter();
  25.     bytesReceivedPerformanceCounter.CategoryName = ".NET CLR Networking";
  26.     bytesReceivedPerformanceCounter.CounterName = "Bytes Received";
  27.     bytesReceivedPerformanceCounter.InstanceName = GetInstanceName();
  28.     bytesReceivedPerformanceCounter.ReadOnly = true;
  29.   }
  30.  
  31.  
  32.   public float GetBytesSent()
  33.   {
  34.     float bytesSent = bytesSentPerformanceCounter.RawValue;
  35.  
  36.     return bytesSent;
  37.   }
  38.  
  39.   public float GetBytesReceived()
  40.   {
  41.     float bytesReceived = bytesReceivedPerformanceCounter.RawValue;
  42.  
  43.     return bytesReceived;
  44.   }
  45.  
  46.   private static string GetInstanceName()
  47.   {
  48.     // Used Reflector to find the correct formatting:
  49.     string assemblyName = GetAssemblyName();
  50.     if ((assemblyName == null) || (assemblyName.Length == 0))
  51.     {
  52.       assemblyName = AppDomain.CurrentDomain.FriendlyName;
  53.     }
  54.     StringBuilder builder = new StringBuilder(assemblyName);
  55.     for (int i = 0; i < builder.Length; i++)
  56.     {
  57.       switch (builder[i])
  58.       {
  59.         case '/':
  60.         case '\\':
  61.         case '#':
  62.           builder[i] = '_';
  63.           break;
  64.         case '(':
  65.           builder[i] = '[';
  66.           break;
  67.  
  68.         case ')':
  69.           builder[i] = ']';
  70.           break;
  71.       }
  72.     }
  73.  
  74.     return string.Format(CultureInfo.CurrentCulture,
  75.                          "{0}[{1}]",
  76.                          builder.ToString(),
  77.                          Process.GetCurrentProcess().Id);
  78.   }
  79.   private static string GetAssemblyName()
  80.   {
  81.     string str = null;
  82.     Assembly entryAssembly = Assembly.GetEntryAssembly();
  83.     if (entryAssembly != null)
  84.     {
  85.       AssemblyName name = entryAssembly.GetName();
  86.       if (name != null)
  87.       {
  88.         str = name.Name;
  89.       }
  90.     }
  91.     return str;
  92.   }
  93.  
  94.  
  95.   public static void Main()
  96.   {
  97.     NetworkTraffic networkTraffic = new NetworkTraffic();
  98.     try
  99.     {
  100.       while (true)
  101.       {
  102.         WebRequest webRequest = WebRequest.Create("http://www.google.com");
  103.         webRequest.Method = "GET";
  104.  
  105.         using (WebResponse response = webRequest.GetResponse())
  106.         using (Stream responseStream = response.GetResponseStream())
  107.         using (StreamReader reader = new StreamReader(responseStream))
  108.         {
  109.         }
  110.         Console.WriteLine("Bytes sent: {0}", networkTraffic.GetBytesSent());
  111.         Console.WriteLine("Bytes received: {0}", networkTraffic.GetBytesReceived());
  112.         Thread.Sleep(1000);
  113.       }
  114.     }
  115.     catch (Exception e)
  116.     {
  117.       Console.WriteLine(e);
  118.     }
  119.     Console.ReadLine();
  120.   }
  121. }