Synopsis

Class Diagram

You can find the complete class diagram here : ClassDiagram

Public Methods

  • MailSlot(string name):
Create a new local MailSlot server(reader)
  • MailSlot(string name, string remote)
Create a new remote Mailslot client (writer). The remote parameter is the scope; "*' for the primary domain, a domain name, a computer name or "." for the local computer. If it is null or empty, it will default to "." for local.
  • bool Open(string name)
Close the current underlying win32 Mailslot object and open a new local server one.
  • bool Open(string name, string remote)
Close the current underlying win32 Mailslot object and open a new remote client one.
  • void Close()
Close the current underlying win32 Mailslot and associated FileStream
  • void Dispose()
Dispose the current Mailslot instance.

Public Properties

  • MaxMessageSize
You can change the default(4Kb) of the maximum message size on Mailslot creation. Note that if you change this property, you will need to use the Open method to reopen a mailslot with the new value.
  • Name
To get the name of the Mailslot.
  • Scope
Return the ScopeType : remote or local.
  • Type
Return the SlotType : reader or writer.
  • RemoteName
To get the name of the remote domain or computer
  • Filename
To get the full name of the Mailslot
  • FStream
The fileStream that the consumer will use to read or write in the Mailslot.
  • IsReady
The MailSlot instance is OK to be used : the FileStream is created and ready to use.
  • IsMessageInSlot
For a reader (server) MailSlot; there is a message ready to be read in the Mailslot.
  • NextMessageSize
Size of the next message ready in the Mailslot.

Usage Examples

Mailslot server (reader)

            try
            {
                MailSlot ms = new MailSlot(mailslotname);
                StreamReader sr = new StreamReader(ms.FStream);
                while (true)
                 {
                    if (ms.IsMessageInSlot > 0)
                    {
                        string message = sr.ReadLine();
                        Console.WriteLine(@"Message --> {0}", (message != null)?message:@"NULL");
                    }
                 }
           }
            catch (Exception e)
            {
                Console.WriteLine("The process failed: {0}", e.ToString());
                return;
            }

Mailslot client (writer)

           try
            {
                MailSlot ms = new MailSlot(mailslotname, @"*"); //Target the primary domain as scope; see MSDN docs on mailslots
                StreamWriter sw = new StreamWriter(ms.FStream);
                String message = String.Format(@"{0}:{1}", Environment.MachineName, @"This is a test message...");
                sw.WriteLine(message);
                sw.Flush();
                Console.WriteLine(@"Wrote to stream: {0}", message);
                sw.Dispose();
                ms.Dispose();
            }
            catch (Exception e)
            {
                Console.WriteLine("The process failed: {0}", e.ToString());
                return;
            }

Sidenotes

If the message is sent to a whole domain (scope = domainname or *) , it will be sent using datagrams so it cannot be bigger that 420 bytes. Also, file and printer sharing should be allowed in the firewall to enable UDP 137 . See the MSDN documentation for more information.

Last edited Apr 7, 2011 at 8:49 PM by jmjulien, version 10

Comments

No comments yet.