使用VB.NET中的套接字通过TCP发送字节数组

时间:2017-08-14 09:16:33

标签: arrays vb.net sockets tcp

我目前正在开发一个通过TCP发送文件的小程序。 我使用的是Visual Studio 2010,编程语言是VB.NET 我希望它发送文件的方式是首先将文件放入一个我已经想出并正在运行的字节数组中,但我需要一次发送一个字节的字节数组,这样我就可以看到它有多远发送字节。现在我想,我可以从数组中获取1个字节并将其放入临时字节数组中,然后使用 tcpServer.Send(tempbyte)发送它

但我得到的只是这个错误

> `error  1   Overload resolution failed because no accessible 'Send' can
> be called with these arguments:
>     'Public Function Send(buffers As System.Collections.Generic.IList(Of System.ArraySegment(Of Byte))) As
> Integer': Value of type 'Byte' cannot be converted to
> 'System.Collections.Generic.IList(Of System.ArraySegment(Of Byte))'.
>     'Public Function Send(buffer() As Byte) As Integer': Value of type 'Byte' cannot be converted to '1-dimensional array of
> Byte'.  C:\Users\Sander\AppData\Local\Temporary Projects\File
> transfer\Form1.vb   40  13  File transfer`

我怎么能解决这个问题,因为我被卡住了:(

提前致谢:)

我遇到问题的代码:(由于某种原因,我不得不拆分代码)

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim filename As String
        Dim i As Integer
        CurrentOperationLabel.Text = "Current operation: Reading file into memory..."
        ProgressBar1.Style = ProgressBarStyle.Marquee
        filename = System.IO.Path.GetFullPath(OpenFileDialog1.FileName)
        Application.DoEvents()
        Dim bytes() As Byte = IO.File.ReadAllBytes(filename)
        FileByteLength = bytes.Length
        Label3.Text = "Transfered bytes: 0 / " & FileByteLength.ToString 



    CurrentOperationLabel.Text = "Current operation: Transfering file..."
    ProgressBar1.Style = ProgressBarStyle.Blocks

    Dim tempstring As String = ""
    Dim tempbyte As Byte
    FileByteCounter = FileByteCounter + 1
    For i = 0 To bytes.GetUpperBound(0)
        Application.DoEvents()
        tempbyte = bytes(i)
        tcpServer.Send(tempbyte) 'this is the point it fails at :/
        FileByteCounter = FileByteCounter + 1
        Label3.Text = "Transfered bytes: " & FileByteCounter.ToString & " / " & FileByteLength.ToString
        ProgressBar1.Value = FileByteCounter / FileByteLength * 100
    Next i

    i = 0
    FileByteCounter = 0
    FileByteLength = 0
End Sub`

1 个答案:

答案 0 :(得分:0)

我的问题已经解决,但解决方案不再与问题相关,因为Visual Vincent让我从头开始帮助我,因为我希望它工作的方式是非常低效的,如果它甚至可以工作。他提出了很多我应该做的事情,最后一切都比我们已经拥有的代码继续下去还要好。谢谢Visual Vincent帮助我!