memory commands messing up random variables

Just starting out? Need help? Post your questions and find answers here.
ChebbyShabby
Enthusiast
Enthusiast
Posts: 121
Joined: Mon Jun 26, 2006 10:47 am

memory commands messing up random variables

Post 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?
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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.
cas
Enthusiast
Enthusiast
Posts: 597
Joined: Mon Nov 03, 2008 9:56 pm

Post 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 
Thalius
Enthusiast
Enthusiast
Posts: 711
Joined: Thu Jul 17, 2003 4:15 pm
Contact:

Post 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
"In 3D there is never enough Time to do Things right,
but there's always enough Time to make them *look* right."
"psssst! i steal signatures... don't tell anyone! ;)"
cas
Enthusiast
Enthusiast
Posts: 597
Joined: Mon Nov 03, 2008 9:56 pm

Post 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.
ChebbyShabby
Enthusiast
Enthusiast
Posts: 121
Joined: Mon Jun 26, 2006 10:47 am

Post by ChebbyShabby »

Thanks for the replies.it works now. One last thing, how do I convert this type of connection to a UDP one?
cas
Enthusiast
Enthusiast
Posts: 597
Joined: Mon Nov 03, 2008 9:56 pm

Post by cas »

You only have to add #PB_Network_UDP flag to CreateNetworkServer() and to OpenNetworkConnection() :wink:
Thalius
Enthusiast
Enthusiast
Posts: 711
Joined: Thu Jul 17, 2003 4:15 pm
Contact:

Post 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
"In 3D there is never enough Time to do Things right,
but there's always enough Time to make them *look* right."
"psssst! i steal signatures... don't tell anyone! ;)"
ChebbyShabby
Enthusiast
Enthusiast
Posts: 121
Joined: Mon Jun 26, 2006 10:47 am

Post by ChebbyShabby »

sorry for being a newb, but it's not working :oops: . 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.
Last edited by ChebbyShabby on Fri Sep 04, 2009 8:07 pm, edited 1 time in total.
cas
Enthusiast
Enthusiast
Posts: 597
Joined: Mon Nov 03, 2008 9:56 pm

Post 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 8)
Post Reply