Click here to Skip to main content
13,704,440 members
Click here to Skip to main content
Add your own
alternative version


72 bookmarked
Posted 22 Oct 2007
Licenced CPOL

Notification Client and Server Written in C#

, 22 Oct 2007
Rate this:
Please Sign up or sign in to vote.
Notification Client and Server written in C#
Screenshot - notify1.jpg Screenshot - notify2.jpg


This article is about two small programs that will allow you to send and receive messages across a normal socket and display the message inside a "balloon" notification.


I was using several applications to communicate messages to various users, and found that the many different methods were becoming somewhat cumbersome. So I hastily slapped together some C# code and came up with the following programs. I had never done any real C# programming before, and it was definitely a learning experience.

Using the Code

The NotifyServer program is pretty simple. Pick a port number and hit the Start button to begin listening. Hit the Stop button to end the listener.

I created some enums to hold the values for my Icons:

private enum balloonIcons
    INFO = ToolTipIcon.Info,
    ERROR = ToolTipIcon.Error,
    WARN = ToolTipIcon.Warning,
    NONE = ToolTipIcon.None
private enum logIcons
    INFO = EventLogEntryType.Information,
    ERROR = EventLogEntryType.Error,
    WARN = EventLogEntryType.Warning,
    NONE = EventLogEntryType.Information,

I wanted my application to minimize to the notification area when the minimize button was pressed, so I added the following event handler to the form.Deactivate event:

this.Deactivate += new EventHandler(this.sendToTray);

void sendToTray(Object sender, EventArgs e)
            if(this.WindowState == FormWindowState.Minimized)

When the Start button is pressed, I create a TcpListener, start it and begin an asynchronous accept:

    //Create my listener and start it
    tcpListen = new TcpListener(new System.Net.IPEndPoint
        (System.Net.IPAddress.Any, Convert.ToInt16(this.txtPort.Text)));
    //Being asynchronous callback using the processEvents routine
    tcpListen.BeginAcceptTcpClient(new AsyncCallback(this.processEvents), tcpListen);
    this.notifyIcon.ShowBalloonTip(0, "NotifyServer", 
        "Now listening on port " + txtPort.Text + ".", ToolTipIcon.Info);
catch(System.Exception error)
    this.notifyIcon.ShowBalloonTip(0, "NotifyServer", 
        "Listener could not be started!", ToolTipIcon.Error);

The processEvents routine looks like this:

    StringBuilder myMessage = new StringBuilder("");
    //I want to write the messages to the event log
    //If the event log source doesn't exists, create it.
        EventLog.CreateEventSource("NotifyServer", "Application");
    //Create a new event log object and set the source
    eLog = new EventLog();
    eLog.Source = "NotifyServer";
    //Safely get our listener object and a TcpClient from that listener
    TcpListener processListen = (TcpListener) asyn.AsyncState;
    TcpClient tcpClient = processListen.EndAcceptTcpClient(asyn);
    //Get our stream
    NetworkStream myStream = tcpClient.GetStream();
    myMessage.Length = 0;
    string[] messageArray;
    //If our stream is readable, put the data in our string, write to the log
    //and create a notify message
        StreamReader readerStream = new StreamReader(myStream);
        messageArray = myMessage.ToString().Split("|".ToCharArray());
            messageArray[2], messageArray[3], 
            (ToolTipIcon) balloonIcons.Parse(typeof(balloonIcons), messageArray[1]));
        eLog.WriteEntry(messageArray[3], (EventLogEntryType)logIcons.Parse
            (typeof(logIcons), messageArray[1]));
    //Close everything
    myMessage = null;
    messageArray = null;
    //Re-register our callback
    tcpListen.BeginAcceptTcpClient(new AsyncCallback(this.processEvents), tcpListen);

The Client program is an easy to use console application as well. You can use any Client you want that can send messages across the socket. The calling format of the client program is:

c:\notifyclient hostname port timeout icon[INFO|WARN|NONE|ERROR] "Title"  "Message" 

The client simply creates a TcpClient, connects to the listener, creates a network stream, and writes to it:

StringBuilder myMessage = new System.Text.StringBuilder();
    //Create a new TcpClient
    TcpClient client = new TcpClient();
    //Create an encoder so we can convert String to Bytes
    ASCIIEncoding encoder = new ASCIIEncoding();
    //Do the conversion
    byte[] buffer = encoder.GetBytes(myMessage.ToString());
    //Connect to the host using the command line argument provided
    client.Connect(args[0], Convert.ToInt16(args[1]));
    //Get our network stream
    NetworkStream stream = client.GetStream();
    //Write to our stream
    stream.Write(buffer, 0, buffer.Length);
    //Close everything
    Console.WriteLine("Message was sent to host " + args[0] + " on port " + args[1]);
catch(System.Exception e)
    Console.WriteLine("Message was not sent");

The message is sent to the server in the following format:


Points of Interest

This was one of the first C# projects I ever did, so I'm sure it's quite messy. I'm still learning about thread safety, delegates, and all the other interesting little "gotchas" that come along with the .NET Framework. I'm sure that there are a lot of things done wrong in this program.

Apparently, there is a min and max value for balloon timeouts. The timeout is somewhere between 10 and 30 seconds. You can specify a timeout in between those values and it will be obeyed. However, anything over or under those timeouts is ignored and a default timeout is used instead. The timeout counter doesn't seem to tick when the user is idle.

You can use any type of client that can send information over a socket.


  • October 22, 2007 - Posted


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


About the Author

Web Developer
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

Generali can't get any notifire to work Pin
NoteNv1-Nov-07 0:29
memberNoteNv1-Nov-07 0:29 
GeneralRe: i can't get any notifire to work Pin
Helbrax1-Nov-07 1:50
memberHelbrax1-Nov-07 1:50 
GeneralI can't get any bubble notifier to work Pin
leajohnston23-Oct-07 6:51
memberleajohnston23-Oct-07 6:51 
GeneralRe: I can't get any bubble notifier to work Pin
Helbrax23-Oct-07 7:18
memberHelbrax23-Oct-07 7:18 
GeneralRe: I can't get any bubble notifier to work Pin
leajohnston24-Oct-07 8:06
memberleajohnston24-Oct-07 8:06 
GeneralRe: I can't get any bubble notifier to work Pin
Helbrax24-Oct-07 8:57
memberHelbrax24-Oct-07 8:57 
GeneralRe: I can't get any bubble notifier to work Pin
wilpeck30-Oct-07 5:46
memberwilpeck30-Oct-07 5:46 
QuestionPlease Help!! Pin
Baba Deep22-Oct-07 16:27
memberBaba Deep22-Oct-07 16:27 
AnswerRe: Please Help!! Pin
Helbrax23-Oct-07 3:01
memberHelbrax23-Oct-07 3:01 
GeneralRe: Please Help!! Pin
Baba Deep24-Oct-07 15:19
memberBaba Deep24-Oct-07 15:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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 | Cookies | Terms of Use | Mobile
Web04-2016 | 2.8.180920.1 | Last Updated 22 Oct 2007
Article Copyright 2007 by Helbrax
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid