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

0 Members and 1 Guest are viewing this topic.

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
I would try it, but the whole program is needed, not just a part of it. I will try to write a new program that will differ from the current one by adding a control section for network traffic. It often happens in the network that some data will be lost, so it is necessary to count on it. This is the difference between LOCALHOST mode and real network mode.
The future program will work as follows:


1) HOST sends the file name of the uploaded file, for example, the file name has 20 characters.
2) The HOST waits for the CLIENT response to get 20 characters. But he is watching the time. If the answer does not arrive, for example, in a second, HOST sends the data again until the client confirms the correct size, after time limit CLIENT reset all incoming datas to zero and wait for new transfer.
3) HOST will send the filename (whether it is in the form of ASC numbers or a string directly, CLIENT waits until it has 20 values, then CLIENT sends the received string back to the guest and waits for confirmation. If this is not done correctly  in the specified time, CLIENT resets the string to empty and HOST sends it again and waits for confirmation until this is not correctly transferred, and then proceeds:
4) Send the data SIZE for the transfer
5) Waiting for size confirmation if no match, resetting value and sending again
6) a packet and a predetermined size are sent.
7) if it does not arrive or does not reach the full size in tim limit for one packet, and the total size of the received data is less than the data value sent in point 4, this packet is reset and sent again.

It was because I was writing a game with LAN support. It went 5 times, but then, sometimes it stuck, and it ended with endless waiting for data that, although sent, was lost somewhere along the way. The error was that I did not realize this option at that time, and did not watch the time and did not send the specific data again.

Offline Chris80194

  • Newbie
  • Posts: 42
    • View Profile
Ok I see this works on the LOCALHOST now how would I break this up like my example so that I could run this from 2 (or more) computers to transfer a file across a network.

Ill try to break this up.

I am trying to put this in to a program.

The receiving end just needs to be a listener.

Offline Chris80194

  • Newbie
  • Posts: 42
    • View Profile
With lots of help from FellippeHeitor and his rewrite of my transfer code  I was able to send a file across a network between 2 computers (the goal) now I am adding fluff, so some automation and a few menu choices and I am stuck with the loops.
The HOST side is pretty well set mostly but the client side is where I am having the hang up.
You need both sides.

Host.bas
Code: QB64: [Select]
  1. 'host - waits for connection and receives file
  2.  
  3. endMarker$ = CHR$(26) + "<END>" 'CHR$(26) + "<END>" is arbitraty. You can choose another ending marker.
  4. port = 1234
  5. host = _OPENHOST("TCP/IP:" + STR$(port))
  6. IF host = 0 THEN
  7.     PRINT "Opening port "; port; " failed."
  8.     END
  9. 1
  10. COLOR 15, 6: CLS
  11. LOCATE 6, 1: PRINT "Host started. Waiting for a client on port "; port; "..."
  12. LOCATE 13, 1: PRINT "Thanks for the help from FellippeHeitor who contributed the majority of the file transfer code"
  13. LOCATE 25, 1: PRINT "Press the <esc> to stop"
  14.  
  15.     LOCATE 4, 15: PRINT TIME$; " "; DATE$
  16.     IF INKEY$ = CHR$(27) THEN 999
  17.     client = _OPENCONNECTION(host)
  18.     IF client THEN
  19.         'a client connected; get data until EOF() then close
  20.         LOCATE 6, 1: COLOR 15, 8: PRINT "waiting for data... ";
  21.         receivedFile$ = ""
  22.         DO
  23.             GET client, , incoming$
  24.             receivedFile$ = receivedFile$ + incoming$
  25.         LOOP UNTIL INSTR(receivedFile$, endMarker$) > 0
  26.         CLOSE client
  27.         client = 0
  28.         fileName$ = LEFT$(receivedFile$, INSTR(receivedFile$, "<CONTENTS>") - 1)
  29.         PRINT fileName$
  30.         receivedFile$ = LEFT$(receivedFile$, INSTR(receivedFile$, endMarker$) - 1)
  31.         LOCATE 7, 1: COLOR 15, 8: PRINT LEN(receivedFile$); " bytes received."
  32.         'IF fileName$ = "" THEN INPUT "File name to save: ", fileName$
  33.  
  34.         IF LEN(fileName$) THEN
  35.             fileHandle = FREEFILE
  36.             OPEN fileName$ FOR BINARY AS #fileHandle
  37.             PUT #fileHandle, 1, receivedFile$
  38.             CLOSE #fileHandle
  39.             LOCATE 8, 1: COLOR 15, 8: PRINT "Saved as "; fileName$; TIME$; DATE$
  40.         ELSE
  41.             LOCATE 8, 1: COLOR 15, 8: PRINT "canceled; received data discarded."; TIME$; DATE$
  42.         END IF
  43.         LOCATE 5, 1: COLOR 15, 8: PRINT "Waiting for a new client...": COLOR 15, 6: GOTO 1
  44.     END IF
  45.     _LIMIT 30
  46. 999
  47. LOCATE 10, 1: COLOR 7, 1: PRINT "closing connections..."
  48.  

Client.bas
Code: QB64: [Select]
  1. 1
  2. 'host - waits for connection and receives file
  3. COLOR 6, 1: CLS
  4. endMarker$ = CHR$(26) + "<END>" 'CHR$(26) + "<END>" is arbitraty. You can choose another ending marker.
  5. port = 1234
  6. PRINT "Press (L) for localhost"
  7. PRINT "Press (I) to enter Host IP"
  8.     Choice$ = UCASE$(INKEY$)
  9.     IF Choice$ <> "" THEN EXIT DO
  10.     T = T + 1
  11.     IF T = 400000 THEN Choice$ = "A": EXIT DO
  12. IF Choice$ = "A" THEN address$ = "192.168.3.208": GOTO 2
  13. IF Choice$ = "L" THEN address$ = "localhost": GOTO 2
  14. IF Choice$ = "I" THEN INPUT "What is the HOST IP ", address$: GOTO 2
  15. 2
  16. COLOR 6, 1: CLS
  17. PRINT "Looking for host "; address$; ":"; port
  18. LOCATE 18, 1: PRINT "Thanks for the help from FellippeHeitor who contributed the majority of the file transfer code"
  19. host = _OPENCLIENT("TCP/IP:" + STR$(port) + ":" + address$)
  20. IF host = 0 THEN
  21.     PRINT "No host found at "; address$; " on port "; port;
  22.     END
  23.  
  24. LOCATE 2, 1: PRINT "Host found."
  25. LOCATE 5, 1
  26. PRINT "1 Transfer Mileage.log"
  27. PRINT "2 Transfer Receipt.log"
  28. 'PRINT "3"
  29. PRINT "9 Transfer Any File"
  30. PRINT "<esc> to end"
  31. 9
  32.     LOCATE 3, 15: PRINT TIME$; " "; DATE$
  33.     Choice$ = INKEY$
  34.     IF Choice$ <> "" THEN GOTO 10
  35.  
  36. 10
  37. IF Choice$ = "1" THEN fileName$ = "Mileage.log": GOTO 11
  38. IF Choice$ = "2" THEN fileName$ = "Receipt.log": GOTO 11
  39. 'if Choice$="3"  : GOTO 11
  40. IF Choice$ = "9" THEN LOCATE 10, 1: INPUT "Name of File to send: ", fileName$: GOTO 11
  41. IF Choice$ = CHR$(27) GOTO 54
  42.  
  43. 11
  44. LOCATE 12, 1: COLOR 15, 8: PRINT INKEY$
  45. PRINT "File to send "; fileName$
  46.  
  47. IF _FILEEXISTS(fileName$) = 0 THEN
  48.     LOCATE 12, 1:
  49.     PRINT "File not found: "; fileName$
  50.     PRINT "closing connections...": CLOSE
  51.     PRINT "Press any key to continue to menu"
  52.     DO WHILE INKEY$ <> "": LOOP 'Pause
  53.     GOTO 1
  54.     'END
  55.  
  56. 'read file into memory:
  57. fileHandle = FREEFILE
  58. OPEN fileName$ FOR BINARY AS #fileHandle
  59. fileContents$ = SPACE$(LOF(fileHandle))
  60. GET #fileHandle, 1, fileContents$
  61. CLOSE #fileHandle
  62.  
  63. 'send file:
  64. fileContents$ = fileName$ + "<CONTENTS>" + fileContents$ + endMarker$
  65. 'fileContents$ = fileContents$ + endMarker$
  66. PUT #host, , fileContents$
  67. LOCATE 12, 1:
  68. PRINT LEN(fileContents$) - LEN(endMarker$); " bytes sent"
  69. PRINT "Tranfer complete,"
  70. PRINT "closing connections...": CLOSE
  71. PRINT "Press any key to continue to menu"
  72. DO WHILE INKEY$ <> "": LOOP 'Pause
  73.  
  74. 54
  75. LOCATE 12, 1:
  76. PRINT "closing connections..."
  77.  

I am trying to do loop instead of input this way I can put in a revolving clock while it waits for input or will fail/timeout to a defined selection.
Thanks
« Last Edit: January 11, 2019, 01:25:47 am by Chris80194 »

Offline Chris80194

  • Newbie
  • Posts: 42
    • View Profile
Ok so i have beat up this code a lot.
I have 2 programs that are to run on different computers on a lan.

Thispart resides on my computer in my truck and seems to work.
Where the issue is in the transfer of the files.
Code: QB64: [Select]
  1. 'COLOR Foreground , Background
  2. 'Path$ = "C:\Users\Chris\Downloads\Files"
  3. 'Receive$ = Path$ + TTime$
  4. 'LOCATE 11, 27: PRINT Receive$
  5. 222
  6. COLOR 1, 7
  7. position = 3
  8. DO UNTIL choice$ <> ""
  9.     TTime$ = DATE$ + "_" + LEFT$(TIME$, 2) + "." + MID$(TIME$, 4, 2) + "." + RIGHT$(TIME$, 2)
  10.     LOCATE 3, 15: PRINT "OTS Mileage Log": LOCATE 3, 40: PRINT TTime$ ' only 23 lines on screen 80 wide
  11.  
  12.     LOCATE 4 + position, 1: PRINT "1 Gas Mileage"
  13.     LOCATE 5 + position, 1: PRINT "2 Trip Log"
  14.     LOCATE 10 + position, 1: PRINT "0 Upload"
  15.     LOCATE 23, 1: PRINT "ESC to exit" ' Lock in here no lower
  16.     choice$ = UCASE$(INKEY$) 'change any user key press to uppercase
  17.  
  18.  
  19. IF choice$ = "1" THEN CALL Mileagelog
  20. IF choice$ = "2" THEN CALL Triplog
  21. IF choice$ = "0" THEN CALL Uploader
  22. IF choice$ = CHR$(27) THEN SYSTEM
  23. choice$ = ""
  24. GOTO 222
  25.  
  26. SUB Mileagelog
  27.     CLS
  28.     choice$ = ""
  29.     COLOR 15, 4: CLS
  30.     LOCATE 1, 15: PRINT "OTS Mileage Log"
  31.  
  32.     LOCATE 2, 1: PRINT " 1 Kroger"
  33.     LOCATE 3, 1: PRINT " 2 7 Eleven"
  34.     LOCATE 4, 1: PRINT " 3 Mobile"
  35.     LOCATE 5, 1: PRINT " 4 Exxon"
  36.     LOCATE 6, 1: PRINT " 5 Shell"
  37.     LOCATE 7, 1: PRINT " 6 Costco"
  38.     LOCATE 8, 1: PRINT " 7 Sams"
  39.     LOCATE 9, 1: PRINT " 8 Tom Thumb"
  40.     LOCATE 10, 1: PRINT " 0 Other"
  41.     LOCATE 20, 1: PRINT "ESC to exit"
  42.  
  43.     10
  44.     DO
  45.  
  46.         LOCATE 12, 1: PRINT "Choose a Station above: "; 'semicolon saves position for user entry
  47.  
  48.         DO: keypress$ = UCASE$(INKEY$) 'change any user key press to uppercase
  49.  
  50.         LOOP UNTIL keypress$ <> ""
  51.         COLOR 15, 1: PRINT keypress$
  52.         Station$ = keypress$
  53.         GOTO 11
  54.  
  55.     LOOP
  56.  
  57.     11
  58.     IF Station$ = "1" THEN Station$ = "Kroger": GOTO 12
  59.     IF Station$ = "2" THEN Station$ = "7 Eleven": GOTO 12
  60.     IF Station$ = "3" THEN Station$ = "Mobile": GOTO 12
  61.     IF Station$ = "4" THEN Station$ = "Exxon": GOTO 12
  62.     IF Station$ = "5" THEN Station$ = "Shell": GOTO 12
  63.     IF Station$ = "6" THEN Station$ = "Costco": GOTO 12
  64.     IF Station$ = "7" THEN Station$ = "Sams": GOTO 12
  65.     IF Station$ = "8" THEN Station$ = "Tom Thumb": GOTO 12
  66.     IF Station$ = "0" THEN Station$ = "Other": GOTO 12
  67.     IF Station$ = CHR$(27) THEN GOTO 31
  68.  
  69.     GOTO 10
  70.  
  71.     12
  72.     IF keypress$ = "0" THEN LOCATE 2, 20: INPUT ; "Station ?", Station$
  73.  
  74.     200
  75.     LOCATE 2, 20: PRINT "Station ", Station$
  76.     LOCATE 3, 20: PRINT "Location"
  77.     LOCATE 4, 20: PRINT "Pump"
  78.     LOCATE 5, 20: PRINT "Price"
  79.     LOCATE 6, 20: PRINT "Cost"
  80.     LOCATE 7, 20: PRINT "Discount"
  81.     LOCATE 8, 20: PRINT "Gallons"
  82.     LOCATE 9, 20: PRINT "Mileage"
  83.     LOCATE 10, 20: PRINT "Trip"
  84.     LOCATE 11, 20: PRINT "Hours"
  85.     LOCATE 3, 20: INPUT ; "Location? ", Location$
  86.     LOCATE 4, 20: INPUT ; "Pump? ", Pump$
  87.     LOCATE 5, 20: INPUT ; "Price? ", Price$
  88.     LOCATE 6, 20: INPUT ; "Cost? ", Cost$
  89.     LOCATE 7, 20: INPUT ; "Discount? ", Discount$
  90.     LOCATE 8, 20: INPUT ; "Gallons? ", Gallons$
  91.     LOCATE 9, 20: INPUT ; "Mileage? ", Mileage$
  92.     LOCATE 10, 20: INPUT ; "Trip? ", Trip$
  93.     LOCATE 11, 20: INPUT ; "Hours? ", Hours$
  94.     LOCATE 12, 30: INPUT ; "Do you need to enter the date and time ? (N/y)", choice$
  95.     LOCATE 12, 30: COLOR 7, 0: PRINT ; "                                              ": COLOR 15, 1
  96.     IF LCASE$(choice$) = "y" THEN LOCATE 12, 20: INPUT ; "Time? ", TM$: LOCATE 13, 20: INPUT ; "Date? ", DT$ ELSE TM$ = TIME$: DT$ = DATE$
  97.     IF TM$ = "" THEN TM$ = "Not Entered": IF DT$ = "" THEN DT$ = "Not Entered"
  98.  
  99.     300
  100.     LOCATE 15, 1: PRINT Station$, Location$, Pump$, Price$, Discount$, Gallons$, Mileage$, Trip$, Hours$, DT$, TM$
  101.     OPEN "mileage.csv" FOR APPEND AS #1
  102.     WRITE #1, Station$, Location$, Pump$, Price$, Discount$, Gallons$, Mileage$, Trip$, Hours$, DT$, TM$
  103.     CLOSE #1
  104.    31
  105.  
  106. FUNCTION GetLocalIP$
  107.     choice$ = ""
  108.     SHELL _HIDE "cmd /c ipconfig > IPconfig.tmp"
  109.     A = FREEFILE
  110.     OPEN "IPconfig.tmp" FOR INPUT AS #A
  111.     DO
  112.         LINE INPUT #A, ipline$
  113.         IF UCASE$(LEFT$(LTRIM$(ipline$), 4)) = "IPV4" THEN
  114.             GetLocalIP$ = MID$(ipline$, INSTR(ipline$, ":") + 2)
  115.             IF GetLocalIP$ = "127.0.0.1" THEN GOTO 77
  116.             CLOSE #A
  117.             KILL "IPconfig.tmp" 'kill the messenger?
  118.             EXIT DO
  119.            77
  120.         END IF
  121.     LOOP UNTIL EOF(1)
  122.     CLOSE #A
  123.     KILL "IPconfig.tmp" 'kill the messenger?
  124.  
  125. SUB Triplog
  126.     COLOR 15, 5
  127.     CLS
  128.     choice$ = ""
  129.    1000
  130.     COLOR 7, 0: CLS
  131.     LOCATE 1, 15: PRINT "OTS Trip Log" ' only 23 lines on screen 80 wide
  132.  
  133.     1200
  134.     LOCATE 3, 20: PRINT "Work Order Number"
  135.     LOCATE 4, 20: PRINT "Start Mileage"
  136.     LOCATE 5, 20: PRINT "End Mileage"
  137.     LOCATE 6, 20: PRINT "Client"
  138.     LOCATE 3, 20: INPUT ; "Work Order Number? ", WO$
  139.     LOCATE 4, 20: INPUT ; "Start Mileage ", SM$
  140.     LOCATE 5, 20: INPUT ; "End Mileage? ", EM$
  141.     LOCATE 6, 20: INPUT ; "Client? ", Client$
  142.  
  143.     TM$ = TIME$: DT$ = DATE$
  144.  
  145.     1300
  146.     OPEN "TripLog.csv" FOR APPEND AS #1
  147.     LOCATE 15, 1: PRINT WO$, SM$, EM$, Client$, DT$, TM$
  148.     WRITE #1, WO$, SM$, EM$, Client$, DT$, TM$
  149.     CLOSE #1
  150.  
  151.  
  152. SUB Uploader
  153.    1
  154.     choice$ = ""
  155.     t = 0
  156.     'host - waits for connection and receives file
  157.     COLOR 15, 1: CLS
  158.     endMarker$ = CHR$(26) + "<END>" 'CHR$(26) + "<END>" is arbitraty. You can choose another ending marker.
  159.     address$ = "192.168.3.102": port = 1234
  160.     LOCATE 8
  161.     PRINT "Auto Host " + address$; ":"; port; "(Default)"
  162.     PRINT "Press (L) for localhost"
  163.     PRINT "Press (I) to enter Host IP"
  164.     LOCATE 20, 1: PRINT "ESC to exit"
  165.  
  166.     DO WHILE INKEY$ = ""
  167.         choice$ = UCASE$(INKEY$)
  168.         IF choice$ <> "" THEN EXIT DO
  169.         t = t + 1
  170.         IF t = 600000 THEN choice$ = "A": EXIT DO
  171.     LOOP
  172.     IF choice$ = "A" THEN GOTO 2
  173.     IF choice$ = "L" THEN address$ = "localhost": port = 1234: GOTO 2
  174.     IF choice$ = "I" THEN INPUT "What is the HOST IP ", address$
  175.     IF choice$ = "I" THEN INPUT "What is the PORT # ", port: GOTO 2
  176.     IF choice$ = CHR$(27) GOTO 54
  177.     GOTO 1
  178.    2
  179.     choice$ = ""
  180.     COLOR 12, 1: CLS
  181.     PRINT "Sending to host "; address$; ":"; port
  182.     LOCATE 18, 1: PRINT "Thanks for the help from FellippeHeitor who contributed the majority of the file transfer code"
  183.     host = _OPENCLIENT("TCP/IP:" + STR$(port) + ":" + address$)
  184.     IF host = 0 THEN
  185.         PRINT "No host found at "; address$; " on port "; port;
  186.         END
  187.     END IF
  188.  
  189.     LOCATE 2, 1: PRINT "Host found."
  190.     LOCATE 5, 1
  191.     PRINT "1 Transfer Mileage.csv"
  192.     PRINT "2 Transfer Triplog.csv"
  193.     'PRINT "3"
  194.     PRINT "9 Transfer Any File"
  195.     PRINT "<esc> to end"
  196.  
  197.     9
  198.     t = 0
  199.     PRINT choice$
  200.     DO WHILE INKEY$ = ""
  201.         choice$ = UCASE$(INKEY$)
  202.         t = t + 1
  203.         'IF t = 400000 THEN choice$ = "3": EXIT DO
  204.         IF choice$ <> "" THEN EXIT DO
  205.         LOCATE 3, 15: PRINT TIME$; " "; DATE$
  206.     LOOP
  207.  
  208.     PRINT t; choice$
  209.     IF choice$ = "1" THEN fileName$ = "Mileage.csv": GOTO 11
  210.     IF choice$ = "2" THEN fileName$ = "Triplog.csv": GOTO 11
  211.     'if Choice$="3"  : GOTO 11 '  send all
  212.     IF choice$ = "9" THEN LOCATE 10, 1: INPUT "Name of File to send: ", fileName$: GOTO 11
  213.     IF choice$ = CHR$(27) GOTO 54
  214.     choice$ = ""
  215.     GOTO 9
  216.  
  217.     11
  218.     choice$ = ""
  219.     LOCATE 12, 1: COLOR 15, 8: PRINT INKEY$
  220.     PRINT "File to send "; fileName$
  221.  
  222.     IF _FILEEXISTS(fileName$) = 0 THEN
  223.         LOCATE 12, 1:
  224.         PRINT "File not found: "; fileName$
  225.         PRINT "closing connections...": CLOSE
  226.         PRINT "Press any key to continue to menu"
  227.         DO WHILE INKEY$ <> "": LOOP 'Pause
  228.         GOTO 1
  229.         'END
  230.     END IF
  231.  
  232.     'read file into memory:
  233.     fileHandle = FREEFILE
  234.     OPEN fileName$ FOR BINARY AS #fileHandle
  235.     fileContents$ = SPACE$(LOF(fileHandle))
  236.     GET #fileHandle, 1, fileContents$
  237.     CLOSE #fileHandle
  238.  
  239.     'send file:
  240.     fileContents$ = fileName$ + "<CONTENTS>" + fileContents$ + endMarker$
  241.     'fileContents$ = fileContents$ + endMarker$
  242.     PUT #host, , fileContents$
  243.     LOCATE 12, 1:
  244.     PRINT LEN(fileContents$) - LEN(endMarker$); " bytes sent"
  245.     PRINT "Transfer complete,"
  246.     PRINT "closing connections...": CLOSE
  247.     PRINT "Press any key to continue to menu"
  248.     DO WHILE INKEY$ <> "": LOOP 'Pause
  249.     GOTO 1
  250.  
  251.     54
  252.     LOCATE 12, 1:
  253.     PRINT "closing connections..."
  254.     CLOSE
  255.  
  256.  
  257.  

This side waits for the transfer and when done is supposed to recycle and wait for another file to receive without having to be restarted.
It gets to waiting for file and hangs up.???
This part is supposed to run on a computer on my desk in my room standalone without being touched..

Code: QB64: [Select]
  1. 'host - waits for connection and receives file
  2.  
  3. endMarker$ = CHR$(26) + "<END>" 'CHR$(26) + "<END>" is arbitrary. You can choose another ending marker.
  4. port = 1234
  5. host = _OPENHOST("TCP/IP:" + STR$(port))
  6. IF host = 0 THEN
  7.     PRINT "Opening port "; port; " failed."
  8.     END
  9. 1
  10. COLOR 15, 6: CLS
  11. LOCATE 6, 1: PRINT "Host started. Waiting for a client on port "; port; "..."
  12. LOCATE 13, 1: PRINT "Thanks for the help from FellippeHeitor who contributed the majority of the file transfer code"
  13. LOCATE 25, 1: PRINT "Press the <esc> to stop"
  14.  
  15.     LOCATE 4, 15: PRINT TIME$; " "; DATE$
  16.     IF INKEY$ = CHR$(27) THEN 999
  17.     client = _OPENCONNECTION(host)
  18.     IF client THEN
  19.         'a client connected; get data until EOF() then close
  20.         LOCATE 6, 1: COLOR 15, 8: PRINT "waiting for data... ";
  21.         receivedFile$ = ""
  22.         DO
  23.             GET client, , incoming$
  24.             receivedFile$ = receivedFile$ + incoming$
  25.         LOOP UNTIL INSTR(receivedFile$, endMarker$) > 0
  26.         CLOSE client
  27.         client = 0
  28.         fileName$ = LEFT$(receivedFile$, INSTR(receivedFile$, "<CONTENTS>") - 1)
  29.         PRINT fileName$
  30.         receivedFile$ = LEFT$(receivedFile$, INSTR(receivedFile$, endMarker$) - 1)
  31.         LOCATE 7, 1: COLOR 15, 8: PRINT LEN(receivedFile$); " bytes received."
  32.         'IF fileName$ = "" THEN INPUT "File name to save: ", fileName$
  33.  
  34.         IF LEN(fileName$) THEN
  35.             fileHandle = FREEFILE
  36.             OPEN fileName$ FOR BINARY AS #fileHandle
  37.             PUT #fileHandle, 1, receivedFile$
  38.             CLOSE #fileHandle
  39.             LOCATE 8, 1: COLOR 15, 8: PRINT "Saved as "; fileName$; TIME$; DATE$
  40.         ELSE
  41.             LOCATE 8, 1: COLOR 15, 8: PRINT "canceled; received data discarded."; TIME$; DATE$
  42.         END IF
  43.         LOCATE 5, 1: COLOR 15, 8: PRINT "Waiting for a new client...": COLOR 15, 6: GOTO 1
  44.     END IF
  45.     _LIMIT 30
  46. 999
  47. LOCATE 10, 1: COLOR 7, 1: PRINT "closing connections..."
  48.  
  49.  

This is supposed to run on 2 seperate systems.

Offline Chris80194

  • Newbie
  • Posts: 42
    • View Profile
Coder Hunter helped point a few things with the transfer so I updated the code
Logger.bas

Code: QB64: [Select]
  1. 'COLOR Foreground , Background
  2. 'Path$ = "C:\Users\Chris\Downloads\Files"
  3. 'Receive$ = Path$ + TTime$
  4. 'LOCATE 11, 27: PRINT Receive$
  5. 222
  6. COLOR 1, 7
  7. position = 3
  8. DO UNTIL choice$ <> ""
  9.     TTime$ = DATE$ + "_" + LEFT$(TIME$, 2) + "." + MID$(TIME$, 4, 2) + "." + RIGHT$(TIME$, 2)
  10.     LOCATE 3, 15: PRINT "OTS Mileage Log": LOCATE 3, 40: PRINT TTime$ ' only 23 lines on screen 80 wide
  11.  
  12.     LOCATE 4 + position, 1: PRINT "1 Gas Mileage"
  13.     LOCATE 5 + position, 1: PRINT "2 Trip Log"
  14.     LOCATE 10 + position, 1: PRINT "0 Upload"
  15.     LOCATE 11 + position, 1: PRINT "P Printer Select"
  16.     LOCATE 23, 1: PRINT "ESC to exit" ' Lock in here no lower
  17.     choice$ = UCASE$(INKEY$) 'change any user key press to uppercase
  18.  
  19.  
  20. IF choice$ = "1" THEN CALL Mileagelog
  21. IF choice$ = "2" THEN CALL Triplog
  22. IF choice$ = "0" THEN CALL Uploader
  23. IF choice$ = "P" THEN CALL Select_Printer
  24. IF choice$ = CHR$(27) THEN SYSTEM
  25. choice$ = ""
  26. GOTO 222
  27.  
  28. SUB Mileagelog
  29.     CLS
  30.     choice$ = ""
  31.     COLOR 15, 4: CLS
  32.     LOCATE 1, 15: PRINT "OTS Mileage Log"
  33.  
  34.     LOCATE 2, 1: PRINT " 1 Kroger"
  35.     LOCATE 3, 1: PRINT " 2 7 Eleven"
  36.     LOCATE 4, 1: PRINT " 3 Mobile"
  37.     LOCATE 5, 1: PRINT " 4 Exxon"
  38.     LOCATE 6, 1: PRINT " 5 Shell"
  39.     LOCATE 7, 1: PRINT " 6 Costco"
  40.     LOCATE 8, 1: PRINT " 7 Sams"
  41.     LOCATE 9, 1: PRINT " 8 Tom Thumb"
  42.     LOCATE 10, 1: PRINT " 0 Other"
  43.     LOCATE 20, 1: PRINT "ESC to exit"
  44.  
  45.     10
  46.     DO
  47.  
  48.         LOCATE 12, 1: PRINT "Choose a Station above: "; 'semicolon saves position for user entry
  49.  
  50.         DO: keypress$ = UCASE$(INKEY$) 'change any user key press to uppercase
  51.  
  52.         LOOP UNTIL keypress$ <> ""
  53.         COLOR 15, 1: PRINT keypress$
  54.         Station$ = keypress$
  55.         GOTO 11
  56.  
  57.     LOOP
  58.  
  59.     11
  60.     IF Station$ = "1" THEN Station$ = "Kroger": GOTO 12
  61.     IF Station$ = "2" THEN Station$ = "7 Eleven": GOTO 12
  62.     IF Station$ = "3" THEN Station$ = "Mobile": GOTO 12
  63.     IF Station$ = "4" THEN Station$ = "Exxon": GOTO 12
  64.     IF Station$ = "5" THEN Station$ = "Shell": GOTO 12
  65.     IF Station$ = "6" THEN Station$ = "Costco": GOTO 12
  66.     IF Station$ = "7" THEN Station$ = "Sams": GOTO 12
  67.     IF Station$ = "8" THEN Station$ = "Tom Thumb": GOTO 12
  68.     IF Station$ = "0" THEN Station$ = "Other": GOTO 12
  69.     IF Station$ = CHR$(27) THEN GOTO 31
  70.  
  71.     GOTO 10
  72.  
  73.     12
  74.     IF keypress$ = "0" THEN LOCATE 2, 20: INPUT ; "Station ?", Station$
  75.  
  76.     200
  77.     LOCATE 2, 20: PRINT "Station ", Station$
  78.     LOCATE 3, 20: PRINT "Location"
  79.     LOCATE 4, 20: PRINT "Pump"
  80.     LOCATE 5, 20: PRINT "Price"
  81.     LOCATE 6, 20: PRINT "Cost"
  82.     LOCATE 7, 20: PRINT "Discount"
  83.     LOCATE 8, 20: PRINT "Gallons"
  84.     LOCATE 9, 20: PRINT "Mileage"
  85.     LOCATE 10, 20: PRINT "Trip"
  86.     LOCATE 11, 20: PRINT "Hours"
  87.     LOCATE 3, 20: INPUT ; "Location? ", Location$
  88.     LOCATE 4, 20: INPUT ; "Pump? ", Pump$
  89.     LOCATE 5, 20: INPUT ; "Price? ", Price$
  90.     LOCATE 6, 20: INPUT ; "Cost? ", Cost$
  91.     LOCATE 7, 20: INPUT ; "Discount? ", Discount$
  92.     LOCATE 8, 20: INPUT ; "Gallons? ", Gallons$
  93.     LOCATE 9, 20: INPUT ; "Mileage? ", Mileage$
  94.     LOCATE 10, 20: INPUT ; "Trip? ", Trip$
  95.     LOCATE 11, 20: INPUT ; "Hours? ", Hours$
  96.     LOCATE 12, 30: INPUT ; "Do you need to enter the date and time ? (N/y)", choice$
  97.     LOCATE 12, 30: COLOR 7, 0: PRINT ; "                                              ": COLOR 15, 1
  98.     IF LCASE$(choice$) = "y" THEN LOCATE 12, 20: INPUT ; "Time? ", TM$: LOCATE 13, 20: INPUT ; "Date? ", DT$ ELSE TM$ = TIME$: DT$ = DATE$
  99.     IF TM$ = "" THEN TM$ = "Not Entered": IF DT$ = "" THEN DT$ = "Not Entered"
  100.  
  101.     300
  102.     LOCATE 15, 1: PRINT Station$, Location$, Pump$, Price$, Discount$, Gallons$, Mileage$, Trip$, Hours$, DT$, TM$
  103.     OPEN "mileage.csv" FOR APPEND AS #1
  104.     WRITE #1, Station$, Location$, Pump$, Price$, Discount$, Gallons$, Mileage$, Trip$, Hours$, DT$, TM$
  105.     CLOSE #1
  106.    31
  107.  
  108. FUNCTION GetLocalIP$
  109.     choice$ = ""
  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.     CLOSE #A
  125.     KILL "IPconfig.tmp" 'kill the messenger?
  126.  
  127. SUB Triplog
  128.     COLOR 15, 5
  129.     CLS
  130.     choice$ = ""
  131.    1000
  132.     COLOR 7, 0: CLS
  133.     LOCATE 1, 15: PRINT "OTS Trip Log" ' only 23 lines on screen 80 wide
  134.  
  135.     1200
  136.     LOCATE 3, 20: PRINT "Work Order Number"
  137.     LOCATE 4, 20: PRINT "Start Mileage"
  138.     LOCATE 5, 20: PRINT "End Mileage"
  139.     LOCATE 6, 20: PRINT "Client"
  140.     LOCATE 3, 20: INPUT ; "Work Order Number? ", WO$
  141.     LOCATE 4, 20: INPUT ; "Start Mileage ", SM$
  142.     LOCATE 5, 20: INPUT ; "End Mileage? ", EM$
  143.     LOCATE 6, 20: INPUT ; "Client? ", Client$
  144.  
  145.     TM$ = TIME$: DT$ = DATE$
  146.  
  147.     1300
  148.     OPEN "TripLog.csv" FOR APPEND AS #1
  149.     LOCATE 15, 1: PRINT WO$, SM$, EM$, Client$, DT$, TM$
  150.     WRITE #1, WO$, SM$, EM$, Client$, DT$, TM$
  151.     CLOSE #1
  152.  
  153.  
  154. SUB Uploader
  155.    1
  156.     choice$ = ""
  157.     t = 0
  158.     'host - waits for connection and receives file
  159.     COLOR 15, 1: CLS
  160.     endMarker$ = CHR$(26) + "<END>" 'CHR$(26) + "<END>" is arbitraty. You can choose another ending marker.
  161.     address$ = "192.168.3.102": port = 1234
  162.     LOCATE 8
  163.     PRINT "Auto Host " + address$; ":"; port; "(Default)"
  164.     PRINT "Press (L) for localhost"
  165.     PRINT "Press (I) to enter Host IP"
  166.     LOCATE 20, 1: PRINT "ESC to exit"
  167.  
  168.     DO WHILE INKEY$ = ""
  169.         choice$ = UCASE$(INKEY$)
  170.         IF choice$ <> "" THEN EXIT DO
  171.         t = t + 1
  172.         IF t = 600000 THEN choice$ = "A": EXIT DO
  173.     LOOP
  174.     IF choice$ = "A" THEN GOTO 2
  175.     IF choice$ = "L" THEN address$ = "localhost": port = 1234: GOTO 2
  176.     IF choice$ = "I" THEN INPUT "What is the HOST IP ", address$
  177.     IF choice$ = "I" THEN INPUT "What is the PORT # ", port: GOTO 2
  178.     IF choice$ = CHR$(27) GOTO 54
  179.     GOTO 1
  180.    2
  181.     choice$ = ""
  182.     COLOR 15, 1: CLS
  183.     LOCATE 9,
  184.     PRINT "Host to be sent to "; address$; ":"; port
  185.     PRINT "1 Transfer Mileage.csv"
  186.     PRINT "2 Transfer Triplog.csv"
  187.     'PRINT "3"
  188.     PRINT "9 Transfer Any File"
  189.     PRINT "<esc> to end"
  190.  
  191.     LOCATE 18, 1: PRINT "Thanks for the help from FellippeHeitor who contributed the majority of the file transfer code"
  192.     LOCATE 19, 1: PRINT "Thanks, also for the help from Coder_Hunter who contributed tips to the file transfer code"
  193.  
  194.     9
  195.     t = 0
  196.     DO WHILE INKEY$ = ""
  197.         choice$ = UCASE$(INKEY$)
  198.         t = t + 1
  199.         'IF t = 400000 THEN choice$ = "3": EXIT DO
  200.         IF choice$ <> "" THEN EXIT DO
  201.         LOCATE 3, 15: PRINT TIME$; " "; DATE$
  202.     LOOP
  203.  
  204.     IF choice$ = "1" THEN fileName$ = "Mileage.csv": GOTO 11
  205.     IF choice$ = "2" THEN fileName$ = "Triplog.csv": GOTO 11
  206.     'if Choice$="3"  : GOTO 11 '  send all
  207.     IF choice$ = "9" THEN LOCATE 6, 1: INPUT "Name of File to send: ", fileName$: GOTO 11
  208.     IF choice$ = CHR$(27) GOTO 54
  209.     choice$ = ""
  210.     GOTO 9
  211.  
  212.     11
  213.     choice$ = ""
  214.  
  215.     LOCATE 12, 1: COLOR 15, 8: CLS
  216.     PRINT "File to send "; fileName$
  217.  
  218.     IF _FILEEXISTS(fileName$) = 0 THEN
  219.         LOCATE 13, 1
  220.         PRINT "File not found: "; fileName$
  221.         PRINT "closing connections...": CLOSE
  222.         PRINT "Press any key to continue to menu"
  223.         choice$ = ""
  224.         DO WHILE INKEY$ = ""
  225.             choice$ = UCASE$(INKEY$)
  226.             t = t + 1
  227.             IF t = 400000 THEN choice$ = "3": EXIT DO
  228.             IF choice$ <> "" THEN EXIT DO
  229.             LOCATE 3, 15: PRINT TIME$; " "; DATE$
  230.         LOOP
  231.         GOTO 1
  232.     END IF
  233.     CLS
  234.     host = _OPENCLIENT("TCP/IP:" + STR$(port) + ":" + address$)
  235.     IF host = 0 THEN
  236.         LOCATE 6, 1: PRINT "No host found at "; address$; " on port "; port;
  237.         END
  238.     END IF
  239.  
  240.     LOCATE 6, 1: PRINT "Host found at "; address$; " on port "; port;
  241.  
  242.     'read file into memory:
  243.     fileHandle = FREEFILE
  244.     OPEN fileName$ FOR BINARY AS #fileHandle
  245.     fileContents$ = SPACE$(LOF(fileHandle))
  246.     GET #fileHandle, 1, fileContents$
  247.     CLOSE #fileHandle
  248.  
  249.     'send file:
  250.     fileContents$ = fileName$ + "<CONTENTS>" + fileContents$ + endMarker$
  251.     'fileContents$ = fileContents$ + endMarker$
  252.     PUT #host, , fileContents$
  253.     LOCATE 13, 1
  254.     PRINT fileName$ + ", Transfered"
  255.     PRINT LEN(fileContents$) - LEN(endMarker$); " bytes sent"
  256.     PRINT "Tranfer complete,"
  257.     PRINT "closing connections...": CLOSE
  258.     PRINT "Press any key to continue to menu"
  259.     choice$ = ""
  260.     DO WHILE INKEY$ = ""
  261.         choice$ = UCASE$(INKEY$)
  262.         t = t + 1
  263.         IF t = 400000 THEN choice$ = "3": EXIT DO
  264.         IF choice$ <> "" THEN EXIT DO
  265.         LOCATE 3, 15: PRINT TIME$; " "; DATE$
  266.     LOOP
  267.     GOTO 1
  268.  
  269.     54
  270.     LOCATE 17, 1:
  271.     PRINT "closing connections..."
  272.     CLOSE
  273.  
  274.  
  275. SUB Select_Printer
  276.     CLS
  277.     COLOR 15, 0
  278.     SHELL _HIDE "cmd/c wmic printer get name, default> impresora.txt"
  279.     DIM impresora(50) AS STRING
  280.     OPEN "I", 10, "impresora.txt"
  281.     n% = 0
  282.     LINE INPUT #10, dato$
  283.     DO
  284.         LINE INPUT #10, dato$: LINE INPUT #10, dato$
  285.         IF EOF(10) THEN EXIT DO
  286.         n% = n% + 1
  287.         DO
  288.             a% = INSTR(dato$, CHR$(0))
  289.             IF a% = 0 THEN EXIT DO
  290.             IF a% = 1 THEN
  291.                 dato$ = MID$(dato$, a% + 1)
  292.             ELSEIF a% = LEN(dato$) THEN
  293.                 dato$ = LEFT$(dato$, a% - 1)
  294.             ELSE
  295.                 dato$ = LEFT$(dato$, a% - 1) + MID$(dato$, a% + 1)
  296.             END IF
  297.         LOOP
  298.         a% = INSTR(dato$, "FALSE"): b% = INSTR(dato$, "TRUE")
  299.         IF a% <> 0 THEN
  300.             MID$(dato$, a%, 5) = SPACE$(5)
  301.             dato$ = LTRIM$(RTRIM$(dato$))
  302.         END IF
  303.         IF b% <> 0 THEN
  304.             MID$(dato$, b%, 5) = SPACE$(4)
  305.             dato$ = LTRIM$(RTRIM$(dato$))
  306.             loca% = n%
  307.         END IF
  308.         impresora(n%) = dato$
  309.         impresora(0) = LTRIM$(STR$(n%))
  310.     LOOP
  311.     COLOR 7, 0
  312.     CLS
  313.     COLOR 1, 7
  314.     LOCATE 8, 20: PRINT "ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»"
  315.     LOCATE 9, 20: PRINT "º                                        º"
  316.     LOCATE 10, 20: PRINT "ÌÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ͹"
  317.     LOCATE 11, 20: PRINT "º                                        º"
  318.     LOCATE 12, 20: PRINT "º                                        º"
  319.     LOCATE 13, 20: PRINT "º                                        º"
  320.     LOCATE 14, 20: PRINT "º                                        º"
  321.     LOCATE 15, 20: PRINT "º                                        º"
  322.     LOCATE 16, 20: PRINT "ÈÍÍÍÍÍÍÍ<Esc>ÍÍÍÍ<"; CHR$(24); ">Í<"; CHR$(25); ">ÍÍÍÍ<"; CHR$(17); CHR$(217); ">ÍÍÍÍÍÍÍÍͼ"
  323.     COLOR 15, 1
  324.     LOCATE 9, 21: PRINT "            SELECT PRINTER              ";
  325.     COLOR 1, 7
  326.     max% = VAL(impresora(0))
  327.     GOSUB impridatos
  328.     DO
  329.         x$ = "": WHILE x$ = "": x$ = INKEY$: WEND
  330.         x$ = UCASE$(x$)
  331.         x% = ASC(RIGHT$(x$, 1))
  332.         SELECT CASE x%
  333.             CASE IS = 27
  334.                 a$ = "@"
  335.                 EXIT DO
  336.             CASE IS = 13
  337.                 a$ = impresora(loca%)
  338.                 EXIT DO
  339.             CASE IS = 72: '--Subir
  340.                 IF loca% > 1 THEN
  341.                     loca% = loca% - 1
  342.                     GOSUB impridatos
  343.                 END IF
  344.             CASE IS = 80: '--Bajar
  345.                 IF loca% < max% THEN
  346.                     loca% = loca% + 1
  347.                     GOSUB impridatos
  348.                 END IF
  349.         END SELECT
  350.     LOOP
  351.     COLOR 7, 0
  352.     CLS
  353.     IF a$ <> "@" THEN
  354.         SHELL _HIDE "cmd/c wmic printer where name='" + a$ + "' call setdefaultprinter"
  355.         PRINT "Selected printer: "; a$
  356.     ELSE
  357.         PRINT "The printer has not been modified"
  358.     END IF
  359.     END
  360.  
  361.     impridatos:
  362.     y% = 1
  363.     FOR n% = loca% - 2 TO loca% + 2
  364.         IF n% < 1 OR n% > max% THEN
  365.             COLOR 1, 7: LOCATE y% + 10, 21: PRINT "                                        "
  366.         ELSE
  367.             IF n% = loca% THEN COLOR 15, 4 ELSE COLOR 1, 7
  368.             LOCATE y% + 10, 21: PRINT "                                        "
  369.             LOCATE y% + 10, 21: PRINT LEFT$(impresora(n%), 40)
  370.         END IF
  371.         y% = y% + 1
  372.     NEXT
  373.     RETURN
  374.  
  375.  
  376.  

Host.bas is the auto capture end that runs on another system with out much attention.

Code: QB64: [Select]
  1. 'host - waits for connection and receives file
  2.  
  3. endMarker$ = CHR$(26) + "<END>" 'CHR$(26) + "<END>" is arbitraty. You can choose another ending marker.
  4. port = 1234
  5. host = _OPENHOST("TCP/IP:" + STR$(port))
  6. IF host = 0 THEN
  7.     PRINT "Opening port "; port; " failed."
  8.     END
  9. 1
  10. COLOR 15, 6: CLS
  11. LOCATE 6, 1: PRINT "Host started. Waiting for a client on port "; port; "..."
  12. LOCATE 13, 1: PRINT "Thanks for the help from FellippeHeitor who contributed the majority of the file transfer code"
  13. LOCATE 13, 1: PRINT "Thanks also for the help from Coder_Hunter who has helped iwht bits of the transfer code section also"
  14. LOCATE 25, 1: PRINT "Press the <esc> to stop"
  15.  
  16.     LOCATE 4, 15: PRINT TIME$; " "; DATE$
  17.     IF INKEY$ = CHR$(27) THEN EXIT DO
  18.     client = _OPENCONNECTION(host)
  19.     IF client THEN
  20.         'a client connected; get data until EOF() then close
  21.         LOCATE 6, 1: COLOR 15, 8: PRINT "waiting for data... ";
  22.         receivedFile$ = ""
  23.         DO
  24.             IF INKEY$ = CHR$(27) THEN EXIT DO
  25.             countloop = countloop + 1: IF countloop = 10000 THEN PRINT "Exiting due to excessive loops": EXIT DO
  26.             GET client, , incoming$
  27.             receivedFile$ = receivedFile$ + incoming$
  28.             'LOOP UNTIL INSTR(incoming$, endMarker$) > 0
  29.         LOOP UNTIL RIGHT$(incoming$, 6) = endMarker$
  30.         CLOSE client
  31.         client = 0
  32.         fileName$ = LEFT$(receivedFile$, INSTR(receivedFile$, "<CONTENTS>") - 1)
  33.         PRINT fileName$
  34.         receivedFile$ = LEFT$(receivedFile$, INSTR(receivedFile$, endMarker$) - 1)
  35.         LOCATE 7, 1: COLOR 15, 8: PRINT LEN(receivedFile$); " bytes received."
  36.         'IF fileName$ = "" THEN INPUT "File name to save: ", fileName$
  37.  
  38.         IF LEN(fileName$) THEN
  39.             fileHandle = FREEFILE
  40.             OPEN fileName$ FOR BINARY AS #fileHandle
  41.             PUT #fileHandle, 1, receivedFile$
  42.             CLOSE #fileHandle
  43.             LOCATE 8, 1: COLOR 15, 8: PRINT "Saved as "; fileName$; TIME$; DATE$
  44.         ELSE
  45.             LOCATE 8, 1: COLOR 15, 8: PRINT "canceled; received data discarded."; TIME$; DATE$
  46.         END IF
  47.         LOCATE 5, 1: COLOR 15, 8: PRINT "Waiting for a new client...": COLOR 15, 6: GOTO 1
  48.     END IF
  49.     _LIMIT 30
  50. 999
  51. LOCATE 10, 1: COLOR 7, 1: PRINT "closing connections..."
  52.  

Seems to have fixed the transfer and added a few default loops to break a long pause.

Logger still seems to have issues with keypress choices for file selection.
Selecting might work on the first press or after 3 or 4.
It is getting better,
The next piece will combine the new file with a master file.