VB/VB.Net Problème d'exception IndexOutOfRange

A

Anonymous

Invité
#1
Voilà j'ai un problème assez ennuyeux, j'ai suivis le tutoriel de RaphyTheGeek (merci à lui au passage) pour apprendre à créer un bot très simple qui pour l'instant ne ferait que se connecter. Bon c'est pas pour tout de suite, je ne me suis pas encore frotté au cryptage du mot de passe etc... Tout cela m'a l'air bien compliqué. Mais je suis déterminé.

Bref j'en viens à mon problème. Voici mon code.

Code:
Imports System.Net.Sockets
Imports System.Threading

Public Class FORM_FENETRE

    Delegate Sub MyDelegate(ByVal Text As String)

    Private _socket As Socket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
    Private _ReceptionThread As Thread
    Private _buffer(8191) As Byte

#Region "PrivateMethod"

    Private Sub Log(ByVal Text As String)
        If Me.TB_INFOS.InvokeRequired Then
            Dim d As New MyDelegate(AddressOf Log)
            BeginInvoke(d, Text)
        Else
            Me.TB_INFOS.Text = Me.TB_INFOS.Text + Text & vbCrLf
            Me.TB_INFOS.Select(TB_INFOS.Text.Length, 0)
            Me.TB_INFOS.ScrollToCaret()
        End If
    End Sub

    Private Sub BT_CONNEXION_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BT_CONNEXION.Click
        Try
            _socket.Connect("213.248.126.180", 5555)
            If _socket.Connected Then
                Log("La connection au serveur d'authentification est réussie.")
                _ReceptionThread = New Thread(New ThreadStart(AddressOf Reception))
                _ReceptionThread.Start()
            Else
                Log("La connexion a échoué.")
            End If
        Catch ex As SocketException
            Log("[SocketException] " & ex.Message)
        End Try
    End Sub

    Private Sub Reception()
        While (_socket.Connected)
            ReDim Preserve _buffer(_socket.Available)
            If _buffer.Length > 0 Then
                _socket.Receive(_buffer)
                ParseData(_buffer)
            End If
        End While
    End Sub

    Private Sub ParseData(ByVal DataToParse() As Byte)
        Dim index As Integer = 0
        Dim id_and_packet_lenght_type, packet_id, packet_lenght_type As UShort
        Dim packet_lenght = 0
        Dim packet_content() As Byte
        While index <> DataToParse.Length
            ReDim Preserve DataToParse(DataToParse.Length)
            id_and_packet_lenght_type = DataToParse(index) * 256 + DataToParse(index + 1)
            packet_id = id_and_packet_lenght_type >> 2
            packet_lenght_type = id_and_packet_lenght_type & 3

            index += 2

            If packet_lenght_type = 0 Then
                packet_lenght = 0
            ElseIf packet_lenght_type = 1 Then
                packet_lenght = DataToParse(index)
            ElseIf packet_lenght_type = 2 Then
                packet_lenght = DataToParse(index) * 256 + DataToParse(index + 1)
            ElseIf packet_lenght_type = 3 Then
                packet_lenght = DataToParse(index) * 65536 + DataToParse(index + 1) * 256 + DataToParse(index + 2)
            End If
            ReDim Preserve packet_content(CInt(packet_lenght))
            Array.Copy(DataToParse, index + packet_lenght_type, packet_content, 0, packet_lenght)

            Dim content_hex As String = String.Empty
            Dim huit_bytes As Integer = 0
            For Each b As Byte In packet_content
                If huit_bytes = 8 Then

                    content_hex = content_hex & vbCrLf
                    huit_bytes = 0
                End If
                content_hex += b.ToString("X2") + " "
                huit_bytes = huit_bytes + 1
            Next

            Log("[Reçu] ID = " & packet_id & " | Taille du contenu = " & packet_lenght & vbCrLf & content_hex)

            TreatPacket(packet_id, packet_content)

            index += packet_lenght + packet_lenght_type
        End While
    End Sub

    Sub TreatPacket(ByVal PacketID As Integer, ByVal PacketContent() As Byte)

    End Sub

#End Region

End Class

Lorsque je lance le déboggage, une exception me saute aux yeux (L'exception IndexOutOfRange n'a pas été gérée) à cette ligne précise :

Code:
packet_lenght = DataToParse(index) * 65536 + DataToParse(index + 1) * 256 + DataToParse(index + 2)
Je n'arrive vraiment pas à comprendre où est mon problème, j'ai vraiment beaucoup cherché mais mon problème persiste. Bref j'aurais besoin de conseils et d'un peu d'aide parce que ce tutoriel est en C# et c'est pas toujours facile de suivre en VB...


Merci d'avance pour votre aide ! :D
 

ToOnS

Membre Actif
Inscrit
8 Avril 2009
Messages
974
Reactions
0
#2
Salut , combien y'a t'il de byte(s) dans DataToParse quand l'erreur survient ? si il y'a plus de 6 bytes peut on avoir seulement les 6 premiers ?

(ce parseur j'arrete pas de le repeter mais il est moisi , si le message , je parle pas de paquet mais de message , depasse 8k alors ca sera pas geré car il sera sur plusieurs paquets)
 
A

Anonymous

Invité
#3
Donc il existe une autre manière pour parser? En fait ici je n'arrive pas à comprendre que mettre dans

Code:
Redim preserve DataToParse(??)
J'ai un peu regardé et j'ai bien compris qu'il y avait un problème dans la taille du DataToParse.

Dans le tutoriel de RaphyTheGeek, il ne semble pas redimensionner le tableau de byte entré. Or si je fais ça, je suis certain d'avoir une exception encore à un autre endroit cette fois...

Code:
private void ParseData(byte[] DataToParse)
        {
            int index = 0;
            short id_and_packet_lenght_type, packet_id, packet_lenght_type;
            Int32 packet_lenght = 0;
            byte[] packet_content;

            while (index != DataToParse.Length)
            {
                id_and_packet_lenght_type = (short)(DataToParse[index] * 256 + DataToParse[index + 1]); 
                packet_id = (short)(id_and_packet_lenght_type >> 2);
                packet_lenght_type = (short)(id_and_packet_lenght_type & 3); 

                index += 2; 

                  [...]
 
Haut Bas