TCP-ing from client(s) to a server

Blitz3D Forums/Blitz3D Programming/TCP-ing from client(s) to a server

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.

There is a couple of posts regarding transferring data and the like.

Tried to keep code minimalist so can be expanded further....

Using the client and server code below,compile them and
create a sub directory in the same directory as your compiled server_v2 and client_v2 executables, called file_dir.

This sub directory is used by the server to maintain a list of the clients that are running, without it the VB.net front end will not work.

I have developed a VB.net front end for the two executables server_v2 and client_v2 that keeps a running count of the clients running,
will kill the server and subsequently clean up the file_dir sub directory of historic client files. You do not want
to put any of your own files within the file_dir directory, this directory gets cleaned very well upon Kill Server!

Link to VB.net front end:
http://myweb.tiscali.co.uk/diecastcars/File_Counter.exe

NOTE: Do Not try to run the File_Counter.exe file before you have created a subdirectory call file_dir at the same path. Nothing BAAaaad will happen, it just wont work (;-)
So: You might want once you compile the two slices of code below, called them server_v2.exe and client_v2.exe respectively then put them at the same path as File_Counter.exe, else the start server and client buttons won't find what they need.


Server:

; TCP engine ULTRA FAST
;
; Written by Daniel Eriksson  (daniel@...)
;
; save executable as :
;
; server_v2.exe
;
; for VB.net front end to locate.  Also add a sub-directory
; called "file_dir" for the server to put files associated 
; with clients

; added to by M Gormley, also incorporated
; a VB.net front end which can be used to start
; the server, start clients, monitor how many clients
; are running, kill the server and cleanup files.


;Public Domain free do use as wish including selling it.

;VARS
max_bank=512
global_client_count=0
Dim file_name$(255); 255 possible clients - who'd need more?
Global file_count=0


Type session

	Field link
	Field tcp_sendbank
	Field tcp_recbank
	Field status

End Type

;window=CreateWindow("Key press me window",100,100,640,200,0,1)  BBasic
Graphics 400,700,16,2            ; B3D

server=CreateTCPServer(7056)
Print "server is running.."

msmin=9999999 ; minimums millisecs :D

While Not KeyHit(1)   ;Hehe this command dosent work in BlitzPluss with the console window.

	stream = AcceptTCPStream(server)
	If stream
		global_client_count=global_client_count+1
		Print "New stream detected. memory allocated."
		ses.session = New session
		ses\link=stream
		ses\tcp_sendbank = CreateBank(max_bank)	
		ses\tcp_recbank = CreateBank(max_bank)
		ses\status =10 ; Simulate login.
		;ses\client_count=global_client_count
		
		fn_create_file(Int ses\link)
	
	End If


	oldms=MilliSecs()
	;Validate streams.. 
	For ses.session = Each session
	
		If Eof(ses\link) 
			Print "A session called "+ses\link+" has stop working."
			Print "Frees memory banks for session "+ses\link
			FreeBank ses\tcp_sendbank 
			FreeBank ses\tcp_recbank
			Print "deletes sessions "+ses\link
			
			numberin=ses\link
			fn_delete_file(Int ses\link)
			
			Delete ses
			global_client_count=global_client_count-1
		End If
		
	Next
	
	
	For ses.session = Each session
	
	
		Select ses\status
		Case 10
		
			If ReadAvail(ses\link)>=max_bank
			
				;so we got a new packet :D and its all 100% secure... so we can go on live our lifes
				r=ReadBytes (ses\tcp_recbank,ses\link,0,max_bank)
				Print "for session "+ses\link+" who has a tcp recbank of:"+ses\tcp_recbank
				Print "read "+r+" bytes from stream"
				
				;my attempt at accessing sent count
				rx_count=PeekByte(ses\tcp_recbank,0)
				Print"rx_count is:"+rx_count
			
				ses\status=12 ; well time to reply this packet next round :D
			
			End If
		
		Case 12 ; Well time to reply a packet.
		
			;First we fill it upp with 0
			For i=0 To (max_bank-1)
				;PokeByte ses\tcp_sendbank,i,Rnd(255)
				PokeByte ses\tcp_sendbank,i,Rnd(255)
			Next
			
			

			rx_count=PeekByte(ses\tcp_recbank,0)
			
			;client_count=PeekByte(ses\tcp_recbank,0)
			PokeByte ses\tcp_sendbank,0,global_client_count
			PokeByte ses\tcp_sendbank,1,rx_count
			Print " sent back a rx_count of:"+rx_count

			
			;Secondly we reply it kindly.
			s=WriteBytes (ses\tcp_sendbank,ses\link,0,max_bank)
			;Print "Succesfull sending "+s+" bytes of data"
			 packetssent= packetssent+1
	
	
			ses\status=10 ; Well we cant wait for next coll packet to arive :D"
			
		End Select
	
	
	Next	
	
	ms=MilliSecs()-oldms
	If ms>msmax Then msmax=ms
	If ms<msmin Then msmin=ms
	count=count+1
	cmsmed=cmsmed+ms
	msmed=cmsmed/count   ; medium millisecs
	
	
	tick=tick+1
	If tick>=500 Then Print "Ticks: "+count+" MS="+ms+" MIN="+msmin+" MED="+msmed+" MAX="+msmax+ "Packets sent "+ packetssent : tick=0

	
	If KeyHit(19) Then MSMIN=99999 : msmed=0 : count=0 : msmax=0 : packetssent=0 :cmsmed=0: Print "Counter resets."

	Delay 10 ; this gives cpu time to rest of OS in windows. as server is not so cpu intensive.

Wend
;---------------------------------------------------------------------
;  functions
;---------------------------------------------------------------------



Function fn_delete_file(numberin)
	Print "Deleting File: "+numberin
	del_file_name$="file_dir/"+numberin+".ini"
	DeleteFile (del_file_name$)
	Print "File Deleted"	
End Function


Function fn_create_file(numberin)

	BestName$="Mark"
	;BestScore$="123"
	;BestLevel$="One and only"

	Print "Ganna create a text file with this:"+numberin+" identity."
	
	;file_name$(file_count)="file_dir/"+file_count+"_"+numberin+".ini"
	;got rid of file count prefix string manip hassle
	file_name$(file_count)="file_dir/"+numberin+".ini"
	Print file_name$(file_count)
	writethis$=file_name$(file_count)
	file_count=file_count+1
	
	; Open a file to write to 
	fileout = WriteFile(writethis$)
	Print"-------------------------------------"
	Print"fileout is:"+writethis$
	Print"-------------------------------------"
	; Write the information to the file 
	WriteString( fileout, BestName$ ) 
	;WriteInt( fileout, BestScore$ ) 
	;WriteByte( fileout, BestLevel$ ) 
	
	Print "Wrote to:"+writethis$

	; Close the file 
	CloseFile( fileout ) 
	
End Function







Client:


;------------ Client part ------------------------------
; this file needs to be saved as:
;
; client_v2.exe
;
; for VB.net front end to locate


; this is a dumb client.. it only spams server :D

max_bank = 512
sent_count=0


tcp_sendbank=CreateBank(max_bank)
tcp_recbank=CreateBank(max_bank)

;link=OpenTCPStream("www.tiberion.com",7056)
link=OpenTCPStream("127.0.0.1",7056)			;NOTE:  might need to replace with your IP info

If Not link Then Print "failed to create link." : Delay 3000 : End

Print "Connected.. let the spam flood."

status=12; we emulate a logged in client

While Not KeyHit(1)

	If Eof(link) Then Print "What server died ??? why   WHYYY !!?? " : Delay 5000 : End 


	Select status
	
	Case 10
	
		If ReadAvail(link)>=max_bank
		
			;time to read some ;)
			r=ReadBytes(tcp_recbank,link,0,max_bank)
			Print "have readin "+r+" bytes from server."
			Print "-----------------------------------------------------"
			;my attempt at accessing sent count
				xth=PeekByte(tcp_recbank,0)
				serv_count=PeekByte(tcp_recbank,1)
				Print"client count is:"+xth
				
			Print "Server reckons there is "+xth+" clients connected......"
			Print "Server thinks this clients sent him: "+serv_count+" packets of 512 bytes"
			Print "------------------------------------------------------"
			status=12 ; Yeppp time to respond... 
		End If
			
	
	Case 12
	
		;haha client time to spam server first we make a cool tcp packet with random nunbers.
		For i=0 To (max_bank-1)
			PokeByte tcp_sendbank,i,Rnd(255)
			;Print "filled 512 bytes with randz, bar first one"
			If sentcount<255
			PokeByte tcp_sendbank,0,sent_count
			Else sentcount=255
			EndIf
		Next
		
		;well we got our important randomnes.. time ti send the junk spam to server.
		s=WriteBytes (tcp_sendbank,link,0,max_bank)
		;Print "Sending "+s+" bytes to server."+sent_count+" times"
		sent_count=sent_count+1
		
		status=10  ; Well we go out and wait for packet :D
		
	
	End Select
	
		tick=tick+1
	If tick>=500 Then Print CurrentTime()+"...tick..." : tick=0



	;Delay 10 ; yep we have to delay some here to emulate many clients.. or cpu dies.
    Delay 500  ; MG Delay
Wend




Hope this helps someone.....