Page 1 of 1
memory commands messing up random variables
Posted: Fri Sep 04, 2009 12:12 am
by ChebbyShabby
this is my networking code on the client side:
Code: Select all
InitNetwork()
ConnectionID = OpenNetworkConnection("127.0.0.1", 7843)
Structure WTS
x.f
v.f
EndStructure
enemy.WTS
*pointer = AllocateMemory(SizeOf(WTS))
Repeat
SendNetworkData(ConnectionID, @enemyWTS, SizeOf(WTS))
If NetworkClientEvent(ConnectionID) = 2
ReceiveNetworkData(ConnectionID, *pointer, SizeOf(WTS))
xx = PeekF(*pointer)
*pointer+4
v = PeekF(*pointer)
EndIf
Until Quit = 1
and this on the server side:
Code: Select all
Structure WTS
x.f
v.f
EndStructure
*pointer = AllocateMemory(SizeOf(WTS))
InitNetwork()
CreateNetworkServer(0, 7843)
Repeat
ClientID = EventClient()
If NetworkServerEvent() = 2
ReceiveNetworkData(ClientID, *pointer, SizeOf(WTS))
SendNetworkData(ClientID, *pointer, SizeOf(WTS))
EndIf
Until Quit = 1
CloseNetworkServer(0)
this all works nicely. however, i get random variables from my code (i couldn't include all of it since it's over 700 lines long) changing when i do this. when i send a number instead of a structure, however, this doesn't happen. what's going on?
Posted: Fri Sep 04, 2009 8:47 am
by Trond
You keep increasing *pointer by 4 bytes, but you never decrease it. So it gradually covers new grounds of memory.
Probably you have other similar errors in your code as well.
Posted: Fri Sep 04, 2009 12:17 pm
by cas
Why increase variable *pointer? After that you need to make sure you reset it. I would do it like this:
Code: Select all
InitNetwork()
ConnectionID = OpenNetworkConnection("127.0.0.1", 7843)
Structure WTS
x.f
v.f
EndStructure
enemy.WTS
*pointer = AllocateMemory(SizeOf(WTS))
Repeat
SendNetworkData(ConnectionID, @enemyWTS, SizeOf(WTS))
If NetworkClientEvent(ConnectionID) = 2
ReceiveNetworkData(ConnectionID, *pointer, SizeOf(WTS))
xx = PeekF(*pointer)
v = PeekF(*pointer+4)
EndIf
Until Quit = 1
Posted: Fri Sep 04, 2009 12:24 pm
by Thalius
correct and more easy to maintain would be:
Code: Select all
define v.f
...
v = PeekF(*pointer+OffsetOf(WTS\v))
That way you also just change the structure and the offsets in the code get updated by the compiler - instead fondling with numbers across the code.
Cheers,
Thalius
Posted: Fri Sep 04, 2009 12:29 pm
by cas
Thalius wrote:Code: Select all
define v.f
...
v = PeekF(*pointer+OffsetOf(WTS\v))
Definitely better solution if WTS structure has more elements with different types, but for only x.f and v.f it is not a problem to add only +4.
Posted: Fri Sep 04, 2009 1:04 pm
by ChebbyShabby
Thanks for the replies.it works now. One last thing, how do I convert this type of connection to a UDP one?
Posted: Fri Sep 04, 2009 2:32 pm
by cas
You only have to add
#PB_Network_UDP flag to CreateNetworkServer() and to OpenNetworkConnection()

Posted: Fri Sep 04, 2009 5:01 pm
by Thalius
Note that UDp is a Connectionless Protocol.
So you have to Closenetworkconnection() after receiving a complete packet as the connectionid each time will change.
Also note that UDP has no serialization or error correction. So you have to ensure yourself that the data you receive is correct and in order!
Cheers,
Thalius
Posted: Fri Sep 04, 2009 7:22 pm
by ChebbyShabby
sorry for being a newb, but it's not working

. i changed the type to UDP, put
Code: Select all
ConnectionID = OpenNetworkConnection("127.0.0.1", Port, #PB_Network_UDP)
in the loop and added
Code: Select all
CloseNetworkConnection(ConnectionID (or ClientID))
in the packet receiving parts of both codes.
this works nicely, but when i change the IP from 127.0.0.1 to my IP, i don't get anything.
Posted: Fri Sep 04, 2009 7:29 pm
by cas
Thalius wrote:So you have to Closenetworkconnection() after receiving a complete packet as the connectionid each time will change.
Are you sure about that? I can't find anything in PB manual about that.
EDIT:
After searching PB forums i saw this thread:
http://www.purebasic.fr/english/viewtopic.php?t=38304
Ignore my question, now i see that you are sure about what you are saying
