LAN/Internet Chat Help

Blitz3D Forums/Blitz3D Programming/LAN/Internet Chat Help

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.

SERVER:
Graphics 400,200,32,2
;SERVER;

Global t
Global msg$,port=49494,ready=False

Type user
	Field name$
	Field stream
End Type

server=CreateTCPServer(port)
If server=0 End

While Not KeyHit(1)
t=AcceptTCPStream(server)

If t
	msg$=ReadLine(t)
	If msg$="[LOGIN]"
		msg$=ReadLine(t)
		u.user=New user
		u\name$=msg$
		u\stream=t
		
		For u.user=Each user
			WriteLine u\stream,"[SERVER]"
			WriteLine u\stream,"--------------------------------------"
			WriteLine u\stream,"SERVER: "+Upper(msg$)+" HAS LOGGED ON!"
			WriteLine u\stream,"--------------------------------------"
		Next
	EndIf
EndIf

For u.user=Each user
	If Not Eof(u\stream)
		If (u\stream)
			msg$=ReadLine(u\stream)
			If msg$="[MSG]"
				msg$=ReadLine(u\stream)
				ready=True
			ElseIf msg$="[LOGOUT]"
				the_name$=u\name$
				the_stream=u\stream
				For u.user=Each user
					If u\stream=the_stream Then Delete u:Exit
				Next
				For u.user=Each user
					WriteLine u\stream,"[SERVER]"
					WriteLine u\stream,"--------------------------------------------"
					WriteLine u\stream,"SERVER: "+Upper(the_name$)+" HAS LOGGED OFF!"
					WriteLine u\stream,"--------------------------------------------"
				Next
			EndIf
		EndIf
	Else
		the_name$=u\name$
		the_stream=u\stream
		For u.user=Each user
			If u\stream=the_stream Then Delete u:Exit
		Next
		For u.user=Each user
			WriteLine u\stream,"[SERVER]"
			WriteLine u\stream,"--------------------------------------------"
			WriteLine u\stream,"SERVER: "+Upper(the_name$)+" HAS LOGGED OFF!"
			WriteLine u\stream,"--------------------------------------------"
		Next
	EndIf

If ready=True
	ready=False
	For u.user=Each user
		WriteLine u\stream,"[USER]"
		WriteLine u\stream,msg$
	Next
EndIf

Next

Delay 2

If KeyHit(1)
For u.user=Each user
	WriteLine u\stream,"[SERVER]"
	WriteLine u\stream,"--------------------------------------------"
	WriteLine u\stream,"SERVER: THE SERVER IS OFFLINE!"
	WriteLine u\stream,"--------------------------------------------"
Next
EndIf

Wend
End


CLIENT:
Graphics 640,480,32,2

Global t,msg$,mesg$,name$

font=LoadFont("courier new",16):SetFont font

Print "Connecting..."

t=OpenTCPStream("127.0.0.1",49494)
If t=0
	Print "COULD NOT CONNECT"
	WaitKey()
	End
Else
	Print "CONNECTED"
	WaitKey()
EndIf

name$=Input$("Name: ")
If name$=""
	name$="UNKNOWN"
EndIf

WriteLine t,"[LOGIN]"
WriteLine t,name$
Cls
Locate 0,0
Print "WELCOME!!"

While Not KeyHit(1)
If t
	msg$=ReadLine(t)
	If msg$="[USER]"
		msg$=ReadLine(t)
		Print msg$
	ElseIf msg$="[SERVER]"
		Color 255,0,0
		msg$=ReadLine(t)
		Print msg$
		msg$=ReadLine(t)
		Print msg$
		msg$=ReadLine(t)
		Print msg$
		Color 255,255,255
	EndIf
EndIf

If KeyDown(29)
	While KeyDown(29)
	Wend
	
	mesg$=Input$(name$+": ")
	If mesg$<>""
		mesg$=name$+": "+mesg$
		WriteLine t,"[MSG]"
		WriteLine t,mesg$
	EndIf
EndIf

Delay 2
Wend
WriteLine t,"[LOGOUT]"
End