为什么不提供本地UDP数据包?

时间:2013-03-21 18:23:37

标签: .net vb.net udp udpclient

我正在尝试编写一个非常简单的UDP客户端/服务器对,它将在同一台计算机上一起工作(现在),但我的数据包都没有被传送。下面的代码是100%完整的VB.NET控制台应用程序。它运行时不会抛出错误,但mListener.Receive命令永远不会返回。

问题1:为什么我没有收到任何数据包?

问题2:为什么我的客户端无法使用IPAddress.Any作为终点?当我尝试连接到新IPEndPoint(IPAddress.Any,10123)时,我得到“请求的地址在其上下文0.0.0.0:10123中无效”异常。

Imports System.Net
Imports System.Net.Sockets
Imports System.Threading

Module Module1
    Sub Main()
        Dim ep As New IPEndPoint(IPAddress.Loopback, 10123)
        Dim s As New Server(ep)
        Dim c As New Client(ep)
        Do
            Console.WriteLine("Waiting in main loop...")
            Thread.Sleep(5000)
        Loop
    End Sub
End Module

Public Class Server
    Private mBroadcaster As UdpClient

    Public Sub New(ep As IPEndPoint)
        mBroadcaster = New UdpClient
        mBroadcaster.ExclusiveAddressUse = False
        mBroadcaster.Connect(ep)
        Dim sender As New Thread(AddressOf SendLoop)
        sender.Start()
    End Sub

    Private Sub SendLoop()
        Do
            Dim msg As Byte() = Text.ASCIIEncoding.ASCII.GetBytes("Hello world")
            Console.WriteLine("Sending...")
            mBroadcaster.Send(msg, msg.Length)
            Thread.Sleep(1000)
        Loop
    End Sub
End Class

Public Class Client
    Private mListener As UdpClient

    Public Sub New(ep As IPEndPoint)
        mListener = New UdpClient
        mListener.ExclusiveAddressUse = False
        mListener.Connect(ep) 'Why can't I use IPAddress.Any here?
        Dim poller As New Thread(AddressOf PollLoop)
        poller.Start()
    End Sub

    Private Sub PollLoop()
        Do
            Dim ep As IPEndPoint = Nothing
            Console.WriteLine("  Receiver listening...")
            Dim incomingbytes As Byte() = mListener.Receive(ep)
            Dim msg As String = Text.ASCIIEncoding.ASCII.GetString(incomingbytes)
            Console.WriteLine("  Received: " & msg & " from " & ep.Address.ToString & ":" & ep.Port)
        Loop
    End Sub
End Class

1 个答案:

答案 0 :(得分:0)

我仍然不知道为什么原始代码不起作用,但我确实找到了一种方法来完成原始代码的目的。基本的想法是,将UdpClient用于服务器显然很好,但是对于客户端使用UdpClient是不行的。相反,我将UdpClient更改为较低级别的Socket。我怀疑用Socket替换服务器的UdpClient也没关系,但我没有测试过。这也解决了问题2(原始Socket允许侦听IPAddress.Any)。

我仍然会接受一个不同的答案,这实际上解释了为什么会这样。

Imports System.Net
Imports System.Net.Sockets
Imports System.Threading

Module Module1
    Sub Main()
        Dim s As New Server
        Dim c As New Client
        Do
            Console.WriteLine("Waiting in main loop...")
            Thread.Sleep(5000)
        Loop
    End Sub
End Module

Public Class Server
    Private mBroadcaster As UdpClient

    Public Sub New()
        mBroadcaster = New UdpClient
        mBroadcaster.ExclusiveAddressUse = False
        mBroadcaster.Connect(New IPEndPoint(IPAddress.Broadcast, 10123))
        Dim sender As New Thread(AddressOf SendLoop)
        sender.Start()
    End Sub

    Private Sub SendLoop()
        Do
            Dim msg As Byte() = Text.ASCIIEncoding.ASCII.GetBytes("Hello world")
            Console.WriteLine("Sending...")
            mBroadcaster.Send(msg, msg.Length)
            Thread.Sleep(1000)
        Loop
    End Sub
End Class

Public Class Client
    Private mListener As Socket

    Public Sub New()
        mListener = New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
        mListener.ExclusiveAddressUse = False
        mListener.Bind(New IPEndPoint(IPAddress.Any, 10123))
        Dim poller As New Thread(AddressOf PollLoop)
        poller.Start()
    End Sub

    Private Sub PollLoop()
        Dim buffer As Byte() = New Byte(1023) {}
        Do
            Console.WriteLine("  Receiver listening...")
            Dim nbytes As Integer = mListener.Receive(buffer)
            Dim msg As String = Text.ASCIIEncoding.ASCII.GetString(buffer, 0, nbytes)
            Console.WriteLine("  Received: " & msg)
        Loop
    End Sub
End Class