A little TCP problem (reusing streams)...

Blitz3D Forums/Blitz3D Programming/A little TCP problem (reusing streams)...

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Ok, I've made a simple test thing working over UDP now (a plane with a cube on which is the player, and the second cube is created when the other player joins, and then the second player can control the cube and the other side can see it moves). The only problem is that it's slower than my grandma. Higher FPS I have, then slower it becomes. Here's my code (for both client and server):

;Types
Type clients
  Field stream
  Field model
  Field id%
  Field ip%
  Field joined
End Type

;Constants
Const port%=1234
Const clientport%=1235
Const ip$="x"

;Globals
Global intip% = HostIP(CountHostIPs("x"))

;Initialize
Graphics3D 640,480,32,2
SetBuffer BackBuffer()

;Host/join
.start
mode$=Input("Host or join?: ")

;Host/join/return
If Lower(mode$)="host" Then
  server=CreateUDPStream(port%)
  If server=0 Then
	RuntimeError "Failed to create server at port "+port%+"!"
  EndIf

  host=1
ElseIf Lower(mode$)="join" Then
  ;Create server
  client=CreateUDPStream(clientport%)
  UDPTimeouts 1000
  If client=0 Then
	RuntimeError "Client failed to connect!"
  EndIf

  ;Connect
  WriteString client,"connect"
  SendUDPMsg client,intip%,port%

  src=RecvUDPMsg(client)
  If src=0 Then
	RuntimeError "Lost connection!"
  EndIf

  comclient$=ReadString(client)
  If Not comclient$="connect_ok" Then
	RuntimeError "Failed to establish connection! "+comclient$
  EndIf


  ;ID
  WriteString client,"request_id"
  SendUDPMsg client,intip%,port%

  RecvUDPMsg(client)
  comclient$ = ReadInt(client)
  clientid% = Int(comclient$)

  WriteString client,"join"
  SendUDPMsg client,intip%,port%
  
  RecvUDPMsg(client)
  comclient$ = ReadString(client)
  If Not comclient$ = "joined" Then
	RuntimeError "Not joined "+comclient$
  EndIf


  host=0
Else
  Goto start
EndIf



cube=CreatePlane()
tex=LoadTexture("C:\wall.bmp")
EntityType cube,1
ScaleTexture tex,100,100
EntityTexture cube,tex
FreeTexture tex

cam=CreateCamera()
PositionEntity cam,100,100,100

lig=CreateLight()
PositionEntity lig,-100,100,100

player=CreateCube()
PositionEntity player,0,50,0
ScaleEntity player,2,6,1
EntityType player,2
EntityRadius player,6

Collisions 2,1,2,2


;FPS limit
timer=CreateTimer(10)

While Not KeyDown(1)
  WaitTimer(timer)

  ;Client
  If host=0 Then
	WriteString client,"position"
	SendUDPMsg client,intip%,port%
	
	WriteInt client,EntityX(player)
	WriteInt client,EntityY(player)
	WriteInt client,EntityZ(player)
	WriteInt client,EntityPitch(player)
	WriteInt client,EntityYaw(player)
	WriteInt client,EntityRoll(player)
	SendUDPMsg client,intip%,port%
  EndIf


  ;Server
  If host=1 Then
	For tmp=0 To 2
	;Check for new connection
	recv=RecvUDPMsg(server)
	If recv<>0 Then
	  com$ = ReadString(server)
	  cnt=0

	  ;Connect
	  If com$="connect" Then
		WriteString server,"connect_ok"
		SendUDPMsg server,recv,UDPMsgPort(server)
		cnt=1
	  EndIf
	
	
	  ;Request ID
	  If com$="request_id" Then
		WriteInt server,Rnd(1,50000)
		SendUDPMsg server,recv,UDPMsgPort(server)
		cnt=1
	  EndIf
	
	
	  ;Join
	  If com$="join" Then
		joined%=1
		model=CreateCube()
		ScaleMesh model,2,6,1
		
		WriteString server,"joined"
		SendUDPMsg server,recv,UDPMsgPort(server)
		cnt=1
	  EndIf
	
	
	  ;Position
	  If com$="position" Then
		RecvUDPMsg(server)
		
		x=ReadInt(server)
		y=ReadInt(server)
		z=ReadInt(server)
		pitch=ReadInt(server)
		yaw=ReadInt(server)
		roll=ReadInt(server)
		
		PositionEntity model,x,y,z
		RotateEntity model,pitch,yaw,roll
		cnt=1
	  EndIf
	
	
	  ;Error
	  If cnt=0 Then
		RuntimeError "Command not understood: "+com$
	  EndIf
	EndIf
	Next
  EndIf


  ;Input
  If KeyDown(200) Then
	MoveEntity player,0,0,4
  EndIf
	
  If KeyDown(208) Then
	MoveEntity player,0,0,-4
  EndIf
	
  If KeyDown(203) Then
	TurnEntity player,0,4,0
  EndIf
  
  If KeyDown(205) Then
	TurnEntity player,0,-4,0
  EndIf

  ;Precalculate
  TranslateEntity player,0,-4,0
  PointEntity cam,player

  ;Draw 3D
  UpdateWorld
  RenderWorld

  ;Draw 2D
  ; Client
  If host=0 Then
	Text 0,0,"ID: "+clientid%
  EndIf

  Flip
Wend


There's lot of rubbish code there, but I'm sure you'll understand. The reason for that the client and server ports are different is that I use the same computer for both.