【问题标题】:UdpClient can't receive from DatagramSocketUdpClient 无法从 DatagramSocket 接收
【发布时间】:2012-11-19 01:03:07
【问题描述】:

我正在申请。服务器端在桌面上工作,客户端在 Windows 8 上工作(Metro App-.Net for Windows Store)

这是我的服务器代码:

var udpServer = new UdpClient(7800);
udpServer.BeginRecieve(new AsyncCallback(OnUdpRecieve), null);
. . .
private void OnUdpRecieve(IAsyncResult result)
{
    byte[] data = udpServer.EndReceive(result, ref peerEndPoint);
    udpServer.BeginReceive(new AsyncCallback(OnUdpRecieve), null);
}

这是我在 Win RT 上的客户端代码:

var udpClient = new DatagramSocket();
await udpClient.ConnectAsync(new HostName("localhost"), 7800);
var udpWriter = new DataWriter(udpClient.OutputStream);
udpWriter.WriteBytes(new byte[] {0, 3, 5});
await udpWriter.StoreAsync();

但在服务器端我什么也得不到。我想也许我做错了什么。

谢谢

【问题讨论】:

    标签: c# datagram udpclient


    【解决方案1】:

    我在 MSDN 论坛上阅读了一个问题。这不是我的答案,但有一点是“loclhost”与“127.0.0.1”不同。

    当您使用“localhost”时,您强制数据报使用 IPv4,当您使用“127.0.0.1”时,您强制数据报套接字使用 IPv6。

    这就是重点;)

    【讨论】:

    • 也许您的 IPv4 和 IPv6 倒退了? “127.0.0.1”是 IPv4 地址。 "::1" 是等效的 IPv6 地址。 “localhost”可以是任一个,随系统的心血来潮。
    猜你喜欢
    • 1970-01-01
    • 2014-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多