Sto sviluppando un'applicazione in vb.net che trasferisca file > 1MB da un pc all'altro mediante la classe Imports System.Net.Sockets.
Il mio codice é il seguente :
clienteFile.SendBufferSize =1048633
Dim i As UInt16 = 0
Dim miofile() As Byte = ReadAllBytes(pathFile)
Dim info As FileInfo = My.Computer.FileSystem.GetFileInfo(pathFile)
Dim num_parti As UInt16 = Ceiling(info.Length / clienteFile.SendBufferSize)
Dim byteDaCopiare As UInt64 = clienteFile.SendBufferSize - 57
' se il file ha almeno una parte
For i = 0 To num_parti - 1
Dim frame(clienteFile.SendBufferSize - 1) As Byte
'Informazione che vanno messe in testa ad ogni pacchetto del file
Dim Dest() As Byte = BitConverter.GetBytes(CUShort(1))
Dim Mit() As Byte = BitConverter.GetBytes(CUShort(2))
Dim part() As Byte = BitConverter.GetBytes(CUShort(i))
Dim fine(1) As Byte
Dim nome() As Byte = UTF8.GetBytes(GetFileName(pathFile))
'
Array.Resize(File, 50)
'Inizio a mettere in parti file il le informazioni
Buffer.BlockCopy(Dest, 0, frame, 0, Dest.Length)
Buffer.BlockCopy(Mit, 0, frame, 2, Mit.Length)
Buffer.BlockCopy(part, 0, frame, 4, parte.Length)
Buffer.BlockCopy(nome, 0, frame, 7, nome.Length)
If (i = num_parti - 1) Then
byteDaCopiare = IIf(info.Length - i * (clienteFile.SendBufferSize - 57) = 0, clienteFile.SendBufferSize, info.Length - i * (clienteFile.SendBufferSize - 57))
fine = BitConverter.GetBytes(True)
Else
fine = BitConverter.GetBytes(False)
End If
Buffer.BlockCopy(fine, 0, frame, 6, fine.Length)
Buffer.BlockCopy(miofile, (i * byteDaCopiare), frame, 57, byteDaCopiare)
flussoFile.Write(frame, 0, frame.Length) -> al secondo giro si inchioda l'applicazione
Next
testato con un file di 3.154.976 byte
|