Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 正确使用UDP DatagramSocket_C#_Sockets_Networking_Udp_Windows Runtime - Fatal编程技术网

C# 正确使用UDP DatagramSocket

C# 正确使用UDP DatagramSocket,c#,sockets,networking,udp,windows-runtime,C#,Sockets,Networking,Udp,Windows Runtime,在我的基类中,我有以下代码(下面提供的SNIP)用于处理UDP套接字。我正在添加对WinRT的支持(由#defineforWinRT控制)。我的一切都在为.Net工作,但我的WinRT实现出了问题。当我发送数据时,服务器端没有收到数据。知道出了什么问题吗?我在客户端没有看到任何错误,只是服务器上没有显示任何内容。我以类似的方式使用DataWriter成功地使我的TCP套接字类工作 public UdpSocketClient(IPEndPoint remoteEndPoint,

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

        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
        }

我想出来了。显然它不喜欢我的绑定代码。文档中说您应该能够将null传递到绑定中,但我总是得到一个例外。因此,如果我使用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
        }

首先使用数据包嗅探器查看客户端是否真的将数据包发送到网络上。我很确定它没有发送数据包。我运行的是相同的.Net代码,它可以正常工作。
        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
        }