Computer File Sharing Demo
From QB64 Wiki
The following code by Galleon can be used to send files from one computer to another over an internet network connection.
Host Computer: Receives the file.
' Host Computer: Receives the file 'INPUT "Enter the port number to listen on: ", port$ port$ = "12345" 'comment out demo code v$ = "TCP/IP:" + port$ host = _OPENHOST(v$) IF host <> 0 THEN PRINT "You are the host." DO keyed$ = INKEY$ newclient = _OPENCONNECTION(host) IF newclient <> 0 THEN PRINT "Another computer has connected!" DO: INPUT #newclient, s$: LOOP UNTIL EOF(newclient) = 0 DO: INPUT #newclient, size&: LOOP UNTIL EOF(newclient) = 0 PRINT "Downloading " + s$ + "..." OPEN "download_" + s$ FOR OUTPUT AS #2: CLOSE #2 'clear file's contents OPEN "download_" + s$ FOR BINARY AS #2 filesize& = 0 DO GET #newclient, , t$ filesize& = filesize& + LEN(t$) IF LEN(t$) THEN PUT #2, , t$: PRINT "."; _DELAY 0.01 LOOP UNTIL filesize& = size& PRINT 'newline CLOSE #2 CLOSE #newclient PRINT s$ + " downloaded as download_" + s$ + " successfully!" END IF LOOP UNTIL keyed$ = CHR$(27)
Client computer: Sends the file
' Client computer: Sends the file 'INPUT "Enter a port number to broadcast on: ", port$ port$ = "12345" 'comment out demo code 'INPUT "Enter the IP number to connect to: ", ipnum$ ipnum$ = "localhost" 'comment out demo code wconnect$ = "TCP/IP:" + port$ + ":" + ipnum$ 'INPUT "Enter the name of a file to send: ", s$ s$ = "temp.mp3" 'comment out demo code client = _OPENCLIENT(wconnect$) IF client = 0 THEN PRINT "Could not connect!": END PRINT "Sending " + s$ + "..." PRINT #client, s$ OPEN s$ FOR BINARY AS #2 Size& = LOF(2) PRINT #client, Size& BufferSize& = 1024 BytesPerSecond& = 1048576 Buffer$ = SPACE$(BufferSize&) FOR o& = 1 TO Size& STEP BufferSize& IF o& + BufferSize& - 1 > Size& THEN Buffer$ = SPACE$(Size& - o& + 1) GET #2, , Buffer$ PUT #client, , Buffer$ _DELAY BufferSize& / BytesPerSecond& PRINT "."; NEXT PRINT 'new line CLOSE #2 CLOSE #client PRINT "File " + s$ + " sent successfully!"
See also:
- _OPENCLIENT
- _OPENHOST
- _OPENCONNECTION
- Email Demo, Inter-Program Data Sharing Demo
- IP Configuration, WGET (HTTP and FTP file transfer)