gpt4 book ai didi

vb.net 通过 TCP 发送文件

转载 作者:可可西里 更新时间:2023-11-01 02:52:42 24 4
gpt4 key购买 nike

我知道在各种论坛上都提出过类似的问题,但是没有一个给定的解决方案对我有用,并且想知道是否有人可以提供任何其他指示。我基本上是想在同一网络上的两台计算机之间通过 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

在此先感谢您提供的任何帮助!

最佳答案

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

关于vb.net 通过 TCP 发送文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12442641/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com