• Welcome to Valhalla Legends Archive.
 

[VB.Net] PacketBuffer Class

Started by Noodlez, March 18, 2003, 04:50 PM

Previous topic - Next topic

Noodlez

i read an article on vb.net today and it motivated me to make a bot, so to get other people started heres a packetbuffer class i wrote

'PacketBuffer class by Noodlez
Public Class PacketBuffer
    Private buffer As String
    Public Sub InsertDWORD(ByVal DWord As Int32) 'VB.Net provides types for DWord and Word
        buffer = buffer & DWord.ToString
    End Sub
    Public Sub InsertWORD(ByVal Word As Int16)
        buffer = buffer & Word.ToString
    End Sub
    Public Sub InsertBYTE(ByVal b As Byte)
        buffer = buffer & b.ToString
    End Sub
    Public Sub InsertString(ByVal data As String, Optional ByVal NT As Boolean = True)
        'NT = NullTerminated, if you don't want the string to be nullterminated just set
        '   that to false
        If NT = False Then
            buffer.Insert(Len(buffer), data)
        Else
            buffer.Insert(Len(buffer), data & vbNullChar)
        End If
    End Sub
    Public Sub Clear()
        buffer = ""
    End Sub
    Public Function SendPacket(ByVal Socket As LSock, ByVal PacketID As Byte) As Boolean
        'feel free to replace LSock with whatever winsock class you use
        '(and modify the function if necessary)
        Dim length As Int16
        length = Len(buffer) + 4
        If Socket.State = LSock.Status.Connected Then
            Socket.SendData(ChrW(255) & length.ToString & buffer)
            Return True
        End If
        Clear()
        Return False
    End Function
End Class
enjoy

this is untested code, if you find error in my code, or make any optimizations please let me know

n00blar

If you're basing this off of DarkMinion's PacketBuffer, then I recommend redesigning it (i didn't check it out), because his has a few flaws/problems you will run into when sending packets & et cetera

Mesiah / haiseM

#2
it does? i modified his only a little, and its worked fine for me for about 2 years now...
]HighBrow Innovations
Coming soon...

AIM Online Status: 

dxoigmn

If anyone has actually tried this, it doesn't work.  The 'ToString' method converts the object (Int32, Int16, both of which are an Integer and Short in VB.NET) to the literal string.  For example, if 'myInt' was declared as Integer with a value of 12, then myInt.ToString() would return "12" as a string -- the same thing CStr(12) would do in VB6.  Another thing, why don't you take advantage of VB.NET's overloading ability?  Also, the class should be as abstract as possible, and not reference other objects such as LSock (a Winsock-like implementation in VB.NET).  Seriously, this is horrible code and you consider testing it before you release anything.  This is what causes confusion and is a fuels for much of the bad code that is already on this board.

Edit: grammar :\

Noodlez

#4
didn't read anything about overloading, and thanks for pointing out what ToString does.

n00blar, this was straight off my head. mesiah, he was referring to the C++ packetbuffer (i think?)

kamikazaie, do you have a packetbuffer class i can see?

dxoigmn

#5
I generally hate giving out code for personal reasons.  Though I will give you a hint as to what I did to accomplish this.  Try taking a look at System.BitConverter.GetBytes().  If anyone knows an easier way, feel free to post away.  My knowledge of the .NET Namespaces is not that great, nor do I have a hardcopy reference of them.  I mostly just scour the internet for references.

Camel@sk00l

#6
i use copymemory w/ vb6. unless i'm horribly mistaken, you could use that in .net too.
Public Function MakeDWord(DWord As Int32)
    CopyMemory MakeDWord, ByVal DWord, 4
End Function
or something like that. if you know any c, it helps to note that byref is implied, and is the same as the c & operator. so, you generally (always?) want to do "CopyMemory <destination>, ByVal <source>, <len(source)>", so that it takes the value of <source>, and copys <len(source)> bytes to a place in memory starting at <ByVal <source>>

oh by the way, if you write a string->word/dword function, make sure you check that its at least 2/4 bytes before calling copymemory...and save before you test it ;)

Banana fanna fo fanna

#7
Does .NET have something equivalent to ByteBuffer?

Zeller

#8
Can some one explain why we have to convert longs and bytes to strings in order to send it to bnet?

tA-Kane

QuoteCan some one explain why we have to convert longs and bytes to strings in order to send it to bnet?
The socket only accepts strings for input to be sent.

Instead of thinking it as a string input, think of it as a "raw input", or a binary input. Then, instead of thinking that you're converting the longs to a string, think that you're just making it so that you're able to send the long.

If you were using a good programming environment (example: not VB), you would just pass the address of data to be sent and the length of the data to be sent, and then the socket would then read and write the data as is (so, a long would be sent "as a long", instead of "as a string", in VB terms), to the network.

Just as long as the end result is the same, it really doesn't matter how you get the job done.
Macintosh programmer and enthusiast.
Battle.net Bot Programming: http://www.bash.org/?240059
I can write programs. Can you right them?

http://www.clan-mac.com
http://www.eve-online.com

Banana fanna fo fanna

#10
VB sucks and isn't a programming language.
For christ's sake, I'd rather write in JavaScript!

Mesiah / haiseM

#11
still gets the job done, i can do anything anybody else can do, might require a little more time and work, but it can be done.
]HighBrow Innovations
Coming soon...

AIM Online Status: 

Brendan

#12
with only 2 megabytes wasted space on your hard drive ;)

Grok

#13
QuoteVB sucks and isn't a programming language.
For christ's sake, I'd rather write in JavaScript!

Hehe, you're a sucky fisherman.  Everyone knows your old stinky bait.

What's your evaluation of PHP.  I just did my own evaluation but yours should be interesting.

Tuberload

#14
I personally think PHP is a good scripting language... Not much of an evaluation I guess.
Quote"Pray not for lighter burdens, but for stronger backs." -- Teddy Roosevelt
"Your forefathers have given you freedom, so good luck, see you around, hope you make it" -- Unknown