locked
求教windows phone8 下的UDP编程 RRS feed

  • 问题

  • 各位大神,小弟最近在写wp8应用,遇到个UDP的问题。

    参考了微软的这篇文章, http://msdn.microsoft.com/zh-cn/library/hh202864(v=vs.92).aspx

    可是我自己把IP改成自己局域网的IP,端口改成自己的UDP端口,发包返回值显示正确,可是用工具在服务器端没有接收到数据包,请求下,这个是怎么回事?

    wp8下是不是没有sendto方法 和UdpClient类?

    2014年5月13日 10:23

答案

  • 你好,

    >>如果ServerName是个网址,那这段代码该如何编写?譬如我要UDP到一个服务器上去,但是这个服务器映射出来的是一个网址。<<

    对这个问题我是这样认为的,如果你可以Ping通这个网址,类似于我们Ping www.baidu.com 一样,我们可以得到它的IP,你把ServerName换成这个IP地址试试。包括具体的端口。

    >>在模拟器上是不是没有虚拟网卡驱动,不能配置网卡参数<<

    关于WP8模拟器上网络配置相关问题,请参考下面的链接:

    http://msdn.microsoft.com/zh-cn/library/windowsphone/develop/jj681694(v=vs.105).aspx

    谢谢!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年5月16日 1:49
    版主

全部回复

  • 你好,

    根据你的描述,我建议检查一下服务端的TCP/IP设置。前提是你确定发送是成功的。

    1.在“控制面板”中,打开“程序和功能”。

    2.单击“打开或关闭 Windows 功能”。

    3.在“Windows 功能”对话框中,选中“简单 TCP/IP 服务”复选框以启用此功能,然后单击“确定”。

    具体内容可以查看在您的计算机上启动简单 TCP/IP 服务

    >> WP8下是不是没有SendTo方法 UdpClient类?<<

    WP8下面是没有UdpClient这个类,UdpClient这个类的支持范围,请查看Platform后的内容:http://msdn.microsoft.com/en-us/library/system.net.sockets.udpclient.aspx

    谢谢!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年5月15日 1:41
    版主
  • 你好,感谢你的回复,我已经检查了“简单TCP/IP 服务”,确认是开启状态的。

    另外,我发送数据返回值是success,但是应该socket并没有把数据发送出去,因为我在服务器端用UDP工具一直监听着我的端口,并没有收到数据。

    这个是怎么回事呢

    private string SendData(byte[] data, int p)
            {
                //throw new NotImplementedException();
                string response = "Operation Timeout";
                if (_sock != null)
                {
                    SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
                    // Set properties on context object
                    socketEventArg.RemoteEndPoint = new DnsEndPoint(UDP_ADDRESS, UDP_PORT, AddressFamily.InterNetwork);
                    // Inline event handler for the Completed event.
                    // Note: This event handler was implemented inline in order to make this method self-contained.
                    socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(delegate(object s, SocketAsyncEventArgs e)
                    {
                        response = e.SocketError.ToString();
                        // Unblock the UI thread
                        _clientDone.Set();
                    });
                    socketEventArg.SetBuffer(data, 0, p);
    
                    // Sets the state of the event to nonsignaled, causing threads to block
                    _clientDone.Reset();
                    // Make an asynchronous Send request over the socket
                    _sock.SendToAsync(socketEventArg);
                    // Block the UI thread for a maximum of TIMEOUT_MILLISECONDS milliseconds.
                    // If no response comes back within this time then proceed
                    _clientDone.WaitOne(TIMEOUT_MILLISECONDS);
                    iState = 3;//发送完成
                }
                else
                {
                    response = "Socket is not initialized";
                    iState = 5;//异常
                }
                return response;
            }

    2014年5月15日 5:56
  • 你好,

    我这边有一个例子是OK的。请查看:

    	// Cached Socket object that will be used by each call for the lifetime of this class
        Socket _socket = null;
        // Signaling object used to notify when an asynchronous operation is completed
        static ManualResetEvent _clientDone = new ManualResetEvent(false);
        // Define a timeout in milliseconds for each asynchronous call. If a response is not received within this
        // timeout period, the call is aborted.
        const int TIMEOUT_MILLISECONDS = 5000;
        // The maximum size of the data buffer to use with the asynchronous socket methods
        const int MAX_BUFFER_SIZE = 2048;
    
        /// <summary>
        /// SocketClient Constructor
        /// </summary>
        public SocketClient()
        {
            // The following creates a socket with the following properties:
            // AddressFamily.InterNetwork - the socket will use the IP version 4 addressing scheme to resolve an address
            // SocketType.Dgram - a socket that supports datagram (message) packets
            // PrototcolType.Udp - the User Datagram Protocol (UDP)
            _socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        }
    
    
        /// <summary>
        /// Send the given data to the server using the established connection
        /// </summary>
        /// <param name="serverName">The name of the server</param>
        /// <param name="portNumber">The number of the port over which to send the data</param>
        /// <param name="data">The data to send to the server</param>
        /// <returns>The result of the Send request</returns>
        public string Send(string serverName, int portNumber, string data)
        {
            string response = "Operation Timeout";
            // We are re-using the _socket object that was initialized in the Connect method
            if (_socket != null)
            {
                // Create SocketAsyncEventArgs context object
                SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
                // Set properties on context object
                socketEventArg.RemoteEndPoint = new DnsEndPoint(serverName, portNumber);
                // Inline event handler for the Completed event.
                // Note: This event handler was implemented inline in order to make this method self-contained.
                socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(delegate(object s, SocketAsyncEventArgs e)
                {
                    response = e.SocketError.ToString();
                    // Unblock the UI thread
                    _clientDone.Set();
                });
                // Add the data to be sent into the buffer
                byte[] payload = Encoding.UTF8.GetBytes(data);
                socketEventArg.SetBuffer(payload, 0, payload.Length);
                // Sets the state of the event to nonsignaled, causing threads to block
                _clientDone.Reset();
                // Make an asynchronous Send request over the socket
                _socket.SendToAsync(socketEventArg);
                // Block the UI thread for a maximum of TIMEOUT_MILLISECONDS milliseconds.
                // If no response comes back within this time then proceed
                _clientDone.WaitOne(TIMEOUT_MILLISECONDS);
            }
            else
            {
                response = "Socket is not initialized";
            }
            return response;
        

    谢谢!

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年5月15日 5:59
    版主
  • Jimmy Yang 你好,感谢你的 回复。

    我的方法与你的例子是几乎一样的,如果你的可以使用,那按道理我的也应该是可以使用,除非我PC环境有问题,导致UDP发包失败。

    另外我想咨询个问题:

    socketEventArg.RemoteEndPoint = new DnsEndPoint(serverName, portNumber);

    如果ServerName是个网址,那这段代码该如何编写?譬如我要UDP到一个服务器上去,但是这个服务器映射出来的是一个网址。


    2014年5月16日 0:56
  • 发现一个问题,在模拟器上是不是没有虚拟网卡驱动,不能配置网卡参数,导致Socket在New的时候就有异常出现了,ErrorCode为10022。估计就是这个问题引起的吧。

    2014年5月16日 1:35
  • 你好,

    >>如果ServerName是个网址,那这段代码该如何编写?譬如我要UDP到一个服务器上去,但是这个服务器映射出来的是一个网址。<<

    对这个问题我是这样认为的,如果你可以Ping通这个网址,类似于我们Ping www.baidu.com 一样,我们可以得到它的IP,你把ServerName换成这个IP地址试试。包括具体的端口。

    >>在模拟器上是不是没有虚拟网卡驱动,不能配置网卡参数<<

    关于WP8模拟器上网络配置相关问题,请参考下面的链接:

    http://msdn.microsoft.com/zh-cn/library/windowsphone/develop/jj681694(v=vs.105).aspx

    谢谢!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年5月16日 1:49
    版主