Send files

BlitzPlus Forums/BlitzPlus Programming/Send files

Does anybody know how to send files in an Blitz+ Program?

Wanna be a bit more specific? Send them to where? From where?

From PC to Pc. Peer-to-Peer. Like in ICQ

Deep breath...

You need to establish a TCP connection between a client and a server app. On the server app, the file should be loaded into a bank, and then using the WriteBytes command, write the bank's content to the network stream to be received by the client with ReadBytes into a bank, which you can then save as a file.

Thank you. But this will be my first program using Bank/TCP. Do you have an example? That would be really nice ;-)

From one PC to another? On a LAN:
file% = WriteFile("\\Machine\SharedFolder\file.jpg")


I haven't really used BlitzPlus for a long time as I now use MaxGUI, but I found this code in the archives and have adapted it for sending files. I haven't tested it but it should work:

Server:

;---------------------------SERVER--------------------------
;small example on how to write to files over the net
;BY:Ckob

myfilestring$ = "C:\Windows\Notepad.exe"
myfile=CreateBank(FileSize(myfilestring$))	;Create memory bank the same size as the file
ReadBytes(myfile,ReadFile(myfilestring$),0,FileSize(myfilestring$))	;Load a read file into a memory bank


mainsvr=CreateTCPServer(8080) ;create the server

If mainsvr<>0 Then 

	Print "Server started successfully."

Else

	Print "Server failed to start."
	End

End If

While Not KeyHit(1)

	strStream=AcceptTCPStream(mainsvr)

	If strStream Then 

		Print "Writing file to stream..."
		WriteBytes(myfile,strStream,0,FileSize(myfilestring$))
		Print "Written bytes..."

	Else 

		Print "No message has been recieved yet"
		Delay 1000

	End If

Wend
End
;-----------------------------------------------------------


Client (run after you've started the server):

;---------------------------Client--------------------------
;small example on how to write to files over the net
;BY:Ckob

mainsvr=OpenTCPStream("127.0.0.1",8080)		;127.0.0.1 is always the IP address of the local computer (localhost)

If mainsvr<>0 Then 

	Print "Client Connected successfully."
	Delay 5000
	
	If ReadAvail(mainsvr) Then
		
		Print "Opening File To Write To"
		tmpFile = WriteFile("C:\Notepad received.exe")
		If Not tmpFile Then End
		Print "Create a bank to receive file contents from stream..."
		tmpBank = CreateBank(ReadAvail())
		Print "Reading the data from the stream into the bank..."
		ReadBytes(tmpBank,mainsvr,0,BankSize(tmpBank))
		Print "Write from the stream into the file we opened"
		WriteBytes(tmpBank,tmpFile,0,BankSize(tmpBank))
		Print "Close file"
		CloseFile(tmpFile)
		End

	EndIf

	Print "No data has been sent - try increasing the delay..."
	End	

Else

	Print "Server failed to connect."
	WaitKey 
	End

End If
;-----------------------------------------------------------


Sorry if it doesn't work (I don't have BlitzPlus installed) but it should give you a *very* primitive idea of how to transfer a file. To be used, in practice, you need to send network messages etc.



Seb

If its a binary file, like a BMP, readstring$ and writestring$ can send a pic PDQ.