Click here to Skip to main content
15,867,686 members
Articles / Programming Languages / C#
Article

Notification Client and Server Written in C#

Rate me:
Please Sign up or sign in to vote.
4.53/5 (7 votes)
22 Oct 2007CPOL2 min read 91.7K   3.4K   73   10
Notification Client and Server written in C#
Screenshot - notify1.jpg Screenshot - notify2.jpg

Introduction

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.

Background

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:

C#
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:

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

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

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

C#
try
{
    //Create my listener and start it
    tcpListen = new TcpListener(new System.Net.IPEndPoint
        (System.Net.IPAddress.Any, Convert.ToInt16(this.txtPort.Text)));
    tcpListen.Start();
    //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:

C#
try
{
    StringBuilder myMessage = new StringBuilder("");
    //I want to write the messages to the event log
    //If the event log source doesn't exists, create it.
    if(!EventLog.SourceExists("NotifyServer"))
    {
        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
    if(myStream.CanRead)
    {
        StreamReader readerStream = new StreamReader(myStream);
        myMessage.Append(readerStream.ReadToEnd());
        messageArray = myMessage.ToString().Split("|".ToCharArray());
        this.notifyIcon.ShowBalloonTip(Convert.ToInt16(messageArray[0]), 
            messageArray[2], messageArray[3], 
            (ToolTipIcon) balloonIcons.Parse(typeof(balloonIcons), messageArray[1]));
        eLog.WriteEntry(messageArray[3], (EventLogEntryType)logIcons.Parse
            (typeof(logIcons), messageArray[1]));
        readerStream.Close();
    }
    //Close everything
    myMessage = null;
    messageArray = null;
    myStream.Close();
    tcpClient.Close();
    eLog.Close();
    //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:

C#
StringBuilder myMessage = new System.Text.StringBuilder();
myMessage.AppendFormat("{0}|{1}|{2}|{3}",args[2],args[3],args[4],args[5]);
try
{
    //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
    stream.Close();
    client.Close();
    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:

Timeout|Icon|Title|Message 

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.

History

  • October 22, 2007 - Posted

License

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


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generali can't get any notifire to work Pin
NoteNvGT1-Nov-07 0:29
NoteNvGT1-Nov-07 0:29 
GeneralRe: i can't get any notifire to work Pin
Helbrax1-Nov-07 1:50
Helbrax1-Nov-07 1:50 
GeneralI can't get any bubble notifier to work Pin
leajohnston23-Oct-07 6:51
leajohnston23-Oct-07 6:51 
GeneralRe: I can't get any bubble notifier to work Pin
Helbrax23-Oct-07 7:18
Helbrax23-Oct-07 7:18 
GeneralRe: I can't get any bubble notifier to work Pin
leajohnston24-Oct-07 8:06
leajohnston24-Oct-07 8:06 
GeneralRe: I can't get any bubble notifier to work Pin
Helbrax24-Oct-07 8:57
Helbrax24-Oct-07 8:57 
GeneralRe: I can't get any bubble notifier to work Pin
Wil Peck30-Oct-07 5:46
Wil Peck30-Oct-07 5:46 
QuestionPlease Help!! Pin
Baba Deep22-Oct-07 16:27
Baba Deep22-Oct-07 16:27 
AnswerRe: Please Help!! Pin
Helbrax23-Oct-07 3:01
Helbrax23-Oct-07 3:01 
GeneralRe: Please Help!! Pin
Baba Deep24-Oct-07 15:19
Baba 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.