Crazyloonybin Crazyloonybin - 4 years ago 230 Question sending file over TCP

I know similar questions like this have been asked on various forums, however none of the given solutions have worked for me, and was wondering if anyone had any other pointers they could give. I am basically trying to send a file (an image) over TCP between 2 computers on the same network. I have been trying to convert the image to a byte array, then to a string before sending the string. On the other side, I have received the string, converted to a byte array, and then convert back to an image. However, on the receiving side, the string is empty so I am getting an error when I then convert that to a byte array.

The code for the sending side is:

Private Sub startSending()
Dim ScreenShot As Image = sc.CaptureScreen
Dim path As String = My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData + "\redsquirimgtest"
sc.CaptureScreenToFile(path, System.Drawing.Imaging.ImageFormat.Jpeg)
MsgBox("printscreen saved")

Dim abyt() As Byte = ConvertImageFiletoBytes(path)
MsgBox("image converted to byte array")

Dim str As String = byteArrToString(abyt)

client.Connect("", 55000)
Dim Writer As New StreamWriter(client.GetStream())


Form2.PictureBox1.Image = ScreenShot

End Sub

Public Function ConvertImageFiletoBytes(ByVal ImageFilePath As String) As Byte()
Dim _tempByte() As Byte = Nothing
If String.IsNullOrEmpty(ImageFilePath) = True Then
Throw New ArgumentNullException("Image File Name Cannot be Null or Empty", "ImageFilePath")
Return Nothing
End If
Dim _fileInfo As New IO.FileInfo(ImageFilePath)
Dim _NumBytes As Long = _fileInfo.Length
Dim _FStream As New IO.FileStream(ImageFilePath, IO.FileMode.Open, IO.FileAccess.Read)
Dim _BinaryReader As New IO.BinaryReader(_FStream)
_tempByte = _BinaryReader.ReadBytes(Convert.ToInt32(_NumBytes))
_fileInfo = Nothing
_NumBytes = 0
Return _tempByte
Catch ex As Exception
Return Nothing
End Try
End Function

Public Function byteArrToString(ByVal arr() As Byte) As String

Return System.Text.Encoding.Unicode.GetString(arr)

End Function

And then the receiving side:

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
' Check the TcpListner Pending Property
If TcpListener.Pending = True Then

Dim Message As String = ""
ConnectClient = TcpListener.AcceptTcpClient()

Dim Reader As New StreamReader(ConnectClient.GetStream())
While Reader.Peek > -1
Message = Message + Reader.Read().ToString
End While

Dim abyt() As Byte = StrToByteArray(Message)
MsgBox("string converted to byte array")

Dim img As Image = ConvertBytesToImageFile(abyt)
MsgBox("byte array converted to image")

PictureBox1.Image = img
MsgBox("picture loaded in form")
End If

End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
End Sub

Public Function ConvertBytesToImageFile(ByVal ImageData As Byte()) As Image

Dim myImage As Image
Dim ms As System.IO.Stream = New System.IO.MemoryStream(ImageData)
myImage = System.Drawing.Image.FromStream(ms)
Return myImage

End Function

Public Shared Function StrToByteArray(str As String) As Byte()

Return System.Text.Encoding.Unicode.GetBytes(str)

End Function

Thanks in advance for any help you can give!

Answer Source

Not sure if is the necessarily problem here, but I would suggest converting the bytes to string using System.Convert.ToBase64String(byte_array) This will always give a safe string with no awkward characters which can be transmitted in various ways.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download