Salve, ho dei grossissimi problemi di trasmissione-ricezione file, i codici sono questi:
codice:
Private Sub FileThread()
If DEMO = False And BloccoLan = False Then
Dim PercorsoPrivato As String = PercorsoFileTX
Dim IpPrivatoDestrinatario As String = IpDestinatarioFile
Dim DimMaxBuffer As Integer
Thread.Sleep(Rnd() * 10)
Dim pingResult As Boolean = False
Dim FileAperto = False
Dim Reader As FileStream = Nothing
Do
If TxFileBusy = False Then
Exit Do
End If
Loop
Do While FileAperto = False
Try
TxFileBusy = True
Reader = New IO.FileStream(PercorsoPrivato, FileMode.Open)
FileAperto = True
Catch ex As Exception
FileAperto = False
TxFileBusy = False
End Try
Loop
If FileAperto = True Then
Try
pingResult = My.Computer.Network.Ping(IpPrivatoDestrinatario)
Catch
pingResult = False
End Try
Dim Sizez As Int64 = FileLen(PercorsoFileTX)
If pingResult = True Then
Try
Dim NetFile As NetworkStream
Dim Client As New TcpClient(IpPrivatoDestrinatario, 8003)
NetFile = Client.GetStream
If Client.Connected = True Then
Dim ByteFile(4096) As Byte
If Sizez > 4096 Then
For Block As Int64 = 0 To Sizez Step 4096
'Invoke(New StatoRXTX(AddressOf Me.RXTXSTATO), "TX File..")
If Sizez - Block >= 4096 Then
Reader.Read(ByteFile, 0, 4096)
DimMaxBuffer = 4096
Else
Reader.Read(ByteFile, 0, Sizez - Block)
DimMaxBuffer = Sizez - Block
End If
Try
NetFile.Write(ByteFile, 0, DimMaxBuffer)
Sizez = Sizez
Catch ex As Exception
ex = ex
Exit For
End Try
Next
End If
Else
pingResult = pingResult
End If
Client.Close()
Catch ex As Exception
ex = ex
End Try
End If
Reader.Close()
'Invoke(New StatoRXTX(AddressOf Me.RXTXSTATO), "END TX File..")
TxFileBusy = False
End If
End If
End Sub
la trasmissione è gestita in un thread separato, il codice della ricezione è:
codice:
Public Sub LeggidatiFile() 'SOCKET 8003 Ricezione File
If DEMO = False And BloccoLan = False Then
'Dim sok As TcpClient = Me.SocketFile
Dim netstream As NetworkStream = SocketFile.GetStream
Dim percorso As String = PersorsoFileRX
Dim Stream As FileStream = Nothing
Dim FileAperto As Boolean = False
Dim Indice As Int64 = 0
Dim bytes(4096) As Byte
Do While FileAperto = False
If TxFileBusy = False Then
Try
Stream = New IO.FileStream(percorso, IO.FileMode.Create)
FileAperto = True
Catch ex As Exception
FileAperto = False
Exit Do
End Try
End If
Loop
If FileAperto = True Then
Do
'If SocketFile.Available Then
If netstream.DataAvailable Then
TtXRrrX.Text = "RX File.."
Dim N As Integer = netstream.Read(bytes, 0, 4096)
Stream.Write(bytes, 0, N)
Indice += N
Thread.Sleep(25)
Else
'SocketFile.Close()
If DimensioneFileRicervere <> Indice Then
DimensioneFileRicervere = 0
TxLanToMittente("801#" & NomeFileRX & "#" & Comunicazione.MioIP, IpMittenteFile)
End If
SocketFile.Close()
Stream.Close()
FineRicezioneFile = True
'If DimensioneFileRicervere <> Indice Then
'TxLanToMittente("801#" & PersorsoFileRX & "#" & Comunicazione.MioIP, IpMittenteFile)
'eventuale test sulla dimensione del file ricevuto, problemi di loop non definito dove!!!!
'End If
PersorsoFileRX = ""
percorso = ""
DimensioneFileRicervere = 0
TtXRrrX.Text = "END RX File.."
Exit Do
End If
Loop
End If
End If
End Sub
il problema macroscopico è che il file ricevuto o non è completo o è a dimensione 0, raramente è completo! apparentemente il file viene trasmesso tutto e completamente, ogni tanto da problemi di disconnessione del computer host!
sto davvero impazzendo e il sistema è tutt'altro che stabile!
Scusate dimenticavo, i file sono di dimensione variabile, da pochi byte a qualche Mb. sopratutto file xml. I flussi sono tra pc, tx-rx in base ad handshake esterni.
spero che qualcuno possa essermi d'aiuto
grazie Simone