UDP connect to server?

Blitz3D Forums/Blitz3D Beginners Area/UDP connect to server?

Fuller(Posted 2007) [#1]
I wrote a small test to help me understand the way an internet game should look. But I can't get it to work. The client connects fine, but only the server can see the client and not vice versa. I think the server, once it recieves the new client message (type 100), doesn't send a message identifying the host (type 101)

Here's the code. Thanks for helping.
Graphics 800,600,32,2
SetBuffer BackBuffer()

Global host,udpport,destport,main,intip

Type pl
Field x,y
Field name$,port
End Type

Type netpl
Field x,y
Field name$,port,ip
End Type

host=Input ("host (1) or join (0)")

If host=1 Then
udpport=4000
destport=4001
Else
udpport=4001
destport=4000
EndIf

;create the stream
main=CreateUDPStream(udpport)
If main=0 Then RuntimeError "Could not create stream"

Print "Stream Created"

If host=0 Then
ip$=Input$ ("Host IP: ")
intip=DotToInt%(ip$)  ;NEEDS TO BE AN INT IP!
WriteLine main,"100"+name$
SendUDPMsg main,intip,destport   ;send "new player" message 
EndIf 

name$=Input ("UserName: ")

;make local player
p.pl=New pl
p\x=200
p\y=300
p\name$=name$
p\port=UDPStreamPort(main)

While Not KeyDown(1)

UpdatePlayers()
UpdateNetwork()

Cls 
Draw()
Flip

Wend
CloseUDPStream(main)
End 

Function UpdateNetwork()

HandleMessages()   ;check messages
SendMessages()    ;send messages

End Function 

Function HandleMessages()

If senderip=RecvUDPMsg(main) Then
	msg$=ReadLine$(main)
	DebugLog msg$
	Select Left(msg$,3)
		Case 100
			DebugLog "Player Found"
			;get rid of extranous info
			name$=Replace$(msg$,"100","")
			;new player!
			n.netpl=New netpl
			n\x=600
			n\y=300
			n\name$=name$
			n\port=UDPMsgPort(main)
			n\ip=UDPMsgIP(main)
			;send him the server info
			WriteLine main,"101"+name$
			DebugLog UDPMsgIP(main)
			SendUDPMsg main,DotToInt(UDPMsgIP(main)),destport
			;ADD OTHER PLAYER INFO SENT
		Case 101
			;client identifies host
			DebugLog "Host Found"
			;get rid of extranous info
			name$=Replace$(msg$,"101","")
			;make new player
			n.netpl=New netpl
			n\x=200
			n\y=300
			n\name$=name$
			n\port=UDPMsgPort(main)
			n\ip=UDPMsgIP(main)
		Case 200
			;movement massage
			coor$=Replace$(msg$,"200","")
			newx=Left(coor$,3)
			newy=Right(coor$,3)
			;find player
			For n.netpl=Each netpl
				If UDPMsgIP(main)=n\ip Then
				n\x=newx
				n\y=newy
				EndIf
			Next 
	End Select 
EndIf

End Function 

Function SendMessages()

For p.pl=Each pl

WriteLine main,"200"+LSet$(p\x,3)+LSet(p\y,3)   ;write coordinates to stream

If host=0 Then SendUDPMsg main,intip,destport
If host=1 Then
;cycle through clients
For n.netpl=Each netpl
SendUDPMsg main,n\ip,destport
Next
EndIf

Next

End Function 

Function UpdatePlayers()

For p.pl=Each pl

If KeyDown(200) Then p\y=p\y-2
If KeyDown(208) Then p\y=p\y+2
If KeyDown(205) Then p\x=p\x+2
If KeyDown(203) Then p\x=p\x-2

Next 

End Function 

;Function Chat()
;
;If Not KeyDown(203) Or Not KeyDown(205) Or Not KeyDown(200) Or Not KeyDown(208) Then
;key=GetKey()
;EndIf
;
;If key
;
;End Function 

Function Draw()

For p.pl=Each pl
Text p\x,p\y,"{"+p\name$+"}",1,1
Next
For n.netpl=Each netpl
Text n\x,n\y,"["+n\name$+"]",1,1
Next

End Function 

Function DotToInt%(ip$)
	off1=Instr(ip$,".")	  :ip1=Left$(ip$,off1-1)
	off2=Instr(ip$,".",off1+1):ip2=Mid$(ip$,off1+1,off2-off1-1)
	off3=Instr(ip$,".",off2+1):ip3=Mid$(ip$,off2+1,off3-off2-1)
	off4=Instr(ip$," ",off3+1):ip4=Mid$(ip$,off3+1,off4-off3-1)
	Return ip1 Shl 24 + ip2 Shl 16 + ip3 Shl 8 + ip4
End Function