Network Code Problems

Blitz3D Forums/Blitz3D Beginners Area/Network Code Problems

Q(Posted 2006) [#1]
Whenever I connect to my server I get a 'MAV' error, I'm not sure why.. The problem seems to occur in the update_SERVER function.

This is a chopped of version of PCD Guy's TCP engine, so perhaps he can help? :o)

Here's my code:

; functions

Type user
	
	Field X
	Field Y
	Field stream
	Field name$

End Type

Global tcp,tcp_msg$,tcp_name$
Global total_players
Global ip$="127.0.0.1",port=8080

Function Init_SERVER()

	tcp=CreateTCPServer(8080)
	
	If TCP<>0
		Print "Server initialization successful."
		Print "Press any key to continue.."
		WaitKey()		
	Else
		Print "Server failed to launch."
		Print "Press any key to continue.."
		WaitKey()
		End
	EndIf

End Function

Function Update_SERVER()

	Text 0,0,+total_players

	tcp_stream=AcceptTCPStream(tcp)
	If tcp_stream<>0
		tcp_msg$=ReadLine(tcp_stream)
		If tcp_msg$="login request"
			tcp_msg$=ReadLine(tcp_stream)
			user_taken=False
			For p.user = Each user
				If p\name$=tcp_msg$
					user_taken=True
					Exit
				EndIf
			Next
			If user_taken=True
				WriteLine(tcp,"Username Taken")
			Else		
				p.user = New user
				p\name$=tcp_msg$
				total_players=total_players+1
				Print "User "+tcp_msg$+" has joined the server"	
			EndIf
		EndIf
	EndIf
		
End Function

Function Close_SERVER()

	CloseTCPStream(tcp)
	
End Function

Function Init_CLIENT()

	tcp=OpenTCPStream(ip$,port)
	If tcp=0
		Print "Failed to connect to the server.  Attempting to reconnect.."
		For I=1 To 3
			Print "Attempt "+I+" of 3"
			TCP=OpenTCPStream(ip$,port)
			If TCP<>0
				Exit
			EndIf
		Next
		RuntimeError "Could not connect to server"
	EndIf
	tcp_name$=Input("Enter your desired user-name: ")
	
	WriteLine(tcp,"login request")
	WriteLine(tcp,tcp_name$)
	
End Function

Function Update_CLIENT()

	tcp=OpenTCPStream(ip$,port)
	If tcp=0
		Print "Failed to connect to the server.  Attempting to reconnect.."
		For I=1 To 3
			Print "Attempt "+I+" of 3"
			TCP=OpenTCPStream(ip$,port)
			If TCP<>0
				Exit
			EndIf
		Next
		RuntimeError "Could not connect to server"
	EndIf	
	
End Function

Function Close_CLIENT()
	
	CloseTCPStream(TCP)
	
End Function

;server

Graphics 640,480,16,2
SetBuffer BackBuffer()

Include "ST_TCPIP_CONTROL.BB"

Init_SERVER()

While Not KeyHit(1)

	Cls
		Update_SERVER()	
	Flip
	
Wend
Close_SERVER()
End

;client

Graphics 640,480,16,2
SetBuffer BackBuffer()

Include "ST_TCPIP_CONTROL.BB"

Init_CLIENT()

While Not KeyHit(1)

	Cls
		Update_CLIENT()	
	Flip
	
Wend
Close_CLIENT()
End



Damien Sturdy(Posted 2006) [#2]
Turn on Debug for one :)


Q(Posted 2006) [#3]
Ok, that fixed some of it.. I've updated my post. I'm still having some troubles. When the client connects, it disconnects(I think) after sending the user's user-name to server(line-84). Also, when I exit the server it says "TCP stream does not exist"..? Isn't tcp the stream that needs to be closed?