(c) BigSpeed Computing Inc. - Secure private networking

VB.NET example of secure chat and alert messaging application



  Dim fSettings As CFrmSettings
  Dim CallHandle As Integer 'Handle to the calling socket

  Const pcALERT As Short = 10 'Alert message ID
  Const pcCHAT As Short = 20 'Chat message ID


  'Initializations
  Private Sub fMain_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
    fSettings = New CFrmSettings     'create settings form
    CallHandle = 0
    SetProperties()
    UpdateStatus()
  End Sub



  'Terminate on form closing
  Private Sub fMain_Closed(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Closed
    End
  End Sub




  'Set the component properties
  Private Sub SetProperties()
  If fSettings.txtKey.Text > "" Then
    SockLib.SecurityMode = 2  'shared secret key
    SockLib.SecretKey = fSettings.txtKey.Text
  Else
    SockLib.SecurityMode = 0  'no encryption
  End If

  SockLib.UseCompression = fSettings.chkCompress.Checked

  SockLib.StopListening()
  If fSettings.chkServer.Checked Then
    SockLib.StartListening(Val(fSettings.txtListenPort.Text))
  End If
  End Sub



  'An alert message is available
  Private Sub HaveAlert(ByVal aHandle As Integer)
    Dim Line As String

    Line = "From " & SockLib.GetRemoteAddress(aHandle) & " at " & VB6.Format(Now, "hh:mm:ss") & Chr(13) & Chr(13)
    Line = Line & SockLib.TextFromBuffer(aHandle)
    Call MsgBox(Line, MsgBoxStyle.SystemModal, "Alert")

  End Sub



  'A chat message is available
  Private Sub HaveChat(ByVal aHandle As Integer)
    Dim Line As String
    If txtChatLog.Text > "" Then
      Line = Chr(13) & Chr(10)
    End If
    Line = Line & "[" & VB6.Format(Now, "hh:mm:ss") & "] Peer: "
    Line = Line & SockLib.TextFromBuffer(aHandle)
    txtChatLog.SelectionStart = Len(txtChatLog.Text)
    txtChatLog.SelectedText = Line
  End Sub



  '*** Event handlers


  'A chat message will be sent
  Private Sub btnChat_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles btnChat.Click
    Dim Line As String

    Call SockLib.TextToBuffer(lvPeers.SelectedItems(0).Tag, txtChatMsg.Text) 'Store the message
    Call SockLib.SendPacket(lvPeers.SelectedItems(0).Tag, pcCHAT) 'Send the chat message
    Line = ""
    If txtChatLog.Text > "" Then
      Line = Chr(13) & Chr(10)
    End If
    Line = Line & "[" & VB6.Format(Now, "hh:mm:ss") & "] Me: " & txtChatMsg.Text
    txtChatMsg.Text = ""
    txtChatLog.Text = txtChatLog.Text & Line
  End Sub



  'An alert message will be sent
  Private Sub btnAlert_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles btnAlert.Click
    Call SockLib.TextToBuffer(lvPeers.SelectedItems(0).Tag, txtAlertMsg.Text) 'Store the message
    Call SockLib.SendPacket(lvPeers.SelectedItems(0).Tag, pcALERT) 'Send an alert message
    txtAlertMsg.Text = ""
  End Sub




  'Modify the settings
  Private Sub btnSettings_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles btnSettings.Click

    If fSettings.ShowDialog <> DialogResult.OK Then Exit Sub

    SetProperties()
    UpdateStatus()
  End Sub



  'Show my IP address
  Private Sub btnIP_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles btnIP.Click
    MsgBox(SockLib.LocalIP)
  End Sub


  'Terminate the application
  Private Sub btnExit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles btnExit.Click
    End
  End Sub


  'Initiate a call
  Private Sub btnConnect_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles btnConnect.Click

  'create a new calling socket
  CallHandle = SockLib.CreateSession(txtIP.Text, Val(txtPort.Text))
  If CallHandle = 0 Then
    Call MsgBox("Cannot initiate a call", MsgBoxStyle.SystemModal, "Error")
    CallHandle = 0
  End If

  UpdateStatus()
  End Sub


  'Cancel a call operation
  Private Sub btnCancel_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles btnCancel.Click
  If CallHandle > 0 Then
    SockLib.TerminateSession(CallHandle)
    CallHandle = 0
    UpdateStatus()
  End If
  End Sub



  'Remove a socket
  Private Sub btnRemove_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles btnRemove.Click
  If lvPeers.SelectedItems.Count = 0 Then
    Exit Sub
  End If
  SockLib.TerminateSession(lvPeers.SelectedItems(0).Tag)
  lvPeers.Items.Remove(lvPeers.SelectedItems(0))
  End Sub



  Private Sub lvPeers_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvPeers.SelectedIndexChanged
    UpdateStatus()
  End Sub



  'Hide the focus from the chat box
  Private Sub txtChatLog_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtChatLog.GotFocus
    helper.Focus()
  End Sub



  'A new socket socket is created due to an incoming call
  Private Sub SockLib_OnSessionInvoked(ByVal sender As Object, ByVal e As AxbsSocketLibrary.IBSSockLibXEvents_OnSessionInvokedEvent) Handles SockLib.OnSessionInvoked
    Dim LI As ListViewItem
    Dim S As String
    S = SockLib.GetRemoteAddress(e.aHandle)
    LI = lvPeers.Items.Add(S)                              'IP
    LI.SubItems.Add(SockLib.GetRemotePort(e.aHandle))      'port
    LI.SubItems.Add(VB6.Format(Now, "hh:mm:ss"))           'time
    LI.Tag = e.aHandle

    UpdateStatus()
  End Sub



  'Successful call
  Private Sub SockLib_OnSessionCreated(ByVal sender As Object, ByVal e As AxbsSocketLibrary.IBSSockLibXEvents_OnSessionCreatedEvent) Handles SockLib.OnSessionCreated
    Dim LI As ListViewItem

    LI = lvPeers.Items.Add(txtIP.Text)                 'IP
    LI.SubItems.Add(txtPort.Text)                      'Port
    LI.SubItems.Add(VB6.Format(Now, "hh:mm:ss"))       'time
    LI.Tag = CallHandle
    CallHandle = 0

    If lvPeers.SelectedItems.Count = 0 Then
      LI.Selected = True
    End If

    UpdateStatus()
  End Sub



  'Unsuccessful call
  Private Sub SockLib_OnSessionRejected(ByVal sender As Object, ByVal e As AxbsSocketLibrary.IBSSockLibXEvents_OnSessionRejectedEvent) Handles SockLib.OnSessionRejected
    CallHandle = 0
    UpdateStatus()
    MsgBox("Cannot connect to " & txtIP.Text & ":" & txtPort.Text)
  End Sub




  'The socket is disconnected
  Private Sub SockLib_OnSessionClosed(ByVal sender As Object, ByVal e As AxbsSocketLibrary.IBSSockLibXEvents_OnSessionClosedEvent) Handles SockLib.OnSessionClosed
  Dim i As Long
  For i = 0 To lvPeers.Items.Count - 1
    If lvPeers.Items.Item(i).Tag = e.aHandle Then
      lvPeers.Items.RemoveAt(i)
    End If
  Next i
  UpdateStatus()
  End Sub



  'A new packet is available
  Private Sub SockLib_OnPacketReceived(ByVal eventSender As System.Object, ByVal eventArgs As AxbsSocketLibrary.IBSSockLibXEvents_OnPacketReceivedEvent) Handles SockLib.OnPacketReceived
    Select Case SockLib.GetRcvdCmnd(eventArgs.aHandle)
      Case pcALERT
        Call HaveAlert(eventArgs.aHandle) 'alert message
      Case pcCHAT
        Call HaveChat(eventArgs.aHandle)  'chat message
    End Select
  End Sub








  'Update the buttons state and the status text
  Private Sub UpdateStatus()
    Dim St As String
    Dim LI As ListViewItem
    Dim i, Sk, ConCnt As Integer

    'Buttons

    If CallHandle > 0 Then
      'a call is in progress
      btnConnect.Enabled = False
      btnCancel.Enabled = True
    Else
      'a new call can be initiated
      btnConnect.Enabled = True
      btnCancel.Enabled = False
    End If

    If lvPeers.SelectedItems.Count = 0 Then
      btnRemove.Enabled = False
      btnChat.Enabled = False
      btnAlert.Enabled = False
    Else
      btnRemove.Enabled = True
      btnChat.Enabled = True
      btnAlert.Enabled = True
    End If


    'Status bar

    If fSettings.txtKey.Text > "" Then
      StatusBar.Panels.Item(0).Text = "Encryption: ON"
    Else
      StatusBar.Panels.Item(0).Text = "Encryption: OFF"
    End If

    If fSettings.chkCompress.CheckState = 1 Then
      StatusBar.Panels.Item(1).Text = "Compression: ON"
    Else
      StatusBar.Panels.Item(1).Text = "Compression: OFF"
    End If

    If fSettings.chkServer.CheckState = 1 Then
      StatusBar.Panels.Item(2).Text = "Server: ON"
    Else
      StatusBar.Panels.Item(2).Text = "Server: OFF"
    End If

    If lvPeers.Items.Count = 0 Then
      St = "No active connection"
    Else
      St = Str(lvPeers.Items.Count) & " connected peer(s)"
    End If
    StatusBar.Panels.Item(3).Text = St



  End Sub






End Class
(c) BigSpeed Computing Inc. - Secure private networking