【问题标题】:No UDP packets received using async method VB.Net使用异步方法 VB.Net 未收到 UDP 数据包
【发布时间】:2016-08-25 19:15:59
【问题描述】:

       我正在尝试使用 UDP 通过局域网发送图片。我必须将图片“切割”成小包,然后在另一端重新组装。到目前为止,我已经制作了服务器和几乎客户端(服务器发送图片)。我已经用 BeginReceive 做了一些测试并工作(在其他项目上)。现在我在客户端上什么都没有(没有错误..什么都没有)。这是服务器的代码:

Imports System
Imports System.IO
Imports System.Net
Imports System.Threading
Imports System.Net.Sockets
Imports System.Text.Encoding


Public Class Form1
    Dim publisher As New Sockets.UdpClient(0)

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim sendbytes() As Byte = ASCII.GetBytes(txt1.Text)
        Dim img As Image, img_stream As MemoryStream, buffer As Byte()
        Dim packet_size As Integer = 1500, sent_size As Long


        Try
            publisher.Connect("localhost", 60000)
            img_stream = imgToBytes(txt1.Text)

            ReDim buffer(packet_size)

            While Not img_stream.Position = img_stream.Length
                sent_size += img_stream.Read(buffer, 0, packet_size)

                publisher.Send(buffer, buffer.Length)
            End While


        Catch ex As Exception
            Debug.Print(ex.Message)
        End Try

    End Sub

    Function imgToBytes(ByVal file_name As String) As MemoryStream
        Dim img As Image = Image.FromFile(file_name)
        Dim stream As New MemoryStream

        img.Save(stream, Drawing.Imaging.ImageFormat.Jpeg)
        stream.Position = 0

        Return stream
    End Function

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Form2.Show()
    End Sub
End Class

客户端是第二种形式:

Imports System
Imports System.IO
Imports System.Net
Imports System.Threading
Imports System.Net.Sockets
Imports System.Text.Encoding


Public Class Form2
    Dim ep As IPEndPoint = New IPEndPoint(IPAddress.Any, 0)
    Dim client As New UdpClient(1000)

    Public Event new_msg(ByVal msg As Byte())
    Public Sub client_msg(ByVal msg As Byte())
        Debug.Print("a")
    End Sub

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try      ''don't know why I put this here
            client.BeginReceive(New AsyncCallback(AddressOf receive), client)

        Catch ex As Exception
            Debug.Print(ex.Message)
        End Try
    End Sub

    Sub receive(ByVal ar As IAsyncResult)
        Dim buffer As Byte()
        Debug.Print("b")

        Try
            buffer = client.EndReceive(ar, ep)
            ''RaiseEvent new_msg(buffer)

            client.BeginReceive(New AsyncCallback(AddressOf receive), client)
        Catch ex As Exception
            Debug.Print(ex.Message)
        End Try
    End Sub

End Class


       问题出在哪里?

【问题讨论】:

  • 您是否使用断点单步执行您的代码?
  • 不...我不认为我会得到任何东西...我到处都印着...什么都没有
  • 通过断点,您可以获得应用程序停止执行代码的位置。我现在正在尝试你的代码。虽然我不禁注意到您没有对收到的数据做任何事情,那么真正的问题是什么?

标签: vb.net sockets udp packets


【解决方案1】:

您的问题是您的客户端没有通过同一个端口进行通信。

Form2 你这样做:

Dim client As New UdpClient(1000)

然后在Form1 中,当您连接时:

publisher.Connect("localhost", 60000)

把端口改成1000就可以了:

publisher.Connect("localhost", 1000)

【讨论】:

  • 成功了。但是我不明白一些东西..为什么我必须在创建 udpClient 的新实例时指定端口,而最终我将使用已经包含端口的 ipEndPoint?还有一个奇怪的地方:我把Dim client As New UdpClient(1000)中的端口删了,放到Dim client As New UdpClient(ep)。将 ep 的端口设置为 60000 时,它可以工作(例如 Dim ep As IPEndPoint = New IPEndPoint(IPAddress.Any, 60000))。但我知道 0 表示每个端口。使用Dim ep As IPEndPoint = New IPEndPoint(IPAddress.Any, 0) 它不起作用。为什么?
  • 当我像Dim ep As IPEndPoint = New IPEndPoint(IPAddress.Any, 60000) Dim client As New UdpClient(ep) 这样重写代码时,一切正常。但是对于“通用”端口 (0),它不会收到任何数据包:Dim ep As IPEndPoint = New IPEndPoint(IPAddress.Any, 0) Dim client As New UdpClient(ep)
  • @sergiu:嗯,奇怪。也许端口被IPEndPoint 更改,因为它可能不允许它为0?此外,您不必在创建UdpClient 时指定端口。执行Dim client As New UdpClient() 应该没有问题。
  • 这看起来很奇怪。看起来我正在从服务器发送所有数据包,但我从图片的 320000 字节中只获得了 67000 字节。我只收到 45 个 1500 字节的数据包...
  • 我开始讨厌 UDP。这是我第二次尝试通过局域网发送大图像......没有成功。我正在考虑使用 TCP 而不是 UDP
猜你喜欢
  • 1970-01-01
  • 2012-09-01
  • 2013-10-22
  • 2022-10-17
  • 1970-01-01
  • 2018-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多