# Visual Basic CopyMemory Problem



## CptNeutral (Mar 25, 2008)

Ok. I'm new to this forum , but here goes...

Right. I've made this working multiplayer game (winsock) - fly around in a little spaceship and shoot people.
Details about each player are stored in a type.

To send updates (position, speed, etc), it puts the player's data into a packet type;

```
Private Type ptPacket
    ID As Integer
    PacketID As Long
    Name As String * 20
    Facing As Single
    Heading As Single
    Speed As Single
    X As Single
    Y As Single
    State As Integer
    Colour As Long
    Kills As Integer
    Deaths As Integer
End Type
```
Now this packet is copied into a string (since winsock can only send strings), using CopyMemory;


```
'API declaration
Private Declare Function CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
    dest As Any, src As Any, ByVal length As Long) As Long

'Procedure
Dim mPacket As ptPacket
Dim sPacket As String

...
<Fill mPacket with player's details>
...

'Create the buffer
sPacket = Space$(Len(mPacket) + 1)

'Copy the packet type into the string.
CopyMemory ByVal sPacket, mPacket, Len(mPacket) + 1
```
When the server receives the packet, it copies the string back into the packet type, and sets the players new details;


```
'Copy the memory into the packet and use it to update the players info
CopyMemory mPacket, ByVal sPlayer, Len(sPlayer)
```
Now my problem is this; since the protocol used is UDP (and i'm not changing to TCP), there is no guarantee that the packets will arrive as they were sent, which means when the string is copied back into the packet type, the data will be incorrect.

This is causing my program to crash, and the only way I can think of checking for a corrupt packet is checking its length. But this isnt fixing the problem.

Any ideas?


----------

