# VB.NET  bug driving me nuts.



## shuggans (Nov 7, 2012)

I've been tryign to debug this for a week and have been unsuccessful finding the issue.  

When runnign through the code below, the second function can never return the TargetID # from the database.  I think there is something wrong with my input send to the function.


```
Function recv() As String
        Dim data(4096) As Byte
        sock.Receive(data, 4096, SocketFlags.None)
        Dim mail As String = System.Text.ASCIIEncoding.UTF8.GetString(data)
        Dim mailpeices As Array = mail.Split(" ")
        If mail.Contains(" ") Then
            If mail.Substring(0, 4) = "PING" Then
                Dim pserv As String = mail.Substring(mail.IndexOf(":"), mail.Length - mail.IndexOf(":"))
                pserv = pserv.TrimEnd(Chr(0))
                mail = "PING from " & pserv & vbNewLine & "PONG to " & pserv
                send("PONG " & pserv)
            ElseIf mail.Contains("PRIVMSG") Then
                Dim tmparr() As String = Nothing
                Dim rnick As String
                Dim rmsg As String
                mail = mail.Remove(0, 1)
                tmparr = mail.Split("!")
                rnick = tmparr(0)
                tmparr = mail.Split(":")
                rmsg = tmparr(1)
                mail = "msg: " & rnick & ">" & rmsg

                'command identifier
                If rmsg.StartsWith("!RXGB ") Then

                    ' commands()
                    If rmsg.StartsWith("!RoXTELL join") Then
                        send("JOIN " & Channel)

                    ElseIf rmsg.StartsWith("!RXGB test2") Then
                        send("PRIVMSG " & Channel & " :I LIKE BREAD AND BUTTAAAAA!")

                    ElseIf rmsg.StartsWith("!RXGB test") Then
                        send("PRIVMSG " & rnick & " :Do you like Corn Syrup?")

                    End If
                ElseIf rmsg.StartsWith("!RoXTELL join") Then
                    send("JOIN " & Channel)

                End If


                If rmsg.StartsWith("!RoXATTACK ") Then
                    Dim MessageParts() As String = rmsg.Split(Chr(32))
                    Dim MessageArray As Array = rmsg.Split(" ")

                    Dim Target As String = MessageParts(2) ' need proper case
                    Target = Target.Trim(" ")
                    Dim STAT_PLAYER_Attack As Integer
                    Dim STAT_TARGET_Defense As Integer
                    ' Dim STAT_TARGET_HP As Integer

                    Select Case MessageParts(1).ToLower
                        Case "standard"

                            'grab PLAYER character ID
                            Dim CharacterID As Integer = GrabCharacterID(rnick)

                            Dim ReadStatPlayerAttackConnection As MySqlConnection = New MySqlConnection(mySQL_Server_String)
                            Dim ReadStatPlayerAttackString As String = "select Attack from Stats_" & rnick & " where ID = '" & CharacterID & "';"
                            Dim ReadStatPlayerAttackCommand As New MySqlCommand(ReadStatPlayerAttackString, ReadStatPlayerAttackConnection)
                            Dim ReadStatPlayerAttackReader As MySqlDataReader
                            Dim ReadStatPlayerAttackResult As Integer

                            Try

                                ReadStatPlayerAttackConnection.Open()
                                ReadStatPlayerAttackReader = ReadStatPlayerAttackCommand.ExecuteReader()
                                ReadStatPlayerAttackReader.Read()
                                ReadStatPlayerAttackResult = ReadStatPlayerAttackReader.GetInt16(0)
                                STAT_PLAYER_Attack = ReadStatPlayerAttackResult

                                cout.WriteLine(STAT_PLAYER_Attack)

                                ReadStatPlayerAttackReader.Close()
                                ReadStatPlayerAttackConnection.Close()
                                ReadStatPlayerAttackReader.Dispose()
                                ReadStatPlayerAttackConnection.Dispose()


                                'grab TARGET character ID
                                Dim TargetID As Integer = GrabTargetID(Target)
                                cout.WriteLine(Target & ":" & TargetID.ToString)

                                send("PRIVMSG " & Channel & " :Target is " & Target & ".  Target ID is " & TargetID.ToString)

                                Try

                                    Dim ReadStatTargetDefenseConnection As MySqlConnection = New MySqlConnection(mySQL_Server_String)
                                    Dim ReadStatTargetDefenseString As String = "select Defense from Stats_" & Target & " where ID = '" & TargetID & "';"
                                    Dim ReadStatTargetDefenseCommand As New MySqlCommand(ReadStatTargetDefenseString, ReadStatTargetDefenseConnection)
                                    Dim ReadStatTargetDefenseReader As MySqlDataReader
                                    Dim ReadStatTargetDefenseResult As Integer

                                    ReadStatTargetDefenseConnection.Open()
                                    ReadStatTargetDefenseReader = ReadStatTargetDefenseCommand.ExecuteReader()
                                    ReadStatTargetDefenseReader.Read()
                                    ReadStatTargetDefenseResult = ReadStatTargetDefenseReader.GetInt16(0)
                                    STAT_TARGET_Defense = ReadStatTargetDefenseResult

                                    cout.WriteLine(STAT_TARGET_Defense)

                                    ReadStatTargetDefenseReader.Close()
                                    ReadStatTargetDefenseConnection.Close()
                                    ReadStatTargetDefenseReader.Dispose()
                                    ReadStatTargetDefenseConnection.Dispose()


                                    '   Try
                                    '
                                    '  Dim AttackDamage As Integer
                                    ' AttackDamage = STAT_PLAYER_Attack - STAT_TARGET_Defense

                                    ' Dim STAT_TARGET_NewHP As Integer
                                    ' STAT_TARGET_NewHP = STAT_TARGET_HP - AttackDamage

                                    '  Dim writeline As String = rnick & " attacks " & Target & " for " & AttackDamage.ToString & " points of damage."
                                    '   writeline = writeline.TrimEnd(Chr(0))
                                    '   writeline = writeline.Remove(writeline.LastIndexOf(vbLf), 1)
                                    '   writeline = writeline.Remove(writeline.LastIndexOf(vbCr), 1)
                                    '   cout.WriteLine(writeline)


                                    '    Catch
                                    '    Dim writeline As String = "Error calculating damage"
                                    '     writeline = writeline.TrimEnd(Chr(0))
                                    '   writeline = writeline.Remove(writeline.LastIndexOf(vbLf), 1)
                                    '     writeline = writeline.Remove(writeline.LastIndexOf(vbCr), 1)
                                    '     cout.WriteLine(writeline)
                                    '  End Try


                                Catch
                                    Dim writeline As String = "Error grabbing target ID or Defense Stat"
                                    cout.WriteLine(writeline)
                                End Try
                            Catch
                                Dim writeline As String = "Error grabbing player attack stat"
                                cout.WriteLine(writeline)
                            End Try

                    End Select
                    mail = ""
                End If




            ElseIf mail.Contains("IDENTIFY") Then
                send("PRIVMSG nickserv :identify R@sM8V0Us")
            ElseIf mail.Contains("recognized") Then
                send("JOIN " & Channel)
            ElseIf mail.Contains("JOIN") Then
                If mailpeices(1) = "JOIN" Then
                    send("PRIVMSG " & Channel & " " & mailpeices(0) & " joined " & Channel & ".")
                Else
                End If

            ElseIf mail.Contains(":") Then

            End If
        End If
        Try
            mail = mail.TrimEnd(Chr(0))
            mail = mail.Remove(mail.LastIndexOf(vbLf), 1)
            mail = mail.Remove(mail.LastIndexOf(vbCr), 1)
            Return mail
        Catch

        End Try

    End Function

    Public Function GrabCharacterID(ByVal ProperName As String)
        'Grab the newly created character's character # from the index

        Dim GrabCharacterIDConnection As MySqlConnection = New MySqlConnection(mySQL_Server_String)
        Dim GrabCharacterIDString As String = "select ID from Characters where Name = '" & ProperName & "';"
        Dim GrabCharacterIDCommand As New MySqlCommand(GrabCharacterIDString, GrabCharacterIDConnection)
        Dim GrabCharacterIDReader As MySqlDataReader
        Dim Result As Integer = -1
        Try
            GrabCharacterIDConnection.Open()
            GrabCharacterIDReader = GrabCharacterIDCommand.ExecuteReader()
            GrabCharacterIDReader.Read()
            Result = GrabCharacterIDReader.GetInt16(0)
            Return Result
        Catch e As Exception
            Return Result
        End Try

    End Function

    Public Function GrabTargetID(ByVal TargetName As String)
        'Grab the newly created character's character # from the index
        TargetName = TargetName.Trim(" ")
        Dim GrabCharacterIDConnection As MySqlConnection = New MySqlConnection(mySQL_Server_String2)
        Dim GrabCharacterIDString As String = "select ID from Characters where Name = '" & TargetName & "';"
        Dim GrabCharacterIDCommand As New MySqlCommand(GrabCharacterIDString, GrabCharacterIDConnection)
        Dim GrabCharacterIDReader As MySqlDataReader
        Dim Result As Integer = -1
        Try
            GrabCharacterIDConnection.Open()
            GrabCharacterIDReader = GrabCharacterIDCommand.ExecuteReader()
            GrabCharacterIDReader.Read()
            Result = GrabCharacterIDReader.GetInt16(0)
            Return Result
        Catch e As Exception
            Return Result
        End Try

    End Function
```

As I say - I think there is something with the input sent to the function, as the following line:

```
send("PRIVMSG " & Channel & " :Target is " & Target & ".  Target ID is " & TargetID.ToString)
```
only ever returns "Target is <target>" and cuts the rest off afterwards.  

making a gui version of this resulkts in the same thing.  

The CharacterID (From function GrabCharacterID) is returned properly, but not the TargetID (from fucntion GrabTaretID, which is a copy/paste of GrabCharacterID).  I think I have it narrowed down the the name I am sending to use to retrieve the ID.  Somehow Im not stripping off a linefeed or carriage return or something properly.

Any ideas?  I'm really at a loss here.


----------



## shuggans (Nov 7, 2012)

running the console application... I get a bunch of blank lines after the targetname when printing it to the console.


----------



## shuggans (Nov 8, 2012)

No one has any ideas?


----------



## FordGT90Concept (Nov 8, 2012)

What is "sock" definied as?  When you put in breakpoints, which line fails? The "send" line?  Have you broke at Target and TargetID to confirm they have valid data?


----------



## shuggans (Nov 9, 2012)

*Solution*

My issue was:
I had my received byte defined as 4096.
So if a message was received that was less than this amount of data, white space was added to make it 4096.  

Because I was breaking strings down into arrays based on spaces, the last item in the array contained the text + the whitespace.  ran a for loop which ran through each character and added it together into a new version of the string, but only IF the character showed up as a letter.

instead of sending
"MyName"

it would search for

"MyName                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               



"

Works great now.  Thanks for the reply Ford!


----------



## FordGT90Concept (Nov 9, 2012)

FYI, you can trim that by doing string.Trim(Chr(0)) in VB or string.Trim('\0') in C#.  Good programming practice is to size the packets to its contents, however.


----------

