Client file code
' 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$ = "192.168.3.208" 'comment out demo code
wconnect$ = "TCP/IP:" + port$ + ":" + ipnum$
'INPUT "Enter the name of a file to send: ", s$
's$ = "Mileage.csv" 'comment out demo code
't$ = "TripLog.csv"
S$ = "test2."
T$ = "test1."
PRINT "try to connect to Station " + wconnect$
PRINT "This Station IP is " + GetLocalIP$
PRINT "Send Files " + S$ + " " + T$
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!"
PRINT "Sending " + T$ + "..."
PRINT #client, T$
OPEN T$ 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 " + T$ + " sent successfully!"
FUNCTION GetLocalIP$
SHELL _HIDE "cmd /c ipconfig > IPconfig.tmp"
A = FREEFILE
OPEN "IPconfig.tmp" FOR INPUT AS #A
DO
LINE INPUT #A, ipline$
IF UCASE$(LEFT$(LTRIM$(ipline$), 4)) = "IPV4" THEN
GetLocalIP$ = MID$(ipline$, INSTR(ipline$, ":") + 2)
IF GetLocalIP$ = "127.0.0.1" THEN GOTO 77
CLOSE #A
KILL "IPconfig.tmp" 'kill the messenger?
EXIT DO
77
END IF
LOOP UNTIL EOF(1)
END FUNCTION
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.
' Host Computer: Receives the file
'INPUT "Enter the port number to listen on: ", port$
1
COLOR 7, 4
CLS
CLOSE
port$ = "12345" 'comment out demo code
v$ = "TCP/IP:" + port$
host = _OPENHOST(v$)
TTime$ = DATE$ + "_" + LEFT$(TIME$, 2) + "." + MID$(TIME$, 4, 2) + "." + RIGHT$(TIME$, 2) + ".TXT"
Path$ = "C:\Users\Chris\Downloads\Files"
Receive$ = Path$ + TTime$
Logging$ = Path$ + "IPLOG.txt"
LOCATE 10, 27: PRINT "Esc to shut down host."
IF host <> 0 THEN LOCATE 12, 20: PRINT GetLocalIP$ + ":" + port$; " You are the host."
LOCATE 11, 20: PRINT "Path to save to is " + Path$
DO
keyed$ = INKEY$
IF keyed$ = CHR$(27) THEN SYSTEM
newclient = _OPENCONNECTION(host)
IF newclient <> 0 THEN
PRINT "Another computer has connected! "
B = FREEFILE
OPEN Logging$ FOR APPEND AS #B
IP$ = _CONNECTIONADDRESS(newclient)
PRINT IP$ + " has joined." ' displayed to Host only
PRINT #B, IP$, TTime$ ' print info to a log file
CLOSE B
DO
INPUT #newclient, s$
LOOP UNTIL EOF(newclient) = 0
' DO
' INPUT #newclient, size&
' LOOP UNTIL EOF(newclient) = 0
PRINT "Downloading " + s$ + "... To " + Receive$
C = FREEFILE
PRINT "--------------------------------"
PRINT newclient
PRINT EOF(newclient)
PRINT s$
PRINT size&
PRINT "--------------------------------"
OPEN Receive$ + s$ FOR OUTPUT AS #C
filesize& = 0
DO
GET #newclient, , UT$
filesize& = filesize& + LEN(UT$)
IF LEN(t$) THEN PRINT #C, , UT$: PRINT ".";
_DELAY 0.01
PRINT filesize&, size&
' LOOP UNTIL filesize& = size&
LOOP
PRINT 'newline
CLOSE #B
CLOSE #newclient
PRINT s$ + Receive$ + s$ + " successfully!"
END IF
LOOP
DO
keyed$ = INKEY$
IF keyed$ = CHR$(27) THEN SYSTEM
newclient = _OPENCONNECTION(host)
IF newclient <> 0 THEN
PRINT "Another computer has connected! "
B = FREEFILE
OPEN Logging$ FOR APPEND AS #B
IP$ = _CONNECTIONADDRESS(newclient)
PRINT IP$ + " has joined." ' displayed to Host only
PRINT #B, IP$, TTime$ ' print info to a log file
CLOSE B
DO
INPUT #newclient, t$
LOOP UNTIL EOF(newclient) = 0
' DO
' INPUT #newclient, size&
' LOOP UNTIL EOF(newclient) = 0
PRINT "Downloading " + t$ + "... To " + Receive$
C = FREEFILE
PRINT "--------------------------------"
PRINT newclient
PRINT EOF(newclient)
PRINT t$
PRINT size&
PRINT "--------------------------------"
OPEN Receive$ + t$ FOR OUTPUT AS #C
filesize& = 0
DO
GET #newclient, , UT$
filesize& = filesize& + LEN(UT$)
IF LEN(t$) THEN PRINT #C, , UT$: PRINT ".";
_DELAY 0.01
PRINT filesize&, size&
' LOOP UNTIL filesize& = size&
LOOP
PRINT 'newline
CLOSE #B
CLOSE #newclient
PRINT t$ + Receive$ + t$ + " successfully!"
END IF
LOOP
GOTO 1
FUNCTION GetLocalIP$
SHELL _HIDE "cmd /c ipconfig > IPconfig.tmp"
A = FREEFILE
OPEN "IPconfig.tmp" FOR INPUT AS #A
DO
LINE INPUT #A, ipline$
IF UCASE$(LEFT$(LTRIM$(ipline$), 4)) = "IPV4" THEN
GetLocalIP$ = MID$(ipline$, INSTR(ipline$, ":") + 2)
IF GetLocalIP$ = "127.0.0.1" THEN GOTO 77
CLOSE #A
KILL "IPconfig.tmp" 'kill the messenger?
EXIT DO
77
END IF
LOOP UNTIL EOF(1)
END FUNCTION
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