GNet Chat

BlitzMax Forums/BlitzMax Beginners Area/GNet Chat

YellBellzDotCom(Posted 2011) [#1]
Hello!
Its been a few months but I had to do some keyboard mashing and get some of my projects inline. I had to start with a networking base and after looking at some of the examples, most of which were a bit too much for what I needed or a bit too high level for me, I decided to write a little GNet Chat program just to help me understand a little of the networking code.

Here is the Server...
'Server
SuperStrict

Const GNETSLOT_MSG:Int = 0

Local host:TGNetHost = CreateGNetHost()
Local port:Int = 1234
Local address:String = "127.0.0.1"
Local timeout_ms:Int = 10000

Local listen:Int = GNetListen( host, port )
If Not listen Then RuntimeError "GNetListen failed"

Local localObj:TGNetObject = CreateGNetObject:TGNetObject( host )
Local remoteObj:TGNetObject
Local objList:TList = New TList

Local txt:String[32]
Local num:Int[32]

Local a:Int = 0
Local msgNum:Int = 0
Local netMsg:String = ""

Local maxMsg:Int = 20
Local msg:String[maxMsg]
Local i:Int = 0

Graphics 320,240

While Not KeyHit(KEY_ESCAPE)
	Delay 10

	GNetSync host
	
	objList = GNetObjects( host, GNET_MODIFIED )
	For remoteObj = EachIn objList
		'Get type of msg
		Local tString:String = GetGNetString( remoteObj, 0 )
		Local msgType:String = Left(tString,2)
		
		If(msgType = "01") Then
			'Server is being contacted by client, let client know they connected
			SetGNetString localObj,GNETSLOT_MSG,"01"
			'Cls
			'DrawText GetGNetString( remoteObj, 0 ), 10,10
			'DrawText Mid(tString,3,Len(tString)-2) + " has connected.", 10,10
			tString = Mid(tString,3,Len(tString)-2) + " has connected."
		ElseIf(msgType = "02") Then
			'Client has sent a message
			'Cls
			'DrawText GetGNetString( remoteObj, 0 ), 10,10
			'DrawText Mid(tString,3,Len(tString)-2), 10,10	
			tString = Mid(tString,3,Len(tString)-2)
			SetGNetString localObj,GNETSLOT_MSG,"02"+tString
		EndIf

		'Draw a scrolling message box
		Local c:Int = maxMsg-1
		For i = 0 To maxMsg-2
			msg[c] = msg[c-1]
			c = c - 1
		Next
		
		msg[0] = tString
		msgNum = msgNum + 1	
	Next
	
	'Get user input
	a = GetChar()
	If a > 0 Then
		'User is typing a message
		'Print("Key "+ Chr(a) + ", " + a + " was pressed")
		If a = 8 And Len(netMsg) > 0 Then 'user pressed backspace
			netMsg = Left(netMsg,Len(netMsg)-1)
		Else
			netMsg = netMsg + Chr(a)
		End If
			
		If a = 13 Then 'user is sending chat message by pressing enter
			SetGNetString localObj,GNETSLOT_MSG,"02Server: " +netMsg
			
			'Draw a scrolling message box
			Local c:Int = maxMsg-1
			For i = 0 To maxMsg-2
				msg[c] = msg[c-1]
				c = c - 1
			Next
			msg[0] = "Server: " +netMsg
			netMsg = ""
		End If
	End If
	
	'Draw Messages
	Cls
	SetColor(255,255,255)			
	Local b:Int = 200
	For i = 0 To maxMsg-1
		If Len(msg[i] > 0) Then
			DrawText msg[i],10,b
			b = b - 12
		EndIf
	Next
	
	'Draw User input
	SetColor(255,0,0)
	DrawText netMsg, 10,220
	
	
	Flip
	
Wend
CloseGNetHost(host)
End


Here is the Client...
'Client
SuperStrict

Const GNETSLOT_MSG:Int = 0

Local name:String = "Player"
Local host:TGNetHost = CreateGNetHost()
Local address:String = "127.0.0.1"
Local port:Int = 1234
Local timeout_ms:Int = 10000

Local connect:Int = GNetConnect( host, address$, port, timeout_ms )
If Not connect Then RuntimeError "GNetConnect failed"


Local localObj:TGNetObject = CreateGNetObject:TGNetObject( host )
Local remoteObj:TGNetObject
Local objList:TList = New TList

Local a:Int = 0
Local msgNum:Int = 0
Local netMsg:String = ""

Local maxMsg:Int = 20
Local msg:String[maxMsg]
Local i:Int = 0
Local tString:String
Local msgType:String

Local connReq:Int = 0

Graphics 320,240

While Not KeyHit(KEY_ESCAPE)

	Delay 10

	GNetSync host
	
	'get server messages
	objList = GNetObjects( host, GNET_MODIFIED )
	For remoteObj = EachIn objList
		'Get type of msg
		tString = GetGNetString( remoteObj, 0 )
		msgType = Left(tString,2)
		
		If(msgType = "01") Then
			'Server is being contacted by client, let client know they connected
			tString = "You are now connected to the server"
		ElseIf(msgType = "02") Then
			tString = Mid(tString,3,Len(tString)-2)
		EndIf
		
		'Draw a scrolling message box
		Local c:Int = maxMsg-1
		For i = 0 To maxMsg-2
			msg[c] = msg[c-1]
			c = c - 1
		Next
			
		msg[0] = tString
		msgNum = msgNum + 1		
	Next
	
	If connReq = 0 Then
		'server has not acknowledged connection
		SetGNetString localObj,GNETSLOT_MSG,"01" + name
		connReq = 1
		
	Else
		'Server has acknowledged connection
		a = GetChar()
		If a > 0 Then
			'User is typing a message
			'Print("Key "+ Chr(a) + ", " + a + " was pressed")
			If a = 8 And Len(netMsg) > 0 Then 'user pressed backspace
				netMsg = Left(netMsg,Len(netMsg)-1)
			Else
				netMsg = netMsg + Chr(a)
			End If
			
			If a = 13 Then 'user is sending chat message by pressing enter
				SetGNetString localObj,GNETSLOT_MSG,"02"+name+": " +netMsg
				netMsg = ""
			End If
		End If
	End If
	
	'Draw Server Messages
	Cls
	SetColor(255,255,255)			
	Local b:Int = 200
	For i = 0 To maxMsg-1
		If Len(msg[i] > 0) Then
			DrawText msg[i],10,b
			b = b - 12
		EndIf
	Next
	
	'Draw User input
	SetColor(255,0,0)
	DrawText netMsg, 10,220
	
	
	Flip
	
Wend
CloseGNetHost(host)
End



I have uploaded the compiled exe's to...
http://www.mediafire.com/?lwhz78nrliah7b7

Let me know what you think!
Thanks!