Run this example simultaneously on several computers on a network. All users will be able to exchange plain text messages.
A light-weight solution with no central server, no file sharing, and no access violation, but with automatic user detection.
Note that the communication may require ports 137 to 139 opened on each side. No further configuration is required except the setting a name for the mailslot. Same name have to be used by all participating applications.
A timer control is set to check for inbound messages.
Mailslot name -- in this sample code "foxapp" -- practically keeps all users together. Any other valid file name is appropriate, except some reserved names like "Messengr", "BROWSER" etc.
A mailslot with name \\.\mailslot\foxapp is created on each computer. That turns such a computer into server receiving messages sent to this mailslot from other computers.
A computer name is obtained through SYS(0) function.
* * *
On start the Tmsg instance creates a mailslot with a name "foxapp", same name for all users in this group should be used: \\.\maislot\foxapp.
Then Tmsg broadcasts ONLINE and DETECT messages to all available network computers, using wildcard character * as a destination mailslot name: \\*\mailslot\foxapp.
On receiving DETECT message any existing Tmsg instance automatically sends ONLINE message back to the sender. That is how the new object gets know who else is online.
On termination the Tmsg instance broadcasts OFFLINE message to all users -- again the wildcard character is used -- to let everyone know that this user is disconnected.
Maislots provide no confirmation of receipt. For that reason the Tmsg object on receiving a message automatically sends a confirmation back to the sender.
For sending a message to another computer -- say with DESK3011 name -- the Tmsg object creates file handle using mailslot name \\DESK3011\Maislot\foxapp, and writes message data to this handle using WriteFile function.
A message has a simple format:
sender - 24 bytes -- local computer name
confirmation flag - 1 byte
system message flag - 1 byte
message body - the rest of the message
The GetMessages method is invoked on each Timer event checking for incoming messages.