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
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