This is how I did it, I'm positive there are easier ways. Here's the send function (some of the
commands are my own functions. I am not claiming this is easy to understand, I am just posting it or reference:
` *************************************************************************
function sendFile(file$, networkIndex, recipientIndex)
` *** Sends a file to a connected network recipient
` *** The recipient must respond to the request
` *** recipient is the index of the client
` *************************************************************************
netId = getNetworkId(networkIndex)
clientId = arrClient[recipientIndex].id
` *** Sanity Checks
if isNetworkActive(netId) = 0
displayMessageOver("Tried to send a file, network Unavailable", 0, 2)
exitfunction -1
endif
if getFileExists(file$) = 0
displayMessageOver("Tried to send a file that does not exist", 0, 2)
exitfunction -1
endif
`*** Check Client is available
sts = netClientState(recipientIndex)
if sts < 1
displayMessageOver("Tried to send a file, client Unavailable", 0, 2)
exitfunction -1
endif
`*** Open and send file
f = openToRead(file$)
s = getFileSize(f)
dim i[1024]
count = 0
packets = 0
rmn = mod(s,4)
for n = 1 to s step 4
count = count + 1
i[count] = readInteger(f)
if n > s - 4 then eof = 1
if count = 256 or eof = 1
msg = createNetworkMessage()
addNetworkMessageFloat(msg,cNET_SENDFILE)
addNetworkMessageString(msg,file$)
addNetworkMessageInteger(msg,count)
for x = 1 to count
addNetworkMessageInteger(msg,i[x])
next x
`*** Finish the message with a 0 for more to come, or 1 for complete
if eof <> 1
addNetworkMessageInteger(msg,0)
else
addNetworkMessageInteger(msg,1)
endif
sendnetworkmessage(netId, clientId, msg)
packets = packets + 1
count = 0
endif
next n
closeFile(f)
displayMessageOver("File Sent",0,2)
debugWriteSave("File Sent : " + str(packets) + " packets : " + str(timer()))
endfunction 1
And here is the receive function:
` *************************************************************************
function receiveFile(file$, msg)
` *** Receives file from another client/host
` *** Message is already received when calling this function
` *************************************************************************
g.time = timer()
f = OpenToWrite(file$,0)
`Loop through receiving more data until file complete
packet = 1
do
count = getNetworkMessageInteger(msg)
for n = 1 to count
writeInteger(f,getNetworkMessageInteger(msg))
next n
` Get flag to say whether file complete
ret = getNetworkMessageInteger(msg)
deleteNetworkMessage(msg)
if ret = 0
msg = 0
`*** Wait for next chunk of file
while msg = 0
msg = getNetworkMessage(gBingo.networkId)
if msg > 0
null = getNetworkMessageInteger(msg)
file$ = getNetworkMessageString(msg)
packet = packet + 1
endif
endwhile
else
debugwriteSave("File closed : " + str(timer() - g.time) + "Seconds")
closeFile(f)
sleep(100)
exit
endif
loop
g.time = timer()
endfunction