Click here to Skip to main content
Licence CPOL
First Posted 10 Dec 2009
Views 23,338
Downloads 1,125
Bookmarked 50 times

TrafficLights

By Amit Engineer | 10 Dec 2009
Blinks keyboard LEDs to display network traffic.

1

2
3 votes, 11.5%
3
5 votes, 19.2%
4
18 votes, 69.2%
5
4.85/5 - 26 votes
3 removed
μ 4.62, σa 1.23 [?]

Introduction

I wanted to see the network activity other than in the system tray network icon, so I thought of using the keyboard's CAPS LOCK, SCROLL LOCK, and NUM key LEDs to display the network activity. TrafficLights is a simple application which blinks keyboard lights based on the network traffic on your computer. You can configure which light should blink on send or receive packets. It silently sits on your system tray. You can right click on TrafficLights' system tray icon to configure the LEDs or shut it down.

Background

This is just to learn how to trap network traffic and control keyboard LEDs in .NET.

Using the Code

There are basically two parts to this application:

  • How to identify network traffic.
  • The code below is used to initialize the PerformanceCounter class instances - one for send and one for receive (namely m_performanceCounterSent and m_performanceCounterReceive). They are initialized like so:

    //
    m_performanceCounterCategory = new PerformanceCounterCategory("Network Interface");
    string instance = m_performanceCounterCategory.GetInstanceNames()[0]; // 1st NIC !
    m_performanceCounterSent = new PerformanceCounter("Network Interface", 
                                   "Bytes Sent/sec", instance);
    m_performanceCounterReceived = new PerformanceCounter("Network Interface", 
                                       "Bytes Received/sec", instance);
  • How to control keyboard LEDs.
  • A background thread keeps running in the application, and it keeps on checking on the performance counter to check any network activity. And when an activity is detected, it blinks the configured keyboard LED.

    float fSentValue = m_performanceCounterSent.NextValue() / 1024;
    float fReceivedValue = m_performanceCounterReceived.NextValue() / 1024;
    if (fSentValue > 0)
    {
       keybd_event((byte)m_sendLED, 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr)0);
       keybd_event((byte)m_sendLED, 0x45, 
                    KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, (UIntPtr)0);
    }
    if (fReceivedValue > 0)
    {
       keybd_event((byte)m_receiveLED, 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr)0);
       keybd_event((byte)m_receiveLED, 0x45, 
                    KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, (UIntPtr)0);
    }

Points of Interest

As I mentioned earlier, it's a very simple application, but the intention was to learn how to trap network traffic in .NET.

History

This is the first version of this application. Also, my first attempt to post anything on CodeProject.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Amit Engineer



United States United States

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralGood C# copy PinmemberIgor Tolmachev5:53 16 Jan '10  
GeneralRe: Good C# copy PinmemberAmit Engineer10:01 18 Jan '10  
GeneralNice and funny PinmemberMuhammad Nauman Yousuf23:24 14 Dec '09  
GeneralNice but heavy CPU load PinmemberThomas Kuehn9:38 11 Dec '09  
GeneralRe: Nice but heavy CPU load PinmemberAmit Engineer11:01 11 Dec '09  
QuestionRe: Nice but heavy CPU load PinmemberAmit Engineer11:11 11 Dec '09  
AnswerRe: Nice but heavy CPU load PinmemberIgor Tolmachev5:56 16 Jan '10  
Generalgreat Pinmemberthebeekeeper5:57 11 Dec '09  
Generalfunny.. [modified] Pinmemberreborn_zhang5:45 11 Dec '09  
GeneralRe: funny.. Pinmemberreborn_zhang5:56 11 Dec '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120210.1 | Last Updated 11 Dec 2009
Article Copyright 2009 by Amit Engineer
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid