RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Create a Syslog Sender/Receiver Using the MS Winsock Control : Page 2

With the Microsoft WinSock Control and a few coding tricks, you can use Access as a high-performance syslog sender, receiver, and logger.


Create a WinSock-Enabled Form

Creating a form with the WinSock Control is quite easy:
  1. Create a new form and keep it open in design view.
  2. Open the toolbox or menu entry with available ActiveX components.
  3. Locate the control: Microsoft WinSock Control, version 6.0.
  4. Select this and click OK.

This procedure adds the control to the form, typically with the name Winsock0. At the same time, it verifies that the control is installed and activated. If any errors occur, the control is not installed or activated correctly.

Before going any further, go to the property sheet and set (tab Format) RecordSelectors and NavigationButtons to False. Save the form as frmSyslogTestSend and create a copy of the form by saving it as frmSyslogTestReceive.

Now, open frmSyslogTestReceive in design view, add a wide textbox, and name it txtPackage. Also, set the properties Protocol and LocalPort of the WinSock control to 1 and 514, respectively, and set a descriptive label for the textbox.

Finally, open the form's code module and insert the following code:

Option Compare Database
Option Explicit

Private Sub Form_Open(Cancel As Integer)
  Me!Winsock0.Bind 514

End Sub

Private Sub Winsock0_DataArrival(ByVal bytesTotal As Long)

  Dim strPackage  As String
  Me!Winsock0.GetData strPackage, vbString
  Me!txtPackage.Value = strPackage

End Sub

Figure 1. Test Form to Receive Syslog Messages: This is the simple test form to receive and display syslog messages.

This is the minimal code to create a form for receiving a syslog message. The are no equal signs following Bind and GetData because these are methods, not properties. Save the form and open it. It should look like Figure 1.

Next, open frmSyslogTestSend in design view, add a button, name it btnSend, and change its caption to Send. Also, set the properties Protocol, RemotePort, LocalPort, and RemoteHost of the WinSock control to 1, 514, 10514, and localhost, respectively. The number 10514 is an arbitrary and high unused port number. Finally, open the form's code module and insert the following code:

Option Compare Database
Option Explicit

Private Sub btnSend_Click()
  Dim strPackage  As String
  strPackage = SyslogPackageEncode()
  Me!Winsock0.SendData CStr(strPackage)

End Sub

Figure 2. Test Form to Send a Syslog Message: This is the simple test form to send a syslog message with the current time.

This is the minimal code to create a form for sending a syslog message. Note the missing equal sign following SendData. But why, you may ask, is CStr used? The variable strPackage is a string, so what's the deal? The reason is that Access handles strings as Unicode, thus if you passed the string variable directly like this:

Me!Winsock0.SendData strPackage

The WinSock control would send a Unicode byte stream when a clean ASCII byte stream is what you expect. CStr converts the Unicode string variable to a simple ASCII byte stream. For testing, you could send any string. However, by calling the function SyslogPackageEncode from the basSyslog module, you generate a fully qualified syslog message with the current time and default values for all other parameters, for example:

Figure 3. Configure a Typical Router to Send Out Syslog Messages: Here is how to configure a router to send syslog messages.

<0>Jun 23 23:56:18 MYMACHINENAME Test: <no message>

Save the form and open it. It should look like Figure 2.

The First Test

With the two forms you have created, you are now ready to run your first test. Open both forms and press the Send button. The test message will appear in the textbox. Even though you can't decode it fully, you can read the current time in 24-hour format and the name of the sender, and you can tell that the message is a test not trying to provide any further information than this.

To obtain a series of true syslog messages, you will find many available sources depending on your environment. One is your local router, your gateway to the Internet. If you have access to this, you can configure it to send out statistics and other info via syslog. An example is shown in Figure 3. Make sure to set the receiver to the IP address of your workstation and the port number to 514.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date