c# - UDP DatagramSocket的正确使用

标签 c# sockets networking udp windows-runtime

我的基类中有以下代码(下面提供了片段),用于处理 UDP 套接字。我添加了对 WinRT 的支持(由 #define for WINRT 控制)。我的 .Net 一切正常,但我的 WinRT 实现出了点问题。当我发送数据时,服务器端没有接收到该数据。知道出了什么问题吗?我在客户端没有看到任何错误,只是服务器上没有显示任何内容。我已经成功地让我的 TCP 套接字类正常工作,以类似的方式使用 DataWriter。

        public UdpSocketClient(IPEndPoint remoteEndPoint, int localPort)
        {
            this.remoteEndPoint = remoteEndPoint;
#if WINRT
            this.socket = new DatagramSocket();
            this.socket.MessageReceived += ReceiveCallback;

            // Bind to any port
            Logger.Debug("{0}: UdpSocketClient created. Binding to port {1}.", this, (localPort == 0) ? "[any]" : localPort.ToString());
            IAsyncAction bindAction = this.socket.BindEndpointAsync(new HostName("localhost"), localPort == 0 ? "0" : localPort.ToString());
            bindAction.AsTask().Wait();
            Logger.Trace("{0}: Bind Complete to port {1}", this, this.socket.Information.LocalServiceName);

            // Get IOutputStream
            Logger.Trace("{0}: Getting output stream to {1}.", this, remoteEndPoint);
            IAsyncOperation<IOutputStream> asyncOutput = this.socket.GetOutputStreamAsync(remoteEndPoint.address, remoteEndPoint.port.ToString());
            asyncOutput.AsTask().Wait();
            Logger.Trace("{0}: Got output stream.", this);

            // Create DataWriter
            dataWriter = new DataWriter(asyncOutput.GetResults());
#else
            ...
#endif
        }


        public void SendBuffer(ByteBuffer buffer, int wait = 0)
        {
#if WINRT
            Logger.Trace("{0}: Sending buffer. Wait = {1}", this, wait);
            for (int i = 0; i < buffer.WriteSize(); i++)
                dataWriter.WriteByte(buffer.Buffer()[i]);
            DataWriterStoreOperation op = dataWriter.StoreAsync();
            if (wait != 0) op.AsTask().Wait(wait);
            else op.AsTask().Wait();
            Logger.Trace("{0}: Sending complete.", this);
#else
        ...
#endif
        }

一些相关日志:

04/23 19:08:57.504 DEBUG: Area Sync: UdpSocketClient created. Binding to port [any].
04/23 19:08:57.505 TRACE: Area Sync: Bind Complete to port 59518
04/23 19:08:57.506 TRACE: Area Sync: Getting output stream to 71.227.179.128:1302.
04/23 19:08:57.507 TRACE: Area Sync: Got output stream.

04/23 19:08:57.604 TRACE: Area Sync: Sending contact packet.
04/23 19:08:57.604 TRACE: Area Sync: Sending buffer. Wait = 0
04/23 19:08:57.605 TRACE: Area Sync: Sending complete.

最佳答案

我明白了。显然它不喜欢我的绑定(bind)代码。文档说你应该能够将 null 传递到绑定(bind)中,但我总是遇到异常(exception)。因此,如果我使用 ConnectAsync API,它就可以工作:

        public UdpSocketClient(IPEndPoint remoteEndPoint, int localPort)
        {
            this.remoteEndPoint = remoteEndPoint;
#if WINRT
            this.socket = new DatagramSocket();
            this.socket.MessageReceived += ReceiveCallback;

            Logger.Trace("{0}: Connecting to {1}.", this, remoteEndPoint);
            IAsyncAction connectAction = this.socket.ConnectAsync(remoteEndPoint.address, remoteEndPoint.port.ToString());
            connectAction.AsTask().Wait();
            Logger.Trace("{0}: Connect Complete. Status {1}, ErrorCode {2}", this, connectAction.Status, 
                connectAction.ErrorCode != null ? connectAction.ErrorCode.Message : "None");

            dataWriter = new DataWriter(this.socket.OutputStream);
#else
            ...
#endif
        }

关于c# - UDP DatagramSocket的正确使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10290945/

相关文章:

C# - 使用可自定义的矩形创建 X 轴和 Y 轴

c# - 顺序验证

java - 客户端服务器多线程Socket

java - 从套接字读取时获取空字符串

Python 多进程队列 get() block

iphone - 如何在我的 iPhone 应用程序和 Mac/PC 之间共享文件

c# - 如何在 C# 中执行 C++ 样式的析构函数?

networking - 了解路由表条目

java - 无法通过对象输出流将对象从一个 http 处理程序发送到另一个

c# - 如何使用两个模型制作创建类型的 View