vb.net通过TCP发送文件

时间:2012-09-15 21:13:59

标签: vb.net tcp

我知道在各种论坛上都会提出类似这样的问题,但是没有一个给定的解决方案对我有用,并且想知道是否有人有任何其他指示他们可以提供。我基本上试图通过TCP在同一网络上的两台计算机之间发送文件(图像)。我一直在尝试将图像转换为字节数组,然后在发送字符串之前将其转换为字符串。另一方面,我收到了字符串,转换为字节数组,然后转换回图像。但是,在接收端,字符串是空的,所以当我将其转换为字节数组时,我收到错误。

发送方的代码是:

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)
    MsgBox(str)

    client.Connect("192.168.1.10", 55000)
    Dim Writer As New StreamWriter(client.GetStream())
    Writer.Write(str)
    Writer.Flush()

    MsgBox("sent")

    Form2.PictureBox1.Image = ScreenShot
    Form2.Show()

    MsgBox("done")
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
    Try
        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
        _FStream.Close()
        _FStream.Dispose()
        _BinaryReader.Close()
        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

然后是接收方:

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()
        MsgBox("accepting")

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

        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
    TcpListener.Start()
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

预先感谢您提供任何帮助!

3 个答案:

答案 0 :(得分:1)

不确定这是否是必然的问题,但我建议使用System.Convert.ToBase64String(byte_array)将字节转换为字符串 这将始终提供一个安全的字符串,没有任何尴尬的字符,可以通过各种方式传输。

答案 1 :(得分:0)

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()
        MsgBox("accepting")

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

        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
    TcpListener.Start()
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

答案 2 :(得分:0)

sc.CaptureScreen sc.CaptureScreenToFile(path,System.Drawing.Imaging.ImageFormat.Jpeg)

sc。 <-ist nicht deklariert错误