File Transfer Help Required

nigelr

Member
Licensed User
Hi,
Recently had the file transfer example working perfectly on mobile and pc so now I'm trying to tailor it as I want a device to send particular files to the pc as and when required.
In my client app, I have a connected() sub which opens a connection if available and a send_file(file_name) sub which uses the file-transfer code mostly as-is to send a particular file.
If I call the send_file sub once in app_start, it works as expected but if I call it twice (one after the other), it doesn't seem to be 'negotiating' the end of the first file and then the start of the next. It appears to be reading past the end of the file almost.:confused:
Can someone have a look at the code attached and see my simple mistake (hopefully) please? I suspect something to do with the default buffer size as the files I've been using for test are a lot less than 1kb in size.
Thanks in advance.

CLIENT CODE

If connected Then
send_file("file1.csv")
send_file("file2.csv")
send_file("file3.csv")
End If

Sub connected
ErrorLabel(connected_error)
lblStatus.Text = "Attempting Connection."
client.New1
client.Connect(cServer_ip,cServer_port)
lblStatus.Text = "Connection established."
Return True

connected_error:
Msgbox("No connection available","STOP")
Return False
End Sub

Sub send_file(file_name)
binary1.New1(client.GetStream,False)
binary1.WriteByte(0) 'Signal to the other computer that sending has started.
FileOpen(c1,file_name,cRandom)
fileStream.New1(c1,False)
size = fileStream.Length
lblStatus.Text = "Sending " & file_name & "..."
add_log("dev","Sending " & file_name & " size=" & size)
binary1.WriteInt64(size) 'Send the file size.
binary1.WriteString(file_name) 'Send the file name.
count = fileStream.ReadBytes(buffer(),8192) 'Read from the file stream.
Do While count > 0
binary1.WriteBytes2(buffer(),0,count) 'Write to the network stream.
size = size - count
count = fileStream.ReadBytes(buffer(),8192)
lblStatus.Text = size & " bytes left."
DoEvents
Loop
FileClose(c1)
DoEvents
add_log("dev","Transfer Complete")
lblStatus.Text="Transfer complete"
End Sub


SERVER CODE (from File Transfer Example Code)

Sub tmrWaitForData_Tick 'Checks the DataAvailable property
'If data is available then the other computer is sending a file.
If client.DataAvailable = True Then
tmrWaitForData.Enabled = False
binary1.New1(client.GetStream,False)
binary1.ReadByte 'Read the first byte
size = binary1.ReadInt64 'The size of the file.
If size = 0 Then 'The user pressed cancel.
tmrWaitForData.Enabled = True
Return
End If
name = binary1.ReadString 'The name of the file.
lblStatus.Text = "Receiving... " & name & " Size: " & size
If FileExist(name) = True Then FileDel(name)
FileOpen(c1,name,cRandom)
fileStream.New1(c1,False)
Do Until size = 0
count = binary1.ReadBytes(buffer(),8192) 'Read from the network stream.
fileStream.WriteBytes2(buffer(),0,count) 'Write to the file stream.
size = size - count
lblStatus.Text = "Receiving... " & name & " Bytes left: " & size
DoEvents 'Allow the label to update.
Loop
FileClose(c1)
lblStatus.Text = "File transfer completed."
lblStatus.Text = "Waiting for Connection..."
tmrWaitForData.Enabled = True
End If
End Sub
 

nigelr

Member
Licensed User
Solved it (Simple oversight)

Well.. it was a simple problem of reading more bytes into the buffer than the size of the first file. Just added the following modification on the server side..

If size<cBuffer Then
count = binary1.ReadBytes(buffer(),size) 'Read from the network stream.
Else
count = binary1.ReadBytes(buffer(),cBuffer) 'Read from the network stream.
End If

Maybe useful to someone else - hope so, then I won't feel so much like a newbie!

Cheers :)
 
Top