Hi
I have an application for chat and file transfer between two devices using wifi.
The chat (using short strings as messages) works in both cases - initialize and initializeprefix, but the file transfer works only with initialize.
The problem looks as if the data of the file itself (after the name and size are recieved) is not reaching the newdata sub.
here is the sending part:
and here is the recieving part:
Sorry for the Hebrew part - it is consent or reject of the file by the reciever.
Edit - forgot to mention that the timers are init with this:
Can anyone tell me why it doesn't work with prefix - does not enter " case 3" ?
Thanks.
I have an application for chat and file transfer between two devices using wifi.
The chat (using short strings as messages) works in both cases - initialize and initializeprefix, but the file transfer works only with initialize.
The problem looks as if the data of the file itself (after the name and size are recieved) is not reaching the newdata sub.
here is the sending part:
B4X:
Sub send_string(st As String)
Dim buffer() As Byte
buffer = st.GetBytes("UTF8")
AStreams.Write(buffer)
End Sub
Sub send_file(dir As String)
Dim buffer(8192) As Byte
print("Sending " & fname, False)
raf.Initialize( dir, fname,True)
size = raf.Size ' file size
total = size
Mult = 100 /total
buffer = bc.StringToBytes( fname ,"UTF8") ' name
AStreams.Write(buffer)
Dim buffer(8) As Byte
raftmp.Initialize3(buffer,True)
raftmp.WriteLong(size,0) ' size
AStreams.Write(buffer)
raftmp.Close
bar.Progress = 0
sendtimer.enabled = True
End Sub
Sub sendtimer_tick
Dim buffer(8192) As Byte
Log("send size= " & size)
If size <= 0 Then
raf.Close
sendtimer.Enabled = False
Else
count = raf.ReadBytes(buffer, 0, Min(8192,size), position)
Astreams.Write(buffer)
size = size - count
position = position + count
bar.Progress = (total-size)* mult
End If
End Sub
and here is the recieving part:
B4X:
Sub AStreams_NewData (Buffer() As Byte)
Dim msg As String
Dim result As Int
Select msgtype
Case 0
msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
msg = msg.Replace(Chr(19),"")
Select msg
Case "$ready$"
readyflag = True
If readyflag AND fileselectedflag Then
send_file(directory )
End If
Case "$send$"
result = Msgbox2("האם אתה מאשר קבלת קובץ ? " ,"אישור העברת קובץ","כן", "ביטול","לא",Null)
If result = DialogResponse.POSITIVE Then
send_string("$ready$")
msgtype = 1
Else
send_string("$notready$")
End If
Case "$notready$"
readyflag = False
fileselectedflag = False
Print( "העברת הקובץ נדחתה" , False)
Case Else
Print(msg, True)
End Select
Case 1
fname = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
fname = fname.Replace(Chr(19),"")
print("Recieving " & fname,False)
raf.Initialize(File.DirRootExternal & "/recieved" , fname, False)
msgtype = 2
Case 2
raftmp.Initialize3(buffer,True)
sizeR = raftmp.ReadLong(0)
raftmp.close
totalR = SizeR
positionR = 0
multR = 100 /totalR
bar.Progress = 0
msgtype = 3
Case 3
rcvtimer.Enabled = False
count = buffer.Length
Log("size= " & sizeR & " count= " & count)
raf.WriteBytes(buffer, 0, count, positionR)
SizeR = SizeR - count
positionR = positionR + count
bar.Progress = (totalR-sizeR)* multR
If sizeR <= 0 Then
raf.Close
msgtype = 0
send_string(fname & " Recieved")
Else
rcvtimer.Enabled = True
End If
End Select
End Sub
Sub rcvtimer_tick
If sizeR <= 0 Then
raf.Close
msgtype = 0
send_string(fname & " Recieved")
rcvtimer.Enabled = False
Else
lbl(lcount-1).Text = "Waiting for data"
End If
End Sub
Edit - forgot to mention that the timers are init with this:
B4X:
sendtimer.Initialize("sendtimer",300)
sendtimer.Enabled = False
rcvtimer.initialize("rcvtimer",10000)
rcvtimer.Enabled = False
Thanks.
Last edited: