Author Topic: Help with a Simple file transfer from a client to the host to send 2 text files.  (Read 7183 times)

0 Members and 1 Guest are viewing this topic.

Offline Chris80194

  • Newbie
  • Posts: 42
    • View Profile
Client file code
Code: QB64: [Select]
  1. ' Client computer: Sends the file
  2.  
  3. 'INPUT "Enter a port number to broadcast on: ", port$
  4. port$ = "12345" 'comment out demo code
  5.  
  6. 'INPUT "Enter the IP number to connect to: ", ipnum$
  7. ipnum$ = "192.168.3.208" 'comment out demo code
  8.  
  9. wconnect$ = "TCP/IP:" + port$ + ":" + ipnum$
  10.  
  11. 'INPUT "Enter the name of a file to send: ", s$
  12. 's$ = "Mileage.csv" 'comment out demo code
  13. 't$ = "TripLog.csv"
  14. S$ = "test2."
  15. T$ = "test1."
  16.  
  17. PRINT "try to connect to Station " + wconnect$
  18. PRINT "This Station IP is " + GetLocalIP$
  19. PRINT "Send Files " + S$ + " " + T$
  20. client = _OPENCLIENT(wconnect$)
  21. IF client = 0 THEN PRINT "Could not connect!": END
  22.  
  23. PRINT "Sending " + S$ + "..."
  24. PRINT #client, S$
  25. size& = LOF(2)
  26. PRINT #client, size&
  27. BufferSize& = 1024
  28. BytesPerSecond& = 1048576
  29. Buffer$ = SPACE$(BufferSize&)
  30. FOR o& = 1 TO size& STEP BufferSize&
  31.     IF o& + BufferSize& - 1 > size& THEN Buffer$ = SPACE$(size& - o& + 1)
  32.     GET #2, , Buffer$
  33.     PUT #client, , Buffer$
  34.     _DELAY BufferSize& / BytesPerSecond&
  35.     PRINT ".";
  36. PRINT 'new line
  37. 'CLOSE #client
  38. PRINT "File " + S$ + " sent successfully!"
  39.  
  40. PRINT "Sending " + T$ + "..."
  41. PRINT #client, T$
  42. size& = LOF(2)
  43. PRINT #client, size&
  44. BufferSize& = 1024
  45. BytesPerSecond& = 1048576
  46. Buffer$ = SPACE$(BufferSize&)
  47. FOR o& = 1 TO size& STEP BufferSize&
  48.     IF o& + BufferSize& - 1 > size& THEN Buffer$ = SPACE$(size& - o& + 1)
  49.     GET #2, , Buffer$
  50.     PUT #client, , Buffer$
  51.     _DELAY BufferSize& / BytesPerSecond&
  52.     PRINT ".";
  53. PRINT 'new line
  54. CLOSE #client
  55. PRINT "File " + T$ + " sent successfully!"
  56.  
  57. FUNCTION GetLocalIP$
  58.     SHELL _HIDE "cmd /c ipconfig > IPconfig.tmp"
  59.     A = FREEFILE
  60.     OPEN "IPconfig.tmp" FOR INPUT AS #A
  61.     DO
  62.         LINE INPUT #A, ipline$
  63.         IF UCASE$(LEFT$(LTRIM$(ipline$), 4)) = "IPV4" THEN
  64.             GetLocalIP$ = MID$(ipline$, INSTR(ipline$, ":") + 2)
  65.             IF GetLocalIP$ = "127.0.0.1" THEN GOTO 77
  66.             CLOSE #A
  67.             KILL "IPconfig.tmp" 'kill the messenger?
  68.             EXIT DO
  69.            77
  70.         END IF
  71.     LOOP UNTIL EOF(1)
  72.  
  73.  

The above code acts like it connects and sends the files named in the code just simple text files

The Host connects and spits out Zero and Zero in 2 columns and wont return to :1 to start over and wait, so it could receive another file if another one would be sent.

Code: QB64: [Select]
  1. ' Host Computer: Receives the file
  2.  
  3. 'INPUT "Enter the port number to listen on: ", port$
  4. 1
  5. COLOR 7, 4
  6.  
  7. port$ = "12345" 'comment out demo code
  8.  
  9. v$ = "TCP/IP:" + port$
  10. host = _OPENHOST(v$)
  11. TTime$ = DATE$ + "_" + LEFT$(TIME$, 2) + "." + MID$(TIME$, 4, 2) + "." + RIGHT$(TIME$, 2) + ".TXT"
  12. Path$ = "C:\Users\Chris\Downloads\Files"
  13. Receive$ = Path$ + TTime$
  14. Logging$ = Path$ + "IPLOG.txt"
  15. LOCATE 10, 27: PRINT "Esc to shut down host."
  16. IF host <> 0 THEN LOCATE 12, 20: PRINT GetLocalIP$ + ":" + port$; " You are the host."
  17. LOCATE 11, 20: PRINT "Path to save to is " + Path$
  18.  
  19.     keyed$ = INKEY$
  20.     IF keyed$ = CHR$(27) THEN SYSTEM
  21.     newclient = _OPENCONNECTION(host)
  22.     IF newclient <> 0 THEN
  23.         PRINT "Another computer has connected! "
  24.         B = FREEFILE
  25.         OPEN Logging$ FOR APPEND AS #B
  26.         IP$ = _CONNECTIONADDRESS(newclient)
  27.         PRINT IP$ + " has joined." ' displayed to Host only
  28.         PRINT #B, IP$, TTime$ ' print info to a log file
  29.         CLOSE B
  30.  
  31.         DO
  32.             INPUT #newclient, s$
  33.         LOOP UNTIL EOF(newclient) = 0
  34.  
  35.         '        DO
  36.         '       INPUT #newclient, size&
  37.         '      LOOP UNTIL EOF(newclient) = 0
  38.  
  39.         PRINT "Downloading " + s$ + "... To " + Receive$
  40.         C = FREEFILE
  41.         PRINT "--------------------------------"
  42.         PRINT newclient
  43.         PRINT EOF(newclient)
  44.         PRINT s$
  45.         PRINT size&
  46.         PRINT "--------------------------------"
  47.  
  48.         OPEN Receive$ + s$ FOR OUTPUT AS #C
  49.         filesize& = 0
  50.         DO
  51.             GET #newclient, , UT$
  52.             filesize& = filesize& + LEN(UT$)
  53.             IF LEN(t$) THEN PRINT #C, , UT$: PRINT ".";
  54.             _DELAY 0.01
  55.             PRINT filesize&, size&
  56.             '        LOOP UNTIL filesize& = size&
  57.         LOOP
  58.         PRINT 'newline
  59.         CLOSE #B
  60.         CLOSE #newclient
  61.         PRINT s$ + Receive$ + s$ + " successfully!"
  62.     END IF
  63.  
  64.     keyed$ = INKEY$
  65.     IF keyed$ = CHR$(27) THEN SYSTEM
  66.     newclient = _OPENCONNECTION(host)
  67.     IF newclient <> 0 THEN
  68.         PRINT "Another computer has connected! "
  69.         B = FREEFILE
  70.         OPEN Logging$ FOR APPEND AS #B
  71.         IP$ = _CONNECTIONADDRESS(newclient)
  72.         PRINT IP$ + " has joined." ' displayed to Host only
  73.         PRINT #B, IP$, TTime$ ' print info to a log file
  74.         CLOSE B
  75.  
  76.         DO
  77.             INPUT #newclient, t$
  78.         LOOP UNTIL EOF(newclient) = 0
  79.  
  80.         '        DO
  81.         '       INPUT #newclient, size&
  82.         '      LOOP UNTIL EOF(newclient) = 0
  83.  
  84.         PRINT "Downloading " + t$ + "... To " + Receive$
  85.         C = FREEFILE
  86.         PRINT "--------------------------------"
  87.         PRINT newclient
  88.         PRINT EOF(newclient)
  89.         PRINT t$
  90.         PRINT size&
  91.         PRINT "--------------------------------"
  92.  
  93.         OPEN Receive$ + t$ FOR OUTPUT AS #C
  94.         filesize& = 0
  95.         DO
  96.             GET #newclient, , UT$
  97.             filesize& = filesize& + LEN(UT$)
  98.             IF LEN(t$) THEN PRINT #C, , UT$: PRINT ".";
  99.             _DELAY 0.01
  100.             PRINT filesize&, size&
  101.             '        LOOP UNTIL filesize& = size&
  102.         LOOP
  103.         PRINT 'newline
  104.         CLOSE #B
  105.         CLOSE #newclient
  106.         PRINT t$ + Receive$ + t$ + " successfully!"
  107.     END IF
  108.  
  109. FUNCTION GetLocalIP$
  110.     SHELL _HIDE "cmd /c ipconfig > IPconfig.tmp"
  111.     A = FREEFILE
  112.     OPEN "IPconfig.tmp" FOR INPUT AS #A
  113.     DO
  114.         LINE INPUT #A, ipline$
  115.         IF UCASE$(LEFT$(LTRIM$(ipline$), 4)) = "IPV4" THEN
  116.             GetLocalIP$ = MID$(ipline$, INSTR(ipline$, ":") + 2)
  117.             IF GetLocalIP$ = "127.0.0.1" THEN GOTO 77
  118.             CLOSE #A
  119.             KILL "IPconfig.tmp" 'kill the messenger?
  120.             EXIT DO
  121.            77
  122.         END IF
  123.     LOOP UNTIL EOF(1)
  124.  
  125.  

This looks like it should be a simple fix but TCP was not around when I leaned Basic code.
Thank you for your help.

Ps. I would like a snip of code to verify if a certain IP or maybe domain name can be reached before making an automated attempt in the client side.
Thanks again
« Last Edit: November 13, 2018, 12:19:59 am by Chris80194 »

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
I think, you search IF _CONNECTED (connectionhandle&)? I have a TCP / IP transmission program somewhere, but I have to find it first

FellippeHeitor

  • Guest
I'm still to give your code a more thorough read but one thing caught my eye at a first glance: don't use PRINT and INPUT with TCP/IP. Use GET/PUT. I'll get back to you later after I've had time to analyse it further.

Offline Chris80194

  • Newbie
  • Posts: 42
    • View Profile
Petr How would I use  IF _CONNECTED (connectionhandle&)
Just like that or do I need to add a loop of some kind?  Thanks


FellippeHeitor GET/PUT ill have to change those into the program.... I think the print and input was in the original code snippet that I found.  Thanks

Please keep in touch with any ideas and updates, Thanks.

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Hi. I found the program, which use it:

Code: QB64: [Select]
  1. 'NETWORK File transfer demo program.
  2.  
  3.  
  4. DIM SHARED Host AS LONG, Client AS LONG
  5. DIM InFile AS STRING * 128, OutFile AS STRING * 128, ok AS STRING * 2
  6. CONST PACKET = 4096
  7.  
  8.  
  9.  
  10. I_am = Network("localhost")
  11. InFile$ = "Z:\op_copy.avi" '             1899 megabytes file                         Both this strings are always 128 bytes long (DIM)
  12. OutFile$ = "E:\copy.wav" '    copy send via network to this client place
  13.  
  14.  
  15.     CASE 1 'HOST   -    in this demo HOST send FILE to client
  16.         _TITLE "Host"
  17.         IF _FILEEXISTS(InFile$) THEN OPEN InFile$ FOR BINARY AS #1 ELSE BEEP: PRINT "File for transfer (source) not found"; InFile$: END
  18.         PRINT "Sending File name and file size...("; RTRIM$(InFile$); " and"; LOF(1); LTRIM$(")")
  19.         res$ = InFile$ + MKL$(LOF(1)) '                       size is set by DIM for InFile$ + 4 bytes LONG value for file size
  20.         PUT #Host, , res$ '                                   send file name and file size
  21.         _DELAY .005
  22.         PRINT "Waiting for confirmation of the name and size ..."
  23.  
  24.         ok$ = Transfer$(Host, 2)
  25.         IF ok$ = "OK" THEN PRINT "Name and size transferred correctly." ELSE END
  26.         ok$ = ""
  27.  
  28.         PRINT "Uploading file..."
  29.  
  30.         zbyva& = LOF(1) '                               This variable in begin is the same value as uploaded file size
  31.         DO WHILE zbyva& > 0 '                           Block for transport is 10 000 000 bytes, upload until all bytes are send
  32.             IF zbyva& > PACKET THEN Block$ = SPACE$(PACKET) ELSE Block$ = SPACE$(zbyva&)
  33.             zbyva& = zbyva& - PACKET
  34.             GET #1, , Block$ '
  35.             PUT #Host, , Block$
  36.             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.
  37.         LOOP '                                           THEN is next packet send.
  38.  
  39.         PRINT "Waiting for confirmation FILE SIZE"
  40.         message$ = Transfer$(Host&, 4) '                 It checks if the created and written file is the same size as the source file
  41.         IF CVL(message$) = LOF(1) THEN PRINT "Image transferred correctly. ": END ELSE PRINT "Post:"; LOF(1); "bytes, but received"; CVL(message$); "bytes!"
  42.     CASE 2 'CLIENT - in this demo receive and write file
  43.         _TITLE "Client"
  44.  
  45.         PRINT "Receiving info about file..."
  46.         file$ = Transfer$(Client, 132)
  47.         FileName$ = LTRIM$(LEFT$(file$, 128)) ' maximal file name lenght is set by DIM to 128 bytes in this program
  48.         FileSize& = CVL(RIGHT$(file$, 4))
  49.         PRINT "File in transfer: "; RTRIM$(FileName$)
  50.         PRINT "File size (bytes): "; FileSize&
  51.  
  52.         PRINT "Confirm file name and file size ..."
  53.         ok$ = "OK"
  54.         PUT #Client, , ok$
  55.  
  56.  
  57.         'if filesize is smaller than 10 000 000 bytes, use standard way without blocks
  58.         IF FileSize& < PACKET THEN
  59.             Content$ = Transfer$(Client, FileSize&)
  60.  
  61.             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
  62.             OPEN RTRIM$(OutFile$) FOR BINARY AS #2 '               write binary data to file
  63.             PRINT "Writing file..."
  64.             PUT #2, , Content$
  65.             Writed_Size& = LOF(2)
  66.             CLOSE #2
  67.             confirm$ = MKL$(Writed_Size&) '                        client post WRITED SIZE as control back to Host
  68.             PUT #Client&, , confirm$
  69.             IF Writed_Size& = FileSize& THEN PRINT "Transfer OK!"
  70.  
  71.  
  72.         ELSE 'if file is bigger than 10 000 000  bytes, use blocks:
  73.             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
  74.             OPEN RTRIM$(OutFile$) FOR BINARY AS #2 '               write binary data to file
  75.             zbyva& = FileSize&
  76.  
  77.             IF FileSize& > 0 THEN '                                For percentage transfer calculation
  78.                 IF FileSize& MOD PACKET THEN TB = 1
  79.                 TotalBlocks = INT(FileSize& / PACKET) + TB
  80.             END IF
  81.             Ln = CSRLIN
  82.             'in every loop in transmitted one 10 000 000 bytes lenght block
  83.             DO WHILE zbyva& > 0
  84.                 IF zbyva& > PACKET THEN Block$ = Transfer$(Client, PACKET) ELSE Block$ = Transfer$(Client, zbyva&)
  85.                 zbyva& = zbyva& - PACKET
  86.                 PUT #2, , Block$
  87.                 ok$ = "OK"
  88.                 PUT #Client, , ok$ '                                send confirmation, that this block is writed and can be send next by HOST
  89.  
  90.                 Block = Block + 1
  91.                 Block$ = ""
  92.                 perc = INT(100 * (Block / TotalBlocks))
  93.                 IF oldperc <> perc THEN oldperc = perc: LOCATE Ln,: PRINT "Block "; Block; "/"; TotalBlocks; "("; perc; "%)    "
  94.             LOOP
  95.             _DELAY 2
  96.             Writed_Size& = LOF(2)
  97.             confirm$ = MKL$(Writed_Size&) '                          client post WRITED SIZE as control back to Host
  98.             PUT #Client&, , confirm$
  99.             IF Writed_Size& = FileSize& THEN PRINT "Transfer OK!"
  100.         END IF
  101. CLOSE #2, #1, #Host&, #Client&
  102.  
  103.  
  104. FUNCTION Transfer$ (channel AS LONG, lenght)
  105.     SHARED prubeh
  106.     Transfer$ = ""
  107.     DO
  108.         GET #channel&, , T$
  109.         IF LEN(T$) THEN Transfer$ = Transfer$ + T$
  110.         '  IF _CONNECTED(channel&) = 0 THEN PRINT "Connection failure.": END
  111.     LOOP WHILE LEN(Transfer$) < lenght
  112.  
  113. FUNCTION Network (IP AS STRING)
  114.     Client& = _OPENCLIENT("TCP/IP:3455:" + LTRIM$(IP$))
  115.     IF Client& THEN
  116.         Network = 2 'client
  117.     ELSE
  118.         PRINT "No host found"
  119.         _DELAY 1
  120.         Client& = _OPENHOST("TCP/IP:3455")
  121.         IF Client& THEN
  122.             PRINT "Host created!"
  123.             DO
  124.                 i& = _KEYHIT
  125.                 IF i& = 27 THEN EXIT FUNCTION
  126.                 Host& = _OPENCONNECTION(Client&)
  127.             LOOP UNTIL Host&
  128.             Network = 1
  129.         END IF
  130.     END IF
  131.  

Function Transfer$ show you _CONNECTED using.

Offline Chris80194

  • Newbie
  • Posts: 42
    • View Profile
ill try it out thanks

Offline Chris80194

  • Newbie
  • Posts: 42
    • View Profile
Is that both the Client and Host?
line 10 looks like where I set either or but I am not sure what the context should be?

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Hi. Function NETWORK in my program automatically return what current computer is. 1 for host, 2 for client. Program so as is,  is writed for both sides.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Hi. Function NETWORK in my program automatically return what current computer is. 1 for host, 2 for client. Program so as is,  is writed for both sides.

What a coincidence. My NETWORK sub designates the host and client computers by numbers 1 and 2 as well; however, I use network mapping instead of tcp/ip to network my office software. I have never tried that on Win 10 but I was able to connect with Visa, XP, and Win 7. That helped, as I did not have to update all my office computers as I bought new ones. I found it necessary to use the LOCK statement to prevent multiple users from accessing the same files at the same time.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Craz1000

  • Forum Regular
  • Posts: 111
  • I'm OK
    • View Profile
    • Craz1000.net
any way to get these file transfer as a sub for example i have been trying for hours to no avail.

Call SendFile(File$) and Call GetFile(File$)

connection is already established as #Connection on server and client programs

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Craz1000: It works well for me. Did you run the program twice to simulate HOST and CLIENT on the same pc?


Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Maybe anti-virus software interfering? I vaguely recall having to disable ZoneAlarm or some sort of modification when I networked my office computers.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Craz1000

  • Forum Regular
  • Posts: 111
  • I'm OK
    • View Profile
    • Craz1000.net
Now that i am finally at my desk. I am trying to put together a library of TCP functions and subs to do various things to be $INCLUDEd in future programs. One of the things i want to add is a file transfer support. What I have on here was where I left off last night. I did copy the entire code that was posted and changed some things to get it to work this way, but nothing is transferred. At the point I left off I was grabbing at straws and the transfer code looks nothing like what was posted above at this point. But you can get the basic idea at what i am trying to do with this file.

Code: QB64: [Select]
  1. DIM SHARED BLOCK AS STRING * 4096
  2. DIM SHARED PACKET
  3. PACKET = 4096
  4.  
  5.  
  6.  
  7. SUB GETFILE (FILE$)
  8.     OPEN FILE$ FOR OUTPUT AS #2
  9.     CLOSE #2
  10.     OPEN FILE$ FOR BINARY AS #2
  11.     PRINT "Writing file..."
  12.  
  13.     CALL SENDMSG("READY")
  14.     DO
  15.         IF CONNECTION THEN
  16.             BLOCK$ = ""
  17.             GET #CONNECTION, , BLOCK$
  18.             PRINT BLOCK$
  19.             IF RTRIM$(BLOCK$) <> ":EOF" THEN PUT #2, , BLOCK$
  20.             CALL SENDMSG("NEXT")
  21.         END IF
  22.     LOOP UNTIL RTRIM$(BLOCK$) = ":EOF"
  23.     CLOSE #2
  24.     PRINT "TRANSFER COMPLETE"
  25.  
  26. SUB SENDFILE (FILE$)
  27.     IF _FILEEXISTS(FILE$) THEN
  28.         OPEN FILE$ FOR BINARY AS #1
  29.     ELSE
  30.         PRINT "File for transfer (source) not found"; FILE$
  31.         EXIT SUB
  32.     END IF
  33.  
  34.     IF WAITFORREPLY$ = "READY" THEN
  35.         PRINT "Uploading file..."
  36.  
  37.         DO
  38.             GET #1, , BLOCK$
  39.             PUT #CONNECTION, , BLOCK$
  40.             '    PRINT BLOCK$
  41.             DO
  42.             LOOP UNTIL WAITFORREPLY$ = "NEXT"
  43.             BLOCK$ = ""
  44.         LOOP UNTIL EOF(1)
  45.         CALL SENDMSG(":EOF")
  46.         PRINT "TRANSFER COMPLETE"
  47.         CLOSE #1
  48.     END IF
  49.  
  50. FUNCTION Transfer$ (LENGTH)
  51.     Transfer$ = ""
  52.     DO
  53.         GET #CONNECTION, , T$
  54.         Transfer$ = Transfer$ + T$
  55.     LOOP WHILE LEN(Transfer$) < LENGTH
  56.  
  57.  
  58. SUB SENDMSG (MSG$)
  59.     MSG$ = UCASE$(MSG$)
  60.     PUT #CONNECTION, , MSG$
  61.  
  62. FUNCTION WAITFORREPLYNUM
  63.     DO
  64.         GET #CONNECTION, , RPL
  65.     LOOP UNTIL RPL <> 0
  66.     WAITFORREPLYNUM = RPL
  67.  
  68. FUNCTION WAITFORREPLYRAW$
  69.     DO
  70.         GET #CONNECTION, , RPL$
  71.     LOOP UNTIL RPL$ <> ""
  72.     WAITFORREPLYRAW$ = RPL$
  73.  
  74.  
  75. FUNCTION WAITFORREPLY$
  76.     T! = TIMER
  77.     DO
  78.         GET #CONNECTION, , RPL$
  79.     LOOP UNTIL RPL$ <> "" OR TIMER > T! + 3
  80.  
  81.     IF TIMER > T! + 3 THEN
  82.         WAITFORREPLY$ = "TIMEOUT"
  83.     ELSE
  84.         WAITFORREPLY$ = RTRIM$(RPL$)
  85.     END IF
  86.  
« Last Edit: November 18, 2018, 12:00:42 pm by Craz1000 »

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
I noticed you did not DIM T$.
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Craz1000

  • Forum Regular
  • Posts: 111
  • I'm OK
    • View Profile
    • Craz1000.net
I got it working tried basing it on the code above again.

Code: QB64: [Select]
  1.  
  2.  
  3.  
  4. SUB GETFILE (FILE$)
  5.     PACKET = 4096
  6.     PRINT "Receiving info about file..."
  7.     SENDMSG ("INFO")
  8.     FILESIZE$ = LTRIM$(WAITFORREPLY$)
  9.     FILESIZE& = VAL(FILESIZE$)
  10.     PRINT "File in transfer: "; FILE$
  11.     PRINT "File size (bytes): "; FILESIZE&
  12.     SENDMSG ("OK")
  13.  
  14.     IF FILESIZE& < PACKET THEN
  15.         BLOCK$ = Transfer$(FILESIZE&)
  16.  
  17.         OPEN FILE$ FOR OUTPUT AS #2: CLOSE #2
  18.         OPEN RTRIM$(FILE$) FOR BINARY AS #2
  19.         PRINT "Writing file..."
  20.         PUT #2, , BLOCK$
  21.         WROTE& = LOF(2)
  22.         CLOSE #2
  23.         IF WROTE& = FILESIZE& THEN PRINT "Transfer OK!"
  24.     ELSE
  25.         OPEN FILE$ FOR OUTPUT AS #2: CLOSE #2
  26.         OPEN RTRIM$(FILE$) FOR BINARY AS #2
  27.         zbyva& = FILESIZE&
  28.  
  29.         DO WHILE zbyva& > 0
  30.             IF zbyva& > PACKET THEN BLOCK$ = Transfer$(PACKET) ELSE BLOCK$ = Transfer$(zbyva&)
  31.             zbyva& = zbyva& - PACKET
  32.             PUT #2, , BLOCK$
  33.             SENDMSG ("OK")
  34.             BLOCK$ = ""
  35.         LOOP
  36.         _DELAY 2
  37.         WROTE& = LOF(2)
  38.         CLOSE #2
  39.         IF WROTE& = FILESIZE& THEN PRINT "Transfer OK!"
  40.     END IF
  41.  
  42. SUB SENDFILE (FILE$)
  43.     PACKET = 4096
  44.     IF _FILEEXISTS(FILE$) THEN OPEN FILE$ FOR BINARY AS #1
  45.     PRINT "Sending File name and file size...("; FILE$; " and"; LOF(1); ")"
  46.     IF WAITFORREPLY$ = "INFO" THEN SENDMSG STR$((LOF(1)))
  47.  
  48.     _DELAY .005
  49.  
  50.     PRINT "Waiting for confirmation of the name and size ..."
  51.  
  52.     IF WAITFORREPLY$ = "OK" THEN
  53.         PRINT "Uploading file..."
  54.  
  55.         zbyva& = LOF(1)
  56.         DO WHILE zbyva& > 0
  57.             IF zbyva& > PACKET THEN Block$ = SPACE$(PACKET) ELSE Block$ = SPACE$(zbyva&)
  58.             zbyva& = zbyva& - PACKET
  59.             GET #1, , Block$ '
  60.             PUT #CONNECTION, , Block$
  61.             IF LOF(1) > PACKET THEN DO UNTIL WAITFORREPLY$ = "OK": LOOP
  62.         LOOP
  63.     END IF
  64.     CLOSE #1
  65.  
  66.  
  67. FUNCTION Transfer$ (LENGTH&)
  68.     Transfer$ = ""
  69.     DO
  70.         GET #CONNECTION, , T$
  71.         IF LEN(T$) THEN Transfer$ = Transfer$ + T$
  72.         '  IF _CONNECTED(channel&) = 0 THEN PRINT "Connection failure.": END
  73.     LOOP WHILE LEN(Transfer$) < LENGTH&
  74.  
  75.  
  76. SUB SENDMSG (MSG$)
  77.     MSG$ = UCASE$(MSG$)
  78.     PUT #CONNECTION, , MSG$
  79.  
  80.  
  81. FUNCTION WAITFORREPLY$
  82.     T! = TIMER
  83.     DO
  84.         GET #CONNECTION, , RPL$
  85.     LOOP UNTIL RPL$ <> "" OR TIMER > T! + 3
  86.  
  87.     IF TIMER > T! + 3 THEN
  88.         WAITFORREPLY$ = "TIMEOUT"
  89.     ELSE
  90.         WAITFORREPLY$ = RTRIM$(RPL$)
  91.     END IF
  92.  
  93.