Sockets index out if range
Sockets index out if range
Sockets index out if range
Sockets index out if range
Sockets index out if range
Sockets index out if range Sockets index out if range Sockets index out if range Sockets index out if range Sockets index out if range Sockets index out if range Sockets index out if range Sockets index out if range
Sockets index out if range Sockets index out if range
Sockets index out if range
Go Back  Xtreme Visual Basic Talk > > > Sockets index out if range


Reply
 
Thread Tools Display Modes
  #1  
Old 04-05-2013, 04:03 AM
TrueBriT TrueBriT is offline
Newcomer
 
Join Date: Mar 2007
Posts: 5
Default 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
Reply With Quote
  #2  
Old 04-05-2013, 08:25 AM
AtmaWeapon's Avatar
AtmaWeaponSockets index out if range AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

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.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Sockets index out if range
Sockets index out if range
Sockets index out if range Sockets index out if range
Sockets index out if range
Sockets index out if range
Sockets index out if range Sockets index out if range Sockets index out if range Sockets index out if range Sockets index out if range Sockets index out if range Sockets index out if range
Sockets index out if range
Sockets index out if range
 
Sockets index out if range
Sockets index out if range
 
-->