Int + String in one UDP Message - Byte Ptr Prob?

BlitzMax Forums/BlitzMax Programming/Int + String in one UDP Message - Byte Ptr Prob?

Manromen(Posted 2008) [#1]
Hi,

i have some problems with my UDP Transfer. My Server don't get a correct String. The Integers are just fine. It seems to be a Misstake at the Byte Pointers or so ...

Maybe anyone see my Misstake:

Server:


Client:


I'm very thankful for some hints :-)


Brucey(Posted 2008) [#2]
I know this doesn't help your current problem, but..
data[1] = Byte Ptr(name.ToCString())

This is a memory leak.... unless you call MemFree on the byte ptr at some point.

FYI.


Brucey(Posted 2008) [#3]
Actually, you are not sending the string anywhere.... only a byte ptr reference.

You will need to copy the characters into a byte stream and read those number of bytes out of the other end (using the length as the indicator to size of string) - ie. your data byte array will need to be longer than 4 bytes... you will need 3 + max string length...
This assumes that length, x and y are <= 255. Otherwise you will need more bytes for those too.


Manromen(Posted 2008) [#4]
Okay, thanks for the hint :-)

i will try again and tell the results tomorrow :-)


Brucey(Posted 2008) [#5]
Let's pretend you do in fact want to send :
Local name:String = "Player"
Local xpos:Int = 450
Local ypos:Int = 400

You will need at least.... 7 + 2 + 2 bytes to transfer using non-compressed stream.

If you don't include Length, you can get away with having a null-terminator (ascii-0) at the end of the string, then two bytes (a short) per coord.

Something perhaps like :
SuperStrict

Local name:String = "Player"
Local xpos:Int = 450
Local ypos:Int = 400


Local data:Byte[] = New Byte[11]
Local offset:Byte Ptr = data
For Local i:Int = 0 Until name.length
	offset[i] = name[i]
Next
offset:+ name.length + 1
Short Ptr(offset)[0] = xpos
offset:+2
Short Ptr(offset)[0] = ypos


For Local i:Int = 0 Until data.length
	Print data[i]
Next



Manromen(Posted 2008) [#6]
Oh wow thanks for your work :-D


Brucey(Posted 2008) [#7]
You might find it easier to work with a TBank while you are building your data to send, as it has functions for poking Byte, Short, Int, Long, Float, Double etc...

And be aware of Endian issues if you intend passing data between Intel and PPC systems.


Jesse(Posted 2008) [#8]
I don't know if it would work any other way but if you are sendign bytes then it should be something like this (untested):

it should give you an idea


Manromen(Posted 2008) [#9]
@Jesse
Hey yeah that works - thanks alot :-D

@Brucey
Thanks for the hints - i'll play around with TBanks :-)
Lucky that there is a ?PPC Command - will keep it in mind Thanks :-)


Manromen(Posted 2008) [#10]
Hey, i got it working with TBank now :-)

Client:


Server:


Maybe anyone can use or learn from this :-)

Thanks to Brucey and Jesse for Help and Hints :-)

[Edit]
Btw: I do have to watchout for Endian issues within TBanks ?


Jesse(Posted 2008) [#11]
After it is done reading, don't forget to reset your pointer so it can read from the begging when new data is received:
SeekStream(data,0)



Manromen(Posted 2008) [#12]
Nice Idea :-)

I updated the Post above.