Xtreme Visual Basic Talk

Xtreme Visual Basic Talk (http://www.xtremevbtalk.com/)
-   .NET Communications (http://www.xtremevbtalk.com/-net-communications/)
-   -   Sockets index out if range (http://www.xtremevbtalk.com/-net-communications/325792-sockets-index-range.html)

TrueBriT 04-05-2013 03:03 AM

Sockets index out if range
 
Hi guys im trying to connect to a game server that i currently have running from home, ive been doing a little bit of research on this but i can't seem to find out where im goin wrong.

i keep getting system.IndexOutOfRangeException error
also i have had once socket exception error heres my code

Code:

Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Net.Sockets
Imports System.Net

Public Class frmRcon

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        sendComm("status", "5.70.250.66", "", 28960)
    End Sub

  Private Function sendComm(ByVal rconComm As String, ByVal gameIP As String, ByVal gamePass As String, ByVal gamePort As Integer) As String
        On Error Resume Next

        Dim client As New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
        client.Connect(IPAddress.Parse(gameIP), gamePort)

        Dim command As String

        command = rconComm

        Dim bufferTemp() As Byte = Encoding.ASCII.GetBytes(command)
        Dim buffersend(bufferTemp.Length + 4) As Byte

        buffersend(0) = Byte.Parse("255")
        buffersend(1) = Byte.Parse("255")
        buffersend(2) = Byte.Parse("255")
        buffersend(3) = Byte.Parse("255")

        Dim j As Integer = 4
        For i As Integer = 0 To bufferTemp.Length - 1
            buffersend(j) = bufferTemp(i)
            j += i
        Next i

        Dim remoteIpEndPoint As New IPEndPoint(IPAddress.Any, 0)
        client.Send(buffersend, SocketFlags.None)

        Dim bufferRec(64999) As Byte
        client.Receive(bufferRec)
        Return Encoding.ASCII.GetString(bufferRec)

    End Function
End Class


AtmaWeapon 04-05-2013 07:25 AM

Focus on this bit of code:
Code:

Dim j As Integer = 4
For i As Integer = 0 To bufferTemp.Length - 1
    buffersend(j) = bufferTemp(i)
    j += i
Next i

Think about what the indexes might be on each loop. Let's say bufferTemp has 10 bytes in it. That means buffersend will have 15 bytes in it. (10 + 4 = 14, the number you give an array sets the upper bound, not the size, so 0-14 is 15 elements.)

So keep in mind the biggest i should ever get is 9, and the biggest j should get is 15.
  • On the first iteration, i = 0 and j = 4. Then j := (4 + 0) = 4.
  • Second: i = 1, j = 4. j := (4 + 1) = 5
  • Third: i = 2, j = 5; j := (5 + 2) = 7
  • Fourth: i = 3, j = 7; j := (7 + 3) = 10
  • Fifth: i = 4, j = 10; j := (10 + 4) = 14
  • Sixth: i = 5, j = 14; j := (14 + 5) = 19
I think you meant j += 1, but I would've wrote the loop like this:
Code:

For i As Integer = 0 To bufferTemp.Length - 1
    buffersend(i + 4) = bufferTemp(i)
    j += i
Next i

Alternately, there are overloads of Array.Copy() that will safely copy an array.


All times are GMT -6. The time now is 08:17 PM.

Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Optimisation provided by DragonByte SEO v2.0.15 (Lite) - vBulletin Mods & Addons Copyright © 2017 DragonByte Technologies Ltd.
All site content is protected by the Digital Millenium Act of 1998. Copyright©2001-2011 MAS Media Inc. and Extreme Visual Basic Forum. All rights reserved.
You may not copy or reproduce any portion of this site without written consent.