DatagramSocket类不能从UdpClient接收数据 [英] DatagramSocket cannot receive data from UdpClient

查看:270
本文介绍了DatagramSocket类不能从UdpClient接收数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在连接到一个桌面应用程序的运RT应用程序,他们开始在UDP和TCP通信。

I am making an Win RT app that connects to a desktop app and they start to communicate in UDP and TCP.

我已经成功地实施了,我TCP通信从赢RT发送到桌面,并从桌面发送到赢RT。使用StreamSocket在Win RT和的TcpListener在桌面上。

I have successfully implemented TCP communication in that I can send from Win RT to Desktop and send from Desktop to Win RT. using StreamSocket on Win RT and TcpListener on desktop.

我也使得从赢RT发送UDP数据,桌面没有任何问题。但我不能接受从桌面发送到赢RT数据的。我使用下面的代码,我不觉得这有什么问题,但必须有一些东西。

I also made it to send Udp data from Win RT to desktop without any problem. But I can't receive data's sent from desktop to Win RT. I use the following code and I don't see any problem with that but there must something.

    var g = new DatagramSocket();
    g.MessageReceived += g_MessageReceived;
    g.BindEndpointAsync(new HostName("127.0.0.1"), "6700");
    .
    .
    .
    void g_MessageReceived(DatagramSocket sender, DatagramSocketMessageReceivedEventArgs args)
    { // <- break point here.

    }

这断点永远不会停止,这意味着它永远不会代码一个消息。
我只能想到IBuffer的,因为在我的StreamSocket我应该得到由reader.GetBuffers字节(),而不是reader.GetBytes()。不过这是我需要考虑的赢RT,而不是桌面的东西。因为在TCP我只是发个字节,我得到在Win RT因此同样应该发生的DatagramSocket类以及缓冲器。

That break point never stops the code which means it never gets a message. I can only think of IBuffer because on my StreamSocket I should get the bytes by reader.GetBuffers() and not reader.GetBytes(). However that's the thing I need to think about on the Win RT and not Desktop. because on Tcp I just send bytes and I get buffers in Win RT so the same should happen for DatagramSocket as well.


  • 读卡器= DataReader的

感谢你们。

推荐答案

我不熟悉的新DatagramSocket类,但通常绑定到127.0.0.1意味着你将只能接收发送到环回适配器的消息。由于你的包从另一台主机来了,就应该在NIC,而不是环回适配器来接收

I'm not familiar with the new DatagramSocket class, but usually binding to 127.0.0.1 means that you will only receive messages sent to the loopback adapter. Since your packets are coming from another host, they should be received on a NIC, not the loopback adapter.

修改:从看为你使用,你可以使用 BindServiceNameAsync()方法,而不是 DatagramSocket的API文档BindEndpointAsync()以绑定到所有适配器指定的端口,这是相同的行为,我的System.Net.Sockets API下面的例子。所以,在你的榜样,你必须:

Edit: From looking at the documentation for the DatagramSocket API that you're using, you can just use the BindServiceNameAsync() method instead of BindEndpointAsync() in order to bind to the specified port on all adapters, which is the same behavior as my System.Net.Sockets API example below. So, in your example, you'd have:

g.BindServiceNameAsync("6700");



当然,你还需要确保在桌面主机上的防火墙设置允许它。监听指定端口上的传入UDP数据包

Of course, you'll also want to make sure your firewall settings on the desktop host allow it to listen for incoming UDP packets on the specified port.

试试下面的代码:

    using System.Net;
    using System.Net.Sockets;

    public class UdpState
    {
        public UdpClient client;
        public IPEndPoint ep;
    }

    ...

    private void btnStartListener_Click(object sender, EventArgs e)
    {
        UdpState state = new UdpState();
        //This specifies that the UdpClient should listen on EVERY adapter
        //on the specified port, not just on one adapter.
        state.ep = new IPEndPoint(IPAddress.Any, 31337);
        //This will call bind() using the above IP endpoint information. 
        state.client = new UdpClient(state.ep);
        //This starts waiting for an incoming datagram and returns immediately.
        state.client.BeginReceive(new AsyncCallback(bytesReceived), state);
    }

    private void bytesReceived(IAsyncResult async)
    {
        UdpState state = async.AsyncState as UdpState;
        if (state != null)
        {
            IPEndPoint ep = state.ep;
            string msg = ASCIIEncoding.ASCII.GetString(state.client.EndReceive(async, ref ep));
            //either close the client or call BeginReceive to wait for next datagram here.
        }
    }

请注意,在上面的代码,很显然你应该使用无论编码你发送跨的字符串。当我写的测试程序,我派在ASCII字符串。如果你在Unicode的发送,只需要使用 UnicodeEncoding.Unicode 而不是 ASCIIEncoding.ASCII

Note that in the above code, you should obviously use whatever encoding you're sending the string across with. When I wrote that test app, I sent the string in ASCII. If you're sending it in Unicode, just use UnicodeEncoding.Unicode instead of ASCIIEncoding.ASCII.

如果没有这工作,你可能想摆脱像Wireshark的数据包捕获工具,以确保从RT主机的UDP包,其实,让桌面主机

If none of this works, you might want to break out a packet capture utility like Wireshark to make sure that the UDP packet from the RT host is, in fact, getting to the desktop host.

这篇关于DatagramSocket类不能从UdpClient接收数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆