What on earth am I doing wrong??

Just starting out? Need help? Post your questions and find answers here.
EvilNoodle
New User
New User
Posts: 7
Joined: Mon Jun 30, 2008 9:58 pm

What on earth am I doing wrong??

Post by EvilNoodle »

Hi,

I am trying to write some network code but to do that I need to be able to construct my packets.

I wrote the code below to push into a message...

The type of the message as a byte
The length of the message content as a long
The message as a string

and read it back out again to make sure I didn't suffer from garbage in garbage out.

Code: Select all

Debug("===> Setting up message data...")
messageType.b = 1
message.s = "Test Message :)"
messageLength.l = Len(message)

Debug("===> Printing source message data...")
Debug("")
Debug("--- Source Message Begins ---")
Debug("Message type : " + Str(messageType))
Debug("Message length : " + Str(messageLength))
Debug("Message : " + message)
Debug("--- Source Message Ends---")
Debug("")

Debug("===> Allocating memory block (size = " + Str((5 + messageLength)) + ")...")

;Allocate 1 byte for the type (byte), 4 for the length (long) and the length of the message string
*packet = AllocateMemory(5 + messageLength)

Debug("===> Writing message to memory block...")
PokeB(*packet, messageType)
PokeL(*packet, messageLength)
PokeS(*packet, message)

Debug("===> Extracting message from memory block...")
msgType.b = PeekB(*packet)
msgLen.l = PeekL(*packet)
msg.s = PeekS(*packet)

Debug("===> Printing extracted message data...")
Debug("")
Debug("--- Extracted Message Begins ---")
Debug("Message type : " + Str(msgType))
Debug("Message length : " + Str(msgLen))
Debug("Message : " + msg)
Debug("--- Extracted Message Ends ---")
Debug("")
Is this code correct or am I being really really really stupid because instead of getting the values I expect out...

Message type : 1
Message length : 15
Message : Test Message :)

What I actually get is

Message type : 84
Message length : 1953719636
Message : Test Message :)

Here is the full output...

Code: Select all

===> Setting up message data...
===> Printing source message data...

--- Source Message Begins ---
Message type : 1
Message length : 15
Message : Test Message :)
--- Source Message Ends---

===> Allocating memory block (size = 20)...
===> Writing message to memory block...
===> Extracting message from memory block...
===> Printing extracted message data...

--- Extracted Message Begins ---
Message type : 84
Message length : 1953719636
Message : Test Message :)
--- Extracted Message Ends ---


This is driving me nuts and my only conclusion can be that I am doing something fundamentally wrong here. Any help would be greatly appreciated.

Noodle
maw

Post by maw »

Oh dear, oh dear.. You're going to hit yourself over the head for this :lol:

Your are actually writing, and reading, the byte, long and string at the same memory address! You need to increase the pointer for each read and write!

Code: Select all

PokeB(*packet, messageType)
PokeL(*packet + 1, messageLength)
PokeS(*packet + 5, message) 

Code: Select all

msgType.b = PeekB(*packet)
msgLen.l = PeekL(*packet + 1)
msg.s = PeekS(*packet + 5) 
EvilNoodle
New User
New User
Posts: 7
Joined: Mon Jun 30, 2008 9:58 pm

Post by EvilNoodle »

Thanks a lot for that. I was recently using a different language that maintaines its own read pointer.

Its woking fine now :)
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Code: Select all

Structure MESSAGE
  type.b
  length.l
  content.b[0]
EndStructure

Debug("===> Setting up message data...") 
messageType.b = 1 
message.s = "Test Message :)" 
messageLength.l = Len(message) 

Debug("===> Printing source message data...") 
Debug("") 
Debug("--- Source Message Begins ---") 
Debug("Message type : " + Str(messageType)) 
Debug("Message length : " + Str(messageLength)) 
Debug("Message : " + message) 
Debug("--- Source Message Ends---") 
Debug("") 

Debug("===> Allocating memory block (size = " + Str((5 + messageLength)) + ")...") 

;Allocate 1 byte for the type (byte), 4 for the length (long) and the length of the message string 
*packet.MESSAGE = AllocateMemory(5 + messageLength) 

Debug("===> Writing message to memory block...") 

With *packet
  \type = messageType
  \length = messageLength
EndWith

PokeS(@*packet\content, message) 


Debug("===> Extracting message from memory block...") 
msgType.b = *packet\type
msgLen.l = *packet\length 
msg.s = PeekS(@*packet\content) 

Debug("===> Printing extracted message data...") 
Debug("") 
Debug("--- Extracted Message Begins ---") 
Debug("Message type : " + Str(msgType)) 
Debug("Message length : " + Str(msgLen)) 
Debug("Message : " + msg) 
Debug("--- Extracted Message Ends ---") 
Debug("") 
BERESHEIT
Post Reply