H
Hugh Janus
Hi group,
I am using a TCPStream together with a FileStream to send a file across
a network. Everything works fine except for one thing. Always, at the
end of the file there are several lines of white space. This is no
problem for text files but for binaries it is making them corrupt. I
only realised this when using checksums to verify that the copied file
is the same.
However, I cannot seem to work out where this additional data is coming
from.
Any ideas? See below my code. At a guess I am assuming that the when
EOF is reached additional info is written to the file causing the
checksum to be different.
Dim NWStream As NetworkStream = FTPClient.GetStream
Try
Dim BytesToSend(FTPClient.SendBufferSize) As Byte
Dim FileStream As New FileStream(strFile.FullName,
FileMode.Open, FileAccess.Read)
Dim FileReader As New BinaryReader(FileStream)
Dim NumBytesRead As Integer
Dim Limit As Integer
Dim Bytes As Integer
ProgressBar.Minimum = 0
ProgressBar.Maximum = 100
Do Until Limit = strFile.Length
NumBytesRead = FileStream.Read(BytesToSend, 0,
BytesToSend.Length)
NWStream.Write(BytesToSend, 0, NumBytesRead)
Limit += NumBytesRead
Bytes += NumBytesRead
ProgressBar.Value = (Bytes / strFile.Length * 100)
ProgressBar.Update()
ProgressPercent.Text = ProgressBar.Value & "%"
ProgressPercent.Update()
NWStream.Flush()
Loop
FileStream.Close()
FileReader.Close()
NWStream.Close()
FTPClient.Close()
Threading.Thread.Sleep(1000)
ProgressBar.Value = 0
ProgressPercent.Text = ProgressBar.Value & "%"
Catch ex As Exception
MessageBox.Show(ex.Message, Me.Text, MessageBoxButtons.OK)
End Try
Thanks!
I am using a TCPStream together with a FileStream to send a file across
a network. Everything works fine except for one thing. Always, at the
end of the file there are several lines of white space. This is no
problem for text files but for binaries it is making them corrupt. I
only realised this when using checksums to verify that the copied file
is the same.
However, I cannot seem to work out where this additional data is coming
from.
Any ideas? See below my code. At a guess I am assuming that the when
EOF is reached additional info is written to the file causing the
checksum to be different.
Dim NWStream As NetworkStream = FTPClient.GetStream
Try
Dim BytesToSend(FTPClient.SendBufferSize) As Byte
Dim FileStream As New FileStream(strFile.FullName,
FileMode.Open, FileAccess.Read)
Dim FileReader As New BinaryReader(FileStream)
Dim NumBytesRead As Integer
Dim Limit As Integer
Dim Bytes As Integer
ProgressBar.Minimum = 0
ProgressBar.Maximum = 100
Do Until Limit = strFile.Length
NumBytesRead = FileStream.Read(BytesToSend, 0,
BytesToSend.Length)
NWStream.Write(BytesToSend, 0, NumBytesRead)
Limit += NumBytesRead
Bytes += NumBytesRead
ProgressBar.Value = (Bytes / strFile.Length * 100)
ProgressBar.Update()
ProgressPercent.Text = ProgressBar.Value & "%"
ProgressPercent.Update()
NWStream.Flush()
Loop
FileStream.Close()
FileReader.Close()
NWStream.Close()
FTPClient.Close()
Threading.Thread.Sleep(1000)
ProgressBar.Value = 0
ProgressPercent.Text = ProgressBar.Value & "%"
Catch ex As Exception
MessageBox.Show(ex.Message, Me.Text, MessageBoxButtons.OK)
End Try
Thanks!