TCP Networking Help

Blitz3D Forums/Blitz3D Beginners Area/TCP Networking Help

I'm kinda new to the whole networking thing and I am trying to make a internet game.

my code:

it connects via this function:
host=Input ("1-Host,2-join")

If host=1

Print "Creating Server on port 8080"

svrgame=CreateTCPServer(8080)
Print "Server Started Succesfully"

Delay 500

Else

Print "Joining..."

svrstrm=OpenTCPStream("127.0.0.1",8080)

Print "Stream Connected"

EndIf 


here's where it transfers data.
If host=2

;Read Streams
strm=AcceptTCPStream(svrstrm)   ;ERROR RIGHT HERE

If strm Then 
plx#=ReadFloat#(strm)
ply#=ReadFloat#(strm)
plz#=ReadFloat#(strm)
plyaw=ReadLine(strm)
plpitch=ReadLine(strm)
plroll=ReadLine(strm)
CloseTCPStream strm
EndIf 

;CloseTCPStream(svrstrm)

wrtstrm=OpenTCPStream("127.0.0.1",8080)

;Write Stream
For p.pl=Each pl 
WriteFloat(wrtstrm,EntityX(p\cam))
WriteFloat(wrtstrm,EntityY(p\cam))
WriteFloat(wrtstrm,EntityZ(p\cam))
WriteInt(wrtstrm,EntityYaw(p\cam))
WriteInt(wrtstrm,EntityPitch(p\cam))
WriteInt(wrtstrm,EntityRoll(p\cam))
DebugLog "Sent"+wrtstrm
Next 

CloseTCPStream(wrtstrm)

Else ;if hosting

;Read Streams
strm=AcceptTCPStream(svrgame)

If strm Then 
plx#=ReadFloat#(strm)
ply#=ReadFloat#(strm)
plz#=ReadFloat#(strm)
plyaw=ReadLine(strm)
plpitch=ReadLine(strm)
plroll=ReadLine(strm)
CloseTCPStream strm
EndIf 


wrtstrm=OpenTCPStream("127.0.0.1",8080)

;Write Stream
For p.pl=Each pl 
WriteFloat(wrtstrm,EntityX(p\cam))
WriteFloat(wrtstrm,EntityY(p\cam))
WriteFloat(wrtstrm,EntityZ(p\cam))
WriteInt(wrtstrm,EntityYaw(p\cam))
WriteInt(wrtstrm,EntityPitch(p\cam))
WriteInt(wrtstrm,EntityRoll(p\cam))
DebugLog "Sent"+wrtstrm
Next 

CloseTCPStream(wrtstrm)

EndIf 



Both of those code are in functions and just about every variable is Global.
I run a host program for my first instance and it works fine, then I run a second instance and it connects but gives me the error "Stream Does not Exist" at
strm=AcceptTCPStream(svrstrm) 


(I marked it with an all caps error comment)

Why is it giving me this error? Thanks

I didn't read the source, but when you say the first instance works and the second doesn0t then this sounds to me like:

you are using the same port for both instances, they both are trying to establish a server on the same port. As long as the instances run on the same machine, you should use individual ports.

One I set up as the server and the other the client.
the variable host is checking whether or not you're the host server computer. Though I am running this locally.

ok. try

print svrstrm
waitkey()

before you use AcceptTCPStream. Is it zero? Then the connection failed anyway.

You may also use other IPs such as: "192.168.0.2" (or whatever your lan adress is), or "localhost".

Basicly I suggest to try the TCP example that is provided with the docs ("msg from apollo" thing), and other tested tcp sourcecodes from the archives. This way you'll make sure if your machine can deal with it.

Okay, I'll try that, thanks

The demo works fine and the stream apparently exists because it says 40172 on the screen when I printed it.

Why isn't it working? Is there an example somewhere that shows how to stream data between two computers over the internet?

Without looking at your code, I think that I ran into a similar problem awhile ago. You must create a new stream every time before you send the information. In the examples, they only create one stream because they only send one packet.

Is Creating a tcp stream the same as opening a stream?

What I did was ask whether or not you're the host, then if you are it creates a tcp server. I you're the client, it opens a tcp stream called svrstrm on the local machine. Then in the update part, it tests whether youre the host or not (host=?) If you are the host (host<>2) then it accept streams from the server and reads them and it opens one to be written to. If youre the client (host=2) then accepts a stream from svrstrm (which is where it gives me the error) and read it, then opens and stream and writes to it.

ok, i kinda got it woking, but now the client and server are writing independently from each other, reading their own coordinates. How do I prevent this?
code:
Function GUI()

host=Input ("1-Host,2-join")

If host=1

Print "Creating Server on port 8080"

svrgame=CreateTCPServer(8080)
Print "Server Started Succesfully"

Delay 500

Else

Print "Joining..."

svrclnt=CreateTCPServer(8000)

Print svrclnt

Print "Stream Connected"

Delay 100

EndIf 

End Function 


Function UpdateNetWork()

If host=2 ;CLIENT


Text 0,200,svrclnt

;Read Streams
strm=AcceptTCPStream(svrclnt)

If strm Then 
plx#=ReadFloat#(strm)
ply#=ReadFloat#(strm)
plz#=ReadFloat#(strm)
plyaw=ReadLine(strm)
plpitch=ReadLine(strm)
plroll=ReadLine(strm)
CloseTCPStream strm
EndIf 

Cls 
Text 300,100,plx#
Text 300,150,ply#
Text 300,200,plz#
Flip 

wrtstrm=OpenTCPStream("127.0.0.1",8000)

;Write Stream
For p.pl=Each pl 
WriteFloat(wrtstrm,EntityX(p\cam))
WriteFloat(wrtstrm,EntityY(p\cam))
WriteFloat(wrtstrm,EntityZ(p\cam))
WriteInt(wrtstrm,EntityYaw(p\cam))
WriteInt(wrtstrm,EntityPitch(p\cam))
WriteInt(wrtstrm,EntityRoll(p\cam))
DebugLog "Sent"+wrtstrm
Next 

CloseTCPStream(wrtstrm)

Else ;if HOSTING

;Read Streams
strm=AcceptTCPStream(svrgame)

If strm Then 
plx#=ReadFloat#(strm)
ply#=ReadFloat#(strm)
plz#=ReadFloat#(strm)
plyaw=ReadLine(strm)
plpitch=ReadLine(strm)
plroll=ReadLine(strm)
CloseTCPStream strm
EndIf 


wrtstrm=OpenTCPStream("127.0.0.1",8080)

;Write Stream
For p.pl=Each pl 
WriteFloat(wrtstrm,EntityX(p\cam))
WriteFloat(wrtstrm,EntityY(p\cam))
WriteFloat(wrtstrm,EntityZ(p\cam))
WriteInt(wrtstrm,EntityYaw(p\cam))
WriteInt(wrtstrm,EntityPitch(p\cam))
WriteInt(wrtstrm,EntityRoll(p\cam))
DebugLog "Sent"+wrtstrm
Next 

CloseTCPStream(wrtstrm)

EndIf 

End Function 


What exactly is a port? Do two computer's server have to be on the same port to interact?

No. A port is a virtual connection identifier. So you can have up to 65 thousand simultanous virtual connections in one wire.

For example, on a machine there may be multiple server apps running at the same time, one is serving on port 80, an other one at 1080... Now an other machine, the client can connect to a certain service port. On the clients machine at the other hand multiple client apps may be running, using individual (client) ports as well. And maybe there's even clients and servers on the same machine. In this case the ports of a server and the connected client app may not be the same.

There are some common ports, most web servers are serving on port 80, for example.

You should also know that web pages are based on a REQUEST system. The client (browser) is sending a special request message to the server (eg. to www.blitzbasic.com:80 ). The webserver will then SERVE by sending an answer, usually containing the requested web pages html code.

So basicly it's always:
1 client asks
2 server answers
3 client asks
4 server answers
...

For more info use google.

ok, thank you! That helps a lot. Now i've (re) discovered BlitzPlay and I know its old and unsupported but so far so is Blitz3d.
BlitzPlay has met my needs and is working pretty well.
code:
Function GUI()

host=Input ("Host (1) or Join(0)")

FlushKeys

myname$=Input$ ("UserName= ")
FlushKeys

If host=1 Then     ;HOST

My_Port=2222

If Not BP_HostSession(myname$,10,1,2222,10) ;maxplayer 10  !!!
RuntimeError "Could not Host game"
EndIf
SetupPlayer(myname$,My_Port)

Else If host=0    ;CLIENT

Print "Host Port=2222"
ip$=Input$ ("Host IP ?")
My_Port=Input$ ("Which port (3000,4000) ?")

Cls
Print "Connecting..."
Select BP_JoinSession(myname$,My_Port,ip$,2222)
	Case BP_NOREPLY
		RuntimeError "No Reply from host"
	Case BP_IAMBANNED
		RuntimeError "You have been banned from joining"
	Case BP_GAMEISFULL
		RuntimeError "Game is full"
	Case BP_PORTNOTAVAILABLE
		RuntimeError "Port: "+My_Port+" is not available"
	Case BP_USERABORT
		RuntimeError "Connection Attempt aborted"
End Select
;joining..
Print "Established Connection...."
SetupPlayer(myname$,My_Port)

EndIf

End Function 


and the update network section:

frames=WaitTimer(tmr)
For k=1 To frames
	BP_UpdateNetwork()
	HandleMessages()

p.pl=First pl
x=EntityX(p\cam)
y=EntityY(p\cam)
z=EntityZ(p\cam)

	UpdateGame()         ;update everthing else

	p.pl=First pl 
		msgtosend$="X"+EntityX(p\cam)
		BP_UDPMessage(0,1,msgtosend$)
		msgtosend$="Y"+EntityY(p\cam)
		BP_UDPMessage(0,1,msgtosend$)
		msgtosend$="Z"+EntityZ(p\cam)
		BP_UDPMessage(0,1,msgtosend$)
Next 


and the code in whole:

;weapon particles
Include "Particles/FireBullet.bb"
Include "Particles/Explosion_Rock.bb"
Include "Particles/World_Fire.bb"
Include "Particles/Explo_Wood.bb"

;Main includes
Include "Includes/LoadWorld.bb"
Include "includes/toolui_include.bb"
Include "includes/PyroParticleSystem.bb"
Include "Includes/CubeMap.bb"
Include "Includes\BPliteV1.14.bb"
TInit

my_game$="StealthRoom"

Global tmr=CreateTimer(40)  ;fps timer

Global svrgame,wrtstrm,host,svrstrm,svrclnt,newx,newy,newz   ;internet 

EndGraphics

Type Info
	Field txt$
End Type

;Stealthroom Framework
Graphics3D 800,600,32,2
SetBuffer BackBuffer()

;rhwn=WB3D_InitializeGUI(SystemProperty("RuntimeWindow"))
;B3D_HideGadget(rhwn)

TDesign 3

Global level,tmp_fireB,tmp_ex_norm,b_fire_snd,tmp_fire_torch,jump,tmp_fireC,sky

Const gravity#=5

;collisions
Const COL_FIREB=5

Collisions COL_FIREB,COLLISIONTYPE_BRUSH,2,1

SetupLoadWorld()

Type pl
Field cam,num,com,sel_wep$,list,lockmouse,h_prog,mark
Field ID,name$,port
End Type

Type wep_mark
Field spr,kind$
End Type 

Type wep
Field ent
End Type 

Type expl
Field emt,kind$,par
End Type 

Type destr
Field hits,ent
End Type 

;Weapon Types----

Type fire_b
Field emt,par
End Type 

Type fire_c
Field emt,par
End Type

GUI()
FlushKeys 

LoadSounds()
LoadLevel("maps\test3.b3d")
sky=LoadSky("night")

p.pl=First pl
x=EntityX(p\cam)
y=EntityY(p\cam)
z=EntityZ(p\cam)

Stop 

p.pl=First pl
PositionEntity p\cam,0,4000,0

p.pl=First pl
x=EntityX(p\cam)
y=EntityY(p\cam)
z=EntityZ(p\cam)

Stop

While Not KeyDown(1)

UpdateNetwork()

p.pl=First pl
x=EntityX(p\cam)
y=EntityY(p\cam)
z=EntityZ(p\cam)

Stop 

Cls
TUI
UpdateCubeMaps()
p.pl=First pl:PyroUpdate(p\cam) 
UpdateWorld
RenderWorld
Color 255,255,255 
Text 0,0,"Jumping: "+jump
Text 200,200,newx
Text 200,250,newy
Text 200,300,newz
Flip

Wend
PyroClear()
DeleteAllCubeMapObjects()
BP_EndSession()
End

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;FUNCTIONS;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Function LoadLevel(lev$)

AmbientLight 255,255,255

level=LoadWorld(lev$)
EntityPickMode level,2 

End Function

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Function UpdatePlayer(num)

For p.pl=Each pl

If p\num=num Then 

If p\ID=BP_My_ID Then 

;Camera controls
	TranslateEntity p\cam,0,-gravity#,0
	
	If p\lockmouse=1 Then 
		mx#=CurveValue(MouseXSpeed()*mousespeed,mx,camerasmoothness)
		my#=CurveValue(MouseYSpeed()*mousespeed,my,camerasmoothness)
		MoveMouse GraphicsWidth()/2,GraphicsHeight()/2
		pitch#=EntityPitch(p\cam)
		yaw#=EntityYaw(p\cam)
		pitch=pitch+my
		yaw=yaw-mx
		If pitch>89 pitch=89
		If pitch<-89 pitch=-89
		RotateEntity p\cam,0,yaw,0
		TurnEntity p\cam,pitch,0,0
	EndIf 
	
	cx#=(KeyDown(32)-KeyDown(30))*cameraspeed
	cz#=(KeyDown(17)-KeyDown(31))*cameraspeed
	MoveEntity p\cam,cx,0,cz
	
	If KeyHit(57) Then 
		If p\lockmouse=1 Then p\lockmouse=0:Return
		If p\lockmouse=0 Then p\lockmouse=1:Return
	EndIf
	
	If jump=1 Then 
		For jumpcol=1 To CountCollisions(p\cam)
			jumpcol_ent=CollisionEntity(p\cam,jumpcol)
			If GetEntityType(jumpcol_ent)=COLLISIONTYPE_BRUSH Then 
				jump=0
			EndIf 
		Next 
		TranslateEntity p\cam,0,cameraspeed*3,0
	EndIf
	
	If KeyHit(18) Then 
		If jump=0 Then jump=1
	EndIf 
	
				 
	UpdatePlayerWeapon(p\num)
	UpdateCastedWeapons()
	UpdateStaticWeapons()


EndIf

EndIf 

Next

End Function 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Function UpdatePlayerWeapon(num)

For p.pl=Each pl

If p\ID=BP_My_ID Then 

If p\num=num Then 

Select TItemGet$(TGadgetGet$(p\com,t_selected))

Case "Fire Bullet"

	If MouseHit(1) Then 
		PlaySound b_fire_snd
		f.fire_b=New fire_b
;			f\par=
			f\emt=PyroEmitterNew(CreateFireBullet())
			RotateEntity f\emt,EntityPitch(p\cam),EntityYaw(p\cam),EntityZ(p\cam)
			PositionEntity f\emt,EntityX(p\cam),EntityY(p\cam),EntityZ(p\cam)
			EntityType f\emt,COL_FIREB
	EndIf 	

Case "Fire Column"

	If MouseHit(1) Then 
		PlaySound b_fire_snd
		fc.fire_c=New fire_c
			fc\emt=CreateCube()
			RotateEntity fc\emt,EntityPitch(p\cam),EntityYaw(p\cam),EntityZ(p\cam)
			PositionEntity fc\emt,EntityX(p\cam),EntityY(p\cam),EntityZ(p\cam)
			;SetEmitter(fc\emt,tmp_fireC) ----------------------------------------------------
			;EntityType fc\emt,COL_FIREB
	EndIf 	


End Select 

EndIf  

EndIf 

Next 

UpdateHeldWeapon()

End Function 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
Function UpdateLevel()

End Function

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Function SetupPlayer(name$,port)

p.pl=New pl
p\cam=CreateCamera()
CameraRange p\cam,1,40000
p\num=1
p\lockmouse=1
EntityType p\cam,COLLISIONTYPE_PLAYER
EntityRadius p\cam,32

;create combo weapon box
p\com=TComboBox(0,10,10,130,25)
	p\list=TListNew()
		TItemNew(p\list,"--Magic--")
			TItemNew(p\list,"Fire Bullet") 
			TItemNew(p\list,"Fire Column") 
			TItemNew(p\list,"new item") 
			TItemNew(p\list,"new item") 
		TItemNew(p\list,"--Weapons--")
	TGadgetSet(p\com,t_list,p\list)		
	TGadgetSet(p\com,t_value,1)

p\h_prog=TProgress(0,10,GraphicsHeight()-50,150,30)
	TGadgetSet p\h_prog,t_value,.5

p\name$=name$
p\port=port
p\ID=BP_My_ID


End Function 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Function UpdateCastedWeapons()

For f.fire_b=Each fire_b

	MoveEntity f\emt,0,0,5 
	
	For colfireb=1 To CountCollisions(f\emt)
		colfireb_ent=CollisionEntity(f\emt,colfireb) 
			For d.destr=Each destr
				If colfireb_ent=d\ent Then
					FreeEntity d\ent
					CreateExplosion("wood",f\emt)
					PyroEmitterDelete(f\emt,True):Delete f:Return
				EndIf
			Next 
		If GetEntityType(colfireb_ent)=COLLISIONTYPE_BRUSH
				CreateExplosion("rock",f\emt)
				PyroEmitterDelete(f\emt,True):Delete f:Return
		EndIf 
	Next 
	
Next

;;;;;;;;;;

For fc.fire_c=Each fire_c

	TurnEntity fc\emt,0,5,0
	
Next 

End Function  

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Function CreateExplosion(kind$,ent)

e.expl=New expl
e\kind$=kind$
Select e\kind$
	Case "rock"
		e\emt=PyroEmitterNew(CreateExplo_Rock()) 
	Case "wood"
		e\emt=PyroEmitterNew(CreateExplo_wood())
End Select 

PositionEntity e\emt,EntityX(ent),EntityY(ent),EntityZ(ent)

End Function 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Function UpdateExplosions()  ;FIRST COMMAND CALLED

For e.expl=Each expl

PyroEmitterDelete(e\emt,False)
Delete e
Return 

Next  

End Function

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Function LoadSounds()

;weapon sounds
b_fire_snd=LoadSound("Media/scifi002.wav")

End Function 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Function UpdateStaticWeapons()

For w.wep_mark=Each wep_mark

	TurnEntity w\spr,0,20,0
	
	For p.pl=Each pl
	
		If CameraPick(p\cam,MouseX(),MouseY())=w\spr
			If MouseHit(1) Then PickupWeapon(w\kind$):DeleteCubeMapObject(w\spr):FreeEntity w\spr:Delete w
		EndIf 
	
	Next 

Next 

End Function 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Function PickupWeapon(kind$)

Select kind$

Case "stick"
	For p.pl=Each pl
		we.wep=New wep
		we\ent=LoadAnimMesh("media/stick.b3d") ;anim 0 - all
		ExtractAnimSeq we\ent,1,9 ;anim 1 - attack
		ExtractAnimSeq we\ent,10,16 ;anim 2 - block
		RotateEntity we\ent,EntityPitch(p\cam),EntityYaw(p\cam),EntityRoll(p\cam) 
		PositionEntity we\ent,EntityX(p\cam)+2,EntityY(p\cam),EntityZ(p\cam)+20
		EntityParent we\ent,p\cam
		ScaleEntity we\ent,1,1,1
		tempb=LoadMaterialBrush("media/brush_wood.b3d")
		PaintEntity we\ent,tempb
		FreeBrush tempb
	Next 
	
End Select		

	

End Function

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Function UpdateHeldWeapon()

For we.wep=Each wep

	If MouseHit(2)
		Animate we\ent,3,1,1
	EndIf 
	If MouseHit(3) 
		Animate we\ent,3,1,2
	EndIf 
Next 

End Function  

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Function LoadMaterialBrush(file$,flags=0,u_scale=1,v_scale=1)
p=Instr(file,".")
ext$=Lower(Right(file,Len(file)-p))
Select ext
	Case "jpg","jpeg","bmp","png","tga"
		t=LoadTexture(file,flags)
		If t
			ScaleTexture t,u_scale,v_scale
			b=CreateBrush()
			BrushTexture b,t
			FreeTexture t
			Return b
			EndIf		
	Case "b3d"
		m=LoadMesh(file)
		If m
			If CountSurfaces(m)
				surf=GetSurface(m,1)
				b=GetSurfaceBrush(surf)
				FreeEntity m
				Return b
			EndIf
		Else 
		RuntimeError ".b3d Brush does not Exist!"
		EndIf 		
	End Select
End Function

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Function LoadSky(kind$)

Select kind$

Case "day"

	skymesh=LoadMesh("skies/skydome.b3d")
	ScaleEntity skymesh,5000,5000,5000   
	skytex=LoadTexture("skies/domes/SB_dome_1.png")
	EntityTexture skymesh,skytex 

Case "night"

	skymesh=LoadMesh("skies/skydome.b3d")
	ScaleEntity skymesh,5000,5000,5000   
	skytex=LoadTexture("skies/domes/SB_dome_2.png")
	EntityTexture skymesh,skytex 

Case "galaxy01"

	skymesh=LoadMesh("skies/galaxy.b3d")
	ScaleEntity skymesh,5000,5000,5000   
	skytex=LoadTexture("skies/galaxys/galaxy_1.png")
	EntityTexture skymesh,skytex 

End Select 

Return skymesh

End Function

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Function GUI()

host=Input ("Host (1) or Join(0)")

FlushKeys

myname$=Input$ ("UserName= ")
FlushKeys

If host=1 Then     ;HOST

My_Port=2222

If Not BP_HostSession(myname$,10,1,2222,10) ;maxplayer 10  !!!
RuntimeError "Could not Host game"
EndIf
SetupPlayer(myname$,My_Port)

Else If host=0    ;CLIENT

Print "Host Port=2222"
ip$=Input$ ("Host IP ?")
My_Port=Input$ ("Which port (3000,4000) ?")

Cls
Print "Connecting..."
Select BP_JoinSession(myname$,My_Port,ip$,2222)
	Case BP_NOREPLY
		RuntimeError "No Reply from host"
	Case BP_IAMBANNED
		RuntimeError "You have been banned from joining"
	Case BP_GAMEISFULL
		RuntimeError "Game is full"
	Case BP_PORTNOTAVAILABLE
		RuntimeError "Port: "+My_Port+" is not available"
	Case BP_USERABORT
		RuntimeError "Connection Attempt aborted"
End Select
;joining..
Print "Established Connection...."
SetupPlayer(myname$,My_Port)

EndIf

End Function 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Function Chat()
End Function

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Function UpdateNetWork()

frames=WaitTimer(tmr)
For k=1 To frames
	BP_UpdateNetwork()
	HandleMessages()

p.pl=First pl
x=EntityX(p\cam)
y=EntityY(p\cam)
z=EntityZ(p\cam)

	UpdateGame()         ;update everthing else

	p.pl=First pl 
		msgtosend$="X"+EntityX(p\cam)
		BP_UDPMessage(0,1,msgtosend$)
		msgtosend$="Y"+EntityY(p\cam)
		BP_UDPMessage(0,1,msgtosend$)
		msgtosend$="Z"+EntityZ(p\cam)
		BP_UDPMessage(0,1,msgtosend$)
Next 

End Function

Function UpdateGame()

UpdateExplosions()
For p.pl=Each pl:UpdatePlayer(p\num):Next 
UpdateLevel()

End Function 

Function HandleMessages()

For msg.MsgInfo=Each MsgInfo
Select msg\msgtype
	Case 255  ;new player
		SetupExternalPlayer(msg\msgData,msg\msgFrom)
	Case 254 ;player left
		p.pl=Find_pdata(msg\msgFrom)
		If p=Null Then Delete p
	Case 253  ;host disconnects
		For p.pl=Each pl
			If p\ID<>BP_My_ID Then Delete p
		Next
		RuntimeError "Host Ended the game"
	Case 252   ;someone got kicked
		p.pl=Find_pdata(msg\msgfrom)
		If p\ID=BP_My_ID Then
			If msg\msgData=False Then RuntimeError "You have been kicked" Else RuntimeError "You have been banned"
			For p.pl=Each pl
				If p\ID<>BP_My_ID Then
					Delete p
				EndIf 
			Next
		Else ;if it wasn't you
			If msg\msgData=False Then Print "**"+p\name+" has been kicked!" Else Print("**"+p\name+" has been banned")
			Delete p
		EndIf
	Case 1   ;position packet
		coor$=msg\msgData
		If Left(coor$,1)="X" Then newx=Replace(coor$,"X","")
		If Left(coor$,1)="Y" Then newy=Replace(coor$,"Y","")
		If Left(coor$,1)="Z" Then newz=Replace(coor$,"Z","")
		p.pl=Find_pdata(msg\msgFrom)
		If Not p\ID=BP_My_ID Then PositionEntity p\cam,newx,newy,newz
End Select
Delete msg
Next

End Function

Function SetupExternalPlayer(name$,ID)

p.pl=New pl
p\cam=CreateCube()   ;The "Cam" is actually the marker for the other player
ScaleEntity p\cam,15,30,15
p\num=1
p\lockmouse=1
EntityType p\cam,COLLISIONTYPE_PLAYER
EntityRadius p\cam,32

p\name$=name$
p\ID=ID

End Function

Function Find_pdata.pl (ID)
	For p.pl = Each pl
		If p\ID = ID Then Return p
	Next
End Function




It works fine for a while but after the client connects and go through about 5+ packet transfers it sets the location of the client player to 0,0,0. I think it may have lost connection with the client and assumed zero coordinates. This also happens automatically connected via multiple instances and 127.0.0.1.

Thanks for all your help!