'NETWORK File transfer demo program.
I_am = Network("localhost")
InFile$ = "Z:\op_copy.avi" ' 1899 megabytes file Both this strings are always 128 bytes long (DIM)
OutFile$ = "E:\copy.wav" ' copy send via network to this client place
CASE 1 'HOST - in this demo HOST send FILE to client res$
= InFile$
+ MKL$(LOF(1)) ' size is set by DIM for InFile$ + 4 bytes LONG value for file size PUT #Host
, , res$
' send file name and file size PRINT "Waiting for confirmation of the name and size ..."
ok$ = Transfer$(Host, 2)
ok$ = ""
PRINT "Uploading file..."
zbyva&
= LOF(1) ' This variable in begin is the same value as uploaded file size DO WHILE zbyva&
> 0 ' Block for transport is 10 000 000 bytes, upload until all bytes are send zbyva& = zbyva& - PACKET
IF LOF(1) > PACKET
THEN DO UNTIL Transfer$
(Host
, 2) = "OK":
LOOP ' wait for confirmation, that all bytes from 10 000 000 bytes packet comming to CLIENT. LOOP ' THEN is next packet send.
PRINT "Waiting for confirmation FILE SIZE" message$ = Transfer$(Host&, 4) ' It checks if the created and written file is the same size as the source file
CASE 2 'CLIENT - in this demo receive and write file
PRINT "Receiving info about file..." file$ = Transfer$(Client, 132)
FileName$
= LTRIM$(LEFT$(file$
, 128)) ' maximal file name lenght is set by DIM to 128 bytes in this program PRINT "File size (bytes): "; FileSize&
PRINT "Confirm file name and file size ..." ok$ = "OK"
'if filesize is smaller than 10 000 000 bytes, use standard way without blocks
Content$ = Transfer$(Client, FileSize&)
OPEN RTRIM$(OutFile$
) FOR OUTPUT AS #2:
CLOSE #2 ' create new clear file - in this case named differently, but in real network use File_Name$, here its for localhost test in the same directory confirm$
= MKL$(Writed_Size&
) ' client post WRITED SIZE as control back to Host
ELSE 'if file is bigger than 10 000 000 bytes, use blocks: OPEN RTRIM$(OutFile$
) FOR OUTPUT AS #2:
CLOSE #2 ' create new clear file - in this case named differently, but in real network use File_Name$, here its for localhost test in the same directory zbyva& = FileSize&
IF FileSize&
> 0 THEN ' For percentage transfer calculation TotalBlocks
= INT(FileSize&
/ PACKET
) + TB
'in every loop in transmitted one 10 000 000 bytes lenght block
IF zbyva&
> PACKET
THEN Block$
= Transfer$
(Client
, PACKET
) ELSE Block$
= Transfer$
(Client
, zbyva&
) zbyva& = zbyva& - PACKET
ok$ = "OK"
PUT #Client
, , ok$
' send confirmation, that this block is writed and can be send next by HOST
Block = Block + 1
Block$ = ""
perc
= INT(100 * (Block
/ TotalBlocks
)) IF oldperc
<> perc
THEN oldperc
= perc:
LOCATE Ln
,:
PRINT "Block "; Block;
"/"; TotalBlocks;
"("; perc;
"%) " confirm$
= MKL$(Writed_Size&
) ' client post WRITED SIZE as control back to HostCLOSE #2, #1, #Host&
, #Client&
Transfer$ = ""
' IF _CONNECTED(channel&) = 0 THEN PRINT "Connection failure.": END
Network = 2 'client
Network = 1