how to send a packet?

Blitz3D Forums/Blitz3D Programming/how to send a packet?

ZaroSath(Posted 2013) [#1]
networking doesnt appear to be talked about much around here considering all i have to dig through using the search function only to find nothing specific.

i'm currently using eNet as a networking library and i've got it all setup, however i'm having trouble organizing data so that the server knows what to do with it, currently my project i'm working with uses 'packetType' (an int) which gets sent with the data so the server knows what to do with it, what i need to know is how can i send packetType with the data and be able to sort it out on the other end?

any help would be greatly appreciated.


RifRaf(Posted 2013) [#2]
No idea what Enet is, or how its put together.

Packets are strings with Ints,floats and whatever else you want in them
usually each is compressed into a string and added to the packet with known char lengths per value

Using functions such as the example below to compress a Integer value into a 4 char string, and take a 4 char string and decompress into an integer.

Because you know the order that you add values into the packet and the exact char length of each one you can then unpack them to get the data you packed into them from the other connected client(s) or host.

example 1
packet$=packet$+IntToStr(playerhealth)
packet$=packet$+IntToStr(playermana)
sent packet
receiving
Health=StrToInt(mid$(packet$,1,4))
Mana=StrToInt(mid$(packet$,5,4))



But if Enet does not handle packets for you then you may need to put a prefIx on the packet so you can indentify packet types such as this


example 2
Const Packet_Health=1
Const Packet_Position=2
-Packet
packet$=inttostr(packet_health)
packet$=packet$+IntToStr(playerhealth)
packet$=packet$+IntToStr(playermana)
sent packet


-receiving
Prefix=strtoint(mid$(Packet$,1,4))
select prefix
case packet_health
Health=StrToInt(mid$(packet$,5,4))
Mana=StrToInt(mid$(packet$,9,4))
end select




Function IntToStr$(Num%, StrLen% = 4)
	Local shiftin%
	Local st$ = Chr$(num And 255)
	For shiftin = 1 To (strlen - 1)
		st$ = st$ + Chr$(num Sar (8 * shiftin))
	Next
	Return st
End Function 

Function StrToInt%(st$)
	Local shiftin%
	Local num%
	For shiftin = 0 To (Len (st$) - 1)
		num = num Or (Asc (Mid$ (st$, shiftin + 1, 1)) Shl shiftin * 8)
	Next
	Return num
End Function




ZaroSath(Posted 2013) [#3]
Edit: to be edited, had a problem but may have fixed it.. testing now.


ZaroSath(Posted 2013) [#4]
Edit: thanks for that, i ran into some problems and spent HOURS just to get it working right.. and it turns out to be a fair bit noticeably slower than the previous networking system so i really dont know if i'm going to use it now.. i still need to figure out whats causing the delay.