QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: Chris80194 on November 12, 2018, 07:58:56 pm

Title: Help with a Simple file transfer from a client to the host to send 2 text files.
Post by: Chris80194 on November 12, 2018, 07:58:56 pm
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
Title: Re: Help with a Simple file transfer from a client to the host to send 2 text files.
Post by: Petr on November 13, 2018, 07:02:29 am
I think, you search IF _CONNECTED (connectionhandle&)? I have a TCP / IP transmission program somewhere, but I have to find it first
Title: Re: Help with a Simple file transfer from a client to the host to send 2 text files.
Post by: FellippeHeitor on November 13, 2018, 11:11:01 am
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.
Title: Re: Help with a Simple file transfer from a client to the host to send 2 text files.
Post by: Chris80194 on November 14, 2018, 12:14:01 am
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.
Title: Re: Help with a Simple file transfer from a client to the host to send 2 text files.
Post by: Petr on November 14, 2018, 10:42:02 am
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.
Title: Re: Help with a Simple file transfer from a client to the host to send 2 text files.
Post by: Chris80194 on November 15, 2018, 04:02:26 am
ill try it out thanks
Title: Re: Help with a Simple file transfer from a client to the host to send 2 text files.
Post by: Chris80194 on November 17, 2018, 06:54:15 am
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?
Title: Re: Help with a Simple file transfer from a client to the host to send 2 text files.
Post by: Petr on November 17, 2018, 10:07:25 am
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.
Title: Re: Help with a Simple file transfer from a client to the host to send 2 text files.
Post by: Pete on November 17, 2018, 11:21:53 pm
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
Title: Re: Help with a Simple file transfer from a client to the host to send 2 text files.
Post by: Craz1000 on November 18, 2018, 02:34:21 am
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
Title: Re: Help with a Simple file transfer from a client to the host to send 2 text files.
Post by: Petr on November 18, 2018, 02:50:42 am
Craz1000: It works well for me. Did you run the program twice to simulate HOST and CLIENT on the same pc?

Title: Re: Help with a Simple file transfer from a client to the host to send 2 text files.
Post by: Pete on November 18, 2018, 10:51:11 am
Maybe anti-virus software interfering? I vaguely recall having to disable ZoneAlarm or some sort of modification when I networked my office computers.

Pete
Title: Re: Help with a Simple file transfer from a client to the host to send 2 text files.
Post by: Craz1000 on November 18, 2018, 11:55:47 am
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.  
Title: Re: Help with a Simple file transfer from a client to the host to send 2 text files.
Post by: Pete on November 18, 2018, 12:21:35 pm
I noticed you did not DIM T$.
Title: Re: Help with a Simple file transfer from a client to the host to send 2 text files.
Post by: Craz1000 on November 18, 2018, 01:25:06 pm
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.  
Title: Re: Help with a Simple file transfer from a client to the host to send 2 text files.
Post by: Petr on November 19, 2018, 11:12:11 am
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.
Title: Re: Help with a Simple file transfer from a client to the host to send 2 text files.
Post by: Chris80194 on December 02, 2018, 10:53:32 am
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.
Title: Re: Help with a Simple file transfer from a client to the host to send 2 text files.
Post by: Chris80194 on December 10, 2018, 10:00:24 pm
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
Title: Re: Help with a Simple file transfer from a client to the host to send 2 text files.
Post by: Chris80194 on January 10, 2019, 10:10:11 pm
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.
Title: Re: Help with a Simple file transfer from a client to the host to send 2 text files.
Post by: Chris80194 on January 12, 2019, 12:46:21 am
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.