Remote Chat Part II (For Client)


Option Explicit

Public NickName As String

Private Type CLIENT
    IP As String
    Port As String
    DisplayName As String
End Type
Private BROADCAST As Boolean

Private Declare Function FindWindow Lib “user32” Alias “FindWindowA” (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Dim buf As String

Private Sub Check1_Click()
On Error GoTo Trap

    BROADCAST = IIf(Check1.Value = vbChecked, True, False)
   
    Exit Sub
Trap:
    If Err.Number > 0 Then MsgBox “Error : ” & Err.Number & ” ” & vbNewLine & Err.Description, vbCritical
    Resume Next
End Sub

Private Sub Command1_Click()
On Error GoTo Trap
    If Not BROADCAST Then
        Tower.SendData “!HEAD-!@” & “<” & RTB.Text & “->(” & List1.Text & “-)@” & Tower.RemotePort & “-@%” & NickName & “-%”
        RTB.Text = Empty
    Else
        Tower.SendData “!BROADCAST-!@” & Tower.RemotePort & “-@” & “<” & RTB.Text & “->(” & Combo1.Text & “-)%” & NickName & “-%”
        RTB.Text = Empty
    End If
    Command1.Enabled = False
Exit Sub
Trap:
    If Err.Number > 0 Then MsgBox “Error : ” & Err.Number & ” ” & vbNewLine & Err.Description, vbCritical
   
    Resume Next
End Sub

Private Sub Form_Load()
On Error GoTo Trap
    Dim CD As CLIENT
    Tower.RemoteHost = InputBox(“Enter Server IP”)
GetName:
    NickName = InputBox(“Enter Your Nick Name”)
    If NickName = “ADMIN” Then NickName = Empty: GoTo GetName
    Tower.LocalPort = 4444
    Tower.Bind
    CD.IP = Tower.LocalIP
    CD.Port = Tower.LocalPort
    CD.DisplayName = NickName
    Register CD
    Tower.SendData “!LIST_CLIENTS-!^” & CD.IP & “-^@” & CD.Port & “-@”
Exit Sub
Trap:
    If Err.Number > 0 Then MsgBox “Error : ” & Err.Number & ” ” & vbNewLine & Err.Description, vbCritical
   
    Resume Next
End Sub

Private Sub Register(ByRef pCD As CLIENT)
On Error GoTo Trap
    Dim msg As String
    msg = “!REGISTER-!^” & pCD.IP & “-^@” & pCD.Port & “-@$” & pCD.DisplayName & “-$”
    Tower.SendData msg
Exit Sub
Trap:
    If Err.Number > 0 Then MsgBox “Error : ” & Err.Number & ” ” & vbNewLine & Err.Description, vbCritical
   
    Resume Next
End Sub

Private Function GetMessage(ByRef msg As String) As String
On Error GoTo Trap
    Dim posS As Integer, posE As Integer, DN As String
    posS = InStr(1, buf, “<“): posE = InStr(1, buf, “->”)
    GetMessage = Mid$(buf, posS + 1, posE – posS)
Exit Function
Trap:
    If Err.Number > 0 Then MsgBox “Error : ” & Err.Number & ” ” & vbNewLine & Err.Description, vbCritical
   
    Resume Next
End Function

Private Sub Form_Unload(Cancel As Integer)
On Error GoTo Trap
    UnRegister
Exit Sub
Trap:
    If Err.Number > 0 Then MsgBox “Error : ” & Err.Number & ” ” & vbNewLine & Err.Description, vbCritical
   
    Resume Next
End Sub

Private Sub UnRegister()
On Error GoTo Trap
    Tower.SendData “!UNREGISTER-!^” & Tower.LocalIP & “-^”
Exit Sub
Trap:
    If Err.Number > 0 Then MsgBox “Error : ” & Err.Number & ” ” & vbNewLine & Err.Description, vbCritical
   
    Resume Next
End Sub

Private Sub RTB_Change()
    If RTB.Text = Empty Then Command1.Enabled = False Else Command1.Enabled = True
End Sub

Private Sub Tower_DataArrival(ByVal bytesTotal As Long)
On Error GoTo Trap
    buf = Empty
    Tower.GetData buf
    Dim abuf() As String, pos As Integer
    Dim posS As Integer, posE As Integer, DN As String
    If InStr(1, buf, “!LIST_CLIENTS-!”) > 0 Then
        pos = InStr(1, buf, “!-LIST_CLIENTS-!”)
        buf = Mid$(buf, Len(“!-LIST_CLIENTS-!”))
        abuf = Split(buf, “#”)
        List1.Clear
        For pos = 0 To UBound(abuf) – 1
            List1.AddItem abuf(pos)
        Next
    ElseIf InStr(1, buf, “!-IM_CLIENT-!”) > 0 Then
        posS = InStr(1, buf, “:”): posE = InStr(1, buf, “-:”)
        DN = Mid$(buf, posS + 1, posE – posS)
    Else
        AddToHistory buf
    End If
Exit Sub
Trap:
    If Err.Number > 0 Then MsgBox “Error : ” & Err.Number & ” ” & vbNewLine & Err.Description, vbCritical
   
    Resume Next
End Sub

Private Sub AddToHistory(ByRef msg As String)
On Error GoTo Trap
    History.Text = History.Text & msg & vbNewLine & vbNewLine
Exit Sub
Trap:
    If Err.Number > 0 Then MsgBox “Error : ” & Err.Number & ” ” & vbNewLine & Err.Description, vbCritical
   
    Resume Next
End Sub

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s