发送多个UDP包.NET时出现奇怪的行为

时间:2016-04-26 19:15:51

标签: vb.net udp byte

我想使用多个UDP包在互联网上发送大图像。我剪切并将图像发送到65000字节的数组。我的照片可分为3个包(2 * 65000和2805)。但是只有第一个包到了。我知道包裹不会在“途中”丢失。
看起来包裹从未发送过。这是“发件人”的代码:

Public publisher As New Sockets.UdpClient(0)
Public subscriber As New Sockets.UdpClient(2000)
Public ep As IPEndPoint = New IPEndPoint(IPAddress.Any, 0)



Public sub send()
    publisher.Connect(TBTo.Text, TBPort.Text)
    send_image("C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Blue hills.jpg")
End sub

Public Sub send_image(ByVal file_path As String)
        Dim img As Image = Image.FromFile(file_path)
        Dim bArr As Byte() = imgToByteArray(img)
        Dim buffer As Byte(), packet_size As Long
        Dim no_of_packages As Integer
        Dim max_size = bArr.Length



        no_of_packages = CInt(max_size / 65000) + 1  

        For i As Integer = 0 To no_of_packages - 1
            packet_size = IIf(CInt((max_size - i * 65000) / 65000), 65000, max_size - i * 65000) ''the maximum size of a single packet is set to 65000 and the last packet will get the remaining of bytes..

            buffer = bArr.Skip(i * 65000).Take(packet_size).ToArray
            publisher.Send(buffer, packet_size)
        Next

End Sub

然后在计时器中检查传入的数据包:

Private Sub UDP_listener_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UDP_listener.Tick

        Try
            Dim ep As IPEndPoint = New IPEndPoint(IPAddress.Any, 0)
            receive_image()
            Debug.Print("received")   ''executed only once

        Catch ex As Exception
            ''nothing
        End Try

End Sub

Public Sub receive_image()
        Dim rcvbytes() As Byte = subscriber.Receive(ep)   ''Only the first packet

        Dim img1 As Image = byteArrayToImage(rcvbytes)
        My.Forms.client.p2.Image = img1
End Sub

问题出在哪里?

0 个答案:

没有答案