Sockets again

BlitzMax Forums/BlitzMax Programming/Sockets again

FBEpyon(Posted 2005) [#1]
Hey, Im having problems checking to see if there is a player that has joined, I think me and friend of mine have figured out the SocketAccept part, but now were trying to send data to double check this.. info



Global HostSocket
Global HostStream

Global JoinSocket
Global JoinStream

Global RData:Byte
Global SData:Byte

Global planelist:TList = CreateList()
Global p:netplane

Const PLAYER_JOIN = 100
Const PLAYER_LEFT = 101

Function CreateHostGame:Int(Port:Int)

	HostSocket = CreateTCPSocket()
	BindSocket HostSocket,Port
	HostStream = CreateSocketStream(HostSocket)
	SocketListen HostSocket

End Function

Function HostData()

	Local NewSocket = SocketAccept(HostSocket)
	
	If NewSocket
		
		If ReadByte(HostStream)=PLAYER_JOIN Then DrawText "Player Joined...",8,32
		
	EndIf
	
End Function

Function JoinData()

	DrawText DottedIP(SocketRemoteIP(JoinSocket))+":"+SocketRemotePort(JoinSocket),8,8
	DrawText SocketReadAvail(JoinSocket),8,32

End Function

Function ConnectGame(IP:String,Port:Int)

	JoinSocket = CreateTCPSocket()
	
	ConnectSocket JoinSocket,HostIp(IP),Port
	JoinStream = CreateSocketStream(JoinSocket)
	
End Function



there is the code please help


FBEpyon(Posted 2005) [#2]
Can someone please help me I have had this post up for 15hrs now and no replys to it, Im just lookin for some advice..


'new stuff
'server.bmx

Global ServerSocket
Global ServerStream
Global TotalPlayer

Function CreateConnection()

	ServerSocket = CreateTCPSocket()
	ServerStream:Stream = CreateSocketStream(ServerSocket)
	BindSocket ServerSocket,1280
	SocketListen ServerSocket
	
	Print "Host Socket Made..."
	Print "Waiting for Player(s).."

End Function

Function JoinedConnection()
	
	Local NewSocket = SocketAccept(ServerSocket)
	Local RData = ReadByte(ServerStream)
	
	If NewSocket
		TotalPlayer :+ 1
		Print RData
		Return True
	Else
		Return Null
	EndIf

End Function

Function CheckConnection()

	If SocketConnected(ServerSocket)
		Return True	
	Else
		Return Null	
	EndIf

End Function

CreateConnection

'Graphics 640,480,0

While Not KeyHit(KEY_ESCAPE)

	'Cls

	If JoinedConnection()=True
		Print "Player Joined"',8,8
		Print "Connected Player(s): " + TotalPlayer',8,32
	EndIf
	
	'Flip ; Cls

Wend



Here is the client


'client.bmx
'Client System

Global ClientSocket
Global ClientStream

Function CreateConnection()

	ClientSocket= CreateTCPSocket()
	ClientStream= CreateSocketStream(ClientSocket)
	'BindSocket ClientSocket,1280
	'SocketListen ClientSocket
	ConnectSocket ClientSocket,HostIp("127.0.0.1"),1280

End Function

Function CheckConnection()

	If SocketConnected(ClientSocket)
		Return True	
	Else
		Return Null	
	EndIf

End Function

CreateConnection

Graphics 640,480,0

While Not KeyHit(KEY_ESCAPE)

	Cls

		If CheckConnection()
			DrawText "Connected!!, Welcome",8,8
			DrawText "Host IP: " + DottedIP(SocketRemoteIP(ClientSocket)),8,32
			WriteByte ClientStream,1
		Else
			DrawText "Connection Lost!!",8,8
		EndIf
	
	Flip ; Cls

Wend