Canot在mono上创建udp连接

时间:2015-07-17 17:43:08

标签: c# mono udp

我无法创建UDP连接:

public void Connect(String host, int port)
{

    System.Net.IPAddress address = Dns.GetHostAddresses (host)[0];
    this.hostport = new IPEndPoint(address, port);

    this.socket = new Socket(
        AddressFamily.InterNetwork, 
        SocketType.Dgram, 
        ProtocolType.Udp
    );

    this.socket.BeginConnect(this.hostport, new AsyncCallback(socket__onConnect), this.socket);
}

private void socket__onConnect(IAsyncResult ar)
{
    // It is never called on udp connections :(
    try 
    {
        Socket client = (Socket) ar.AsyncState;
        client.EndConnect(ar);
        Console.WriteLine("Success!");
    }
    catch (Exception e) 
    {
        Console.WriteLine(e.ToString());
    }
}

尝试连接,但从不调用socket__onConnect。

使用TCP工作正常但UDP doeas不起作用。

我已将perdmissions连接到udp端口:

nc -v -u host.com 53

工作正常。

1 个答案:

答案 0 :(得分:0)

Mono bug?试试这个:

 Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp) { ExclusiveAddressUse = true ; } ;