Downloading files from pc to pc using B3D

Blitz3D Forums/Blitz3D Programming/Downloading files from pc to pc using B3D

xmlspy(Posted 2003) [#1]
.


eBusiness(Posted 2003) [#2]
I'm not an expert, but yes it's possible. Just read the file on the sender pc, preferably send it as packages. Remember not to send too much until the reciever has confirmed that the data has been recieved. I wouldn't wonder if somebody had a complete code set doing this.


jfk EO-11110(Posted 2003) [#3]
check the code archives


BlitzSupport(Posted 2003) [#4]
I made this yesterday for someone -- it only does very basic checking (not really suitable for sending huge files), but might be of some use. I haven't actually tested it over the net (only locally via 127.0.0.1), but it 'should' work!

------

To run the demo...

Load both sources into the Blitz editor. Run one of them, then run the other within 10 seconds (default wait period). The file will be sent between the two.

To use in real life, you'll need the IP address of the computer sending the file. Call ReceiveNetFile with this IP address, then have the sender call SendNetFile (obviously requires a bit of communication between the PCs to time things right). The computer calling ReceiveNetFile will save a copy as named.


; Waits for up to 'timeout' seconds for incoming connection on given port,
; and sends named file if it gets one...

Function SendNetFile (file$, port = 9999, timeout = 10000)

	server = CreateTCPServer (port)
	
	If server
	
		timer = MilliSecs ()
		
		Repeat
		
			incoming = AcceptTCPStream (server)
			
			If incoming
			
				file = ReadFile (file$)
				
				If file
					Repeat
						WriteByte incoming, ReadByte (file)
					Until Eof (file)
					CloseFile file
					complete = True
				EndIf
			
				CloseTCPStream incoming
				
			Else
				
				Delay 500
				
			EndIf
	
		Until (incoming) Or (MilliSecs () > timer + timeout)
			
		CloseTCPServer server
		
	EndIf

	Return complete
	
End Function

; D E M O . . .

; Send test.txt to any incoming connection made within 'timeout' period...

If SendNetFile ("test.txt")
	Notify "File sent!"
Else
	Notify "File not sent!"
EndIf



Function ReceiveNetFile (ip$, savefile$, port = 9999, timeout = 10000)

	timer = MilliSecs ()
	
	Repeat
	
		incoming = OpenTCPStream (ip$, port)
		
		If incoming
	
			save = WriteFile (savefile$)
			
			If save
				Repeat
					WriteByte save, ReadByte (incoming)
				Until Eof (incoming)
				CloseFile save
				complete = True
			EndIf
			
			CloseTCPStream incoming
		
		Else
			Delay 500
		EndIf
	
	Until (incoming) Or (MilliSecs () > timer + timeout)

	Return complete
		
End Function

; D E M O . . .

; Receive file from IP address 127.0.0.1 and save as temp.txt...

If ReceiveNetFile ("127.0.0.1", "temp.txt")
	Notify "File received!"
Else
	Notify "File not received!"
EndIf