Network commands request

Blitz3D Forums/Blitz3D Beginners Area/Network commands request

Hi I know it's a bit cheeky, but I have been toying with this for over a year and I have real difficulty with the whole Networking code stuff.

All I want is a dead simple annotated example of the following:

Checks for net connection and gets other computer details

sends/recieves a few basic details (i.e. player stats)

synchronises start of game

small game loop with regular swapping of details


I havn't tested it, and it is only synchronisation and a little gameloop. For sending data, try something like integer 0, followed by integer frametime, followed by integer player id number (if you plan on having more than 2 players), followed by all data regarding this player, either terminate by integer 0, or write a new player number followed by the same kind of data etc. After terminating the player data section, go on with other stuff in the same manner (id, followed by set amount of data, terminate with 0).

I hope that I havn't confused more than I have helped.
Const host=0
Const client=1
Const logicrate=100
Global nulltime
ip$="127.0.0.1"
port=7777
localmachine=host ;or client
If localmachine=host Then
	server=CreateTCPServer(port)
	Repeat
		stream=AcceptTCPStream(server)
		If stream<>0 Then Exit
		If KeyHit(1) Then RuntimeError "Waiting aborted"
	Forever
	Delay 1000
	sendtime=MilliSecs()
	WriteInt stream,0
	Repeat
		If ReadAvail(stream)>=4 Then
			nulltime=(MilliSecs()+sendtime)/2
			ReadInt(stream)
			Exit
		End If
	Forever
Else
	Repeat
		stream=OpenTCPStream(ip,port)
		If stream<>0 Then Exit
		If KeyHit(1) Then RuntimeError "Waiting aborted"
	Forever
	Repeat
		If ReadAvail(stream)>=4 Then
			nulltime=MilliSecs()
			WriteInt stream,0
			ReadInt(stream)
			Exit
		End If
	Forever
End If
;'the mark'
;load gfx etc if not already done
If localmachine=host Then
	Repeat
		If ReadAvail(stream)>=4 Then
			ReadInt(stream)
			frametime=gametime()+1000
			WriteInt stream,frametime
			Exit
		End If
	Forever
Else
	WriteInt stream,0
	Repeat
		If ReadAvail(stream)>=4 Then
			frametime=ReadInt(stream)
			Exit
		End If
	Forever
End If
frametime=frametime*logicrate
Repeat
	While frametime>gametime*logicrate
		RenderWorld()
		;all rendering stuff in here
		Flip
	Wend
	;game logics, including updateworld go here
	frametime=frametime+1000
Forever
Function gametime()
	Return MilliSecs()-nulltime
End Function

;This should make a good setup for a 2 player realtime game, gametime() should return
;the same time on both computers. If you want multiple clients, you will need the code
;before 'the mark' to be run once for every client, afterwards the host must tell the
;clients how much they need to correct their nulltime. (Just use the difference in the
;nulltime's generated at the host end)

;You could eventually also set up a UDP stream for use during game.


Thanks, that looks great, eBusiness.. I'm still at work so can't test it yet, but it appears to be just what I wanted. From this framework, I can build on it!

Your notes seem real helpful too!
I'll be back once I've been able top have a play with it!