file transfer query

  • Thread starter Thread starter Adrian
  • Start date Start date
A

Adrian

Hello,
I am having a problem with a binary transfer using streams from a
mobile device to a desktop PC. When I transfer a JPG image it becomes
slightly corrupted in the transfer. I have included the client and server
code in case anyone can see what I am doing wrong. Also is there an easier
way to do this as this method does seem more complictaed than needed.

Thanks

Client

Dim FileDialog As New OpenFileDialog
Dim fsRead As FileStream
Dim fsWrite As FileStream
Dim bRead As BinaryReader
Dim b As Byte
Dim bWrite As BinaryWriter

Dim filestreamin As FileStream
Dim bytesread As Integer
Dim byteswrote As Integer
Dim itestwrite As Integer
Dim position As Integer
Dim itestread As Integer
Dim buffersize As Integer
Dim sendbytes(1024) As Byte
Dim count As Integer
Dim filesize(32) As Byte
Dim bytestoread As Integer = 1024
FileDialog.ShowDialog()

If FileDialog.ShowDialog() = DialogResult.OK Then
MsgBox(FileDialog.FileName())
'connectto the server
tcpclient.Connect("194.66.170.218", 8000)
'open the file for reading
' filestreamin = New FileStream(FileDialog.FileName(),
FileMode.Open, FileAccess.Read)
networkstream = tcpclient.GetStream()
End If
'check if read file exists
fsRead = New FileStream(FileDialog.FileName(), FileMode.Open,
FileAccess.Read)
'create a fileStream instance to pass to BinaryWriter object
' fsWrite = New FileStream("temp.bmp", FileMode.CreateNew,
FileAccess.Write)
filesize = Encoding.ASCII.GetBytes(CStr(fsRead.Length).PadRight(32,
""))
'transfer the file size to the server
networkstream.Write(filesize, 0, 32)
bRead = New BinaryReader(fsRead)
'set the file pointer to the start of the file
bRead.BaseStream.Seek(0, SeekOrigin.Begin)
'loop until can no longer read bytes from file
count = 0
Do While True
Try
'read next byte and advance reader
b = bRead.ReadByte
Catch
Exit Do
End Try
'store the byte
sendbytes(count) = b
If (count > 1023) Then
networkstream.Write(sendbytes, 0, 1024)
count = 0
Else
count += 1
End If

Loop

If (CInt(fsRead.Length) Mod 1024 > 0) Then
networkstream.Write(sendbytes, 0, CInt(fsRead.Length) Mod 1024)
End If

'close the reader
bRead.Close()
networkstream.Close()
'close the writer
' bWrite.Close()

'close the file streams
fsRead.Close()
' fsWrite.Close()



Server
' Define the local address of the server.
Dim h As System.Net.IPHostEntry =
System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName)
Dim localAddr As IPAddress = h.AddressList.GetValue(0)
' Must listen on correct port- must be same as port client wants to
connect on.
Dim tcpListener As New TcpListener(localAddr, 8000)
Dim clientdata As String = " "
Dim count As Integer
Dim filesize, totalbytes As Integer
Dim bytestoread As Integer = 1024
Dim bwrite As BinaryWriter
Dim filename As String = "wing.jpg"
Dim bytesread As Integer
Dim outputstream = New FileStream(filename, FileMode.Create,
FileAccess.Write)

bwrite = New BinaryWriter(outputstream)

' Must listen on correct port- must be same as port client wants to
connect on.
tcpListener.Start()
Console.WriteLine("Waiting for connection...")
Try
'Accept the pending client connection and return
Dim tcpClient As TcpClient = tcpListener.AcceptTcpClient()
Console.WriteLine("Connection accepted.")
networkStream = tcpClient.GetStream()
' Read the data into a byte array


Dim bytes(1024) As Byte
bytesread = networkStream.Read(bytes, 0, 32)
filesize = Encoding.ASCII.GetString(bytes, 0, 32)
Console.WriteLine(filesize)

' Read the stream into a byte array
count = 0
Do
bytesread = networkStream.Read(bytes, 0, bytestoread) ' read
in 1024 bytes at a time until the final 1024 bytes
' If (bytesread > 0) Then
' outputstream.write(bytes, 0, bytestoread)
' End If
bwrite.Write(bytes, 0, bytesread)
Console.WriteLine(bytesread)
count += 1
totalbytes += bytesread
If ((bytestoread * count) > (filesize - bytestoread)) Then
bytestoread = filesize - (bytestoread * count)
End If

Loop While (totalbytes < filesize)
outputstream.flush()
networkStream.Close()
bwrite.Close()
outputstream.close()
bytes.Clear(bytes, 0, bytes.Length)

'Close all of the network ports now not needed
tcpClient.Close()
tcpListener.Stop()
Console.WriteLine("Exit - Press Enter")
Console.ReadLine()
Catch e As Exception
'Exception has taken plac and has been caught
Console.WriteLine(e.ToString())
Console.ReadLine()
End Try
 
Have you tried the effect of

bwrite.Flush

before bwrite.close? Perhaps you are closing the stream before all data
have been (physically) written? If this isn't the problem, perhaps you can
determine what might be the cause by looking at the differences between the
files sent and received -- this may point to the problem.

Dick

--
Richard Grier (Microsoft Visual Basic MVP)

See www.hardandsoftware.net for contact information.

Author of Visual Basic Programmer's Guide to Serial Communications, 4th
Edition ISBN 1-890422-28-2 (391 pages) published July 2004. See
www.mabry.com/vbpgser4 to order.
 
I'm having this issue too! However I'm not using binary transfer, I'm using
UnicodeEncoder.Encode to convert to a string and then transferring in xml
through a TCP/IP socket. The file arrives fine and will load fine in
paintbrush and if I load it as a bitmap in my WinForm app, but when I try and
open it in Adobe programs (Photoshop CS etc) I get an "early end of file"
error. I then tried manually copying the files through activesync and the
Adobe programs open the same file fine!

What I think is happening is that activesync is converting the files as they
transfer to add/remove certain PocketPC specific encryption of the jpg file.
(the first time I tried to transfer a file it asked me if I wanted to convert
it...)

Does anyone know how to perform these conversions?! Or even where some
information regarding the different formats of the files is?

Cheers,
Peter Mauger
 
Back
Top