2012-07-14 77 views
0

我試圖開發了Windows Phone 7.5應用程序,我想創建一個監聽套接字聽UDP廣播消息在端口8001如何創建在Windows Phone的UDP廣播監聽套接字

我已經修改示例How to: Create and Use a UDP Socket Client Application for Windows Phone但我得到「無效的參數異常」,但我修復了該錯誤。

現在這是我的代碼:

public String SecureRecive(int portNumber, bool isBroadcast) 
    { 
     SocketAsyncEventArgs socketEventArg; 

     Send("melnibone", portNumber, " ", isBroadcast, out socketEventArg); 
     Thread.Sleep(1000); 

     if (!isHasSent) ; 
     return Receive(portNumber, isBroadcast, socketEventArg); 
    } 

    private string Send(string serverName, int portNumber, string data, bool isBroadcast, out SocketAsyncEventArgs socketEventArg) 
    { 
     string response = "Operation Timeout"; 

     // We are re-using the _socket object that was initialized in the Connect method 
     if (_socket != null) 
     { 
      socketEventArg = new SocketAsyncEventArgs(); 
      // Set properties on context object 
      if (isBroadcast) 
       socketEventArg.RemoteEndPoint = new IPEndPoint(IPAddress.Broadcast, portNumber); 
      else 
       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(); 
       isHasSent = true; 
      }); 
      // 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 
     { 
      socketEventArg = null; 
      response = "Socket is not initialized"; 
     } 

     return response; 
    } 

    /// <summary> 
    /// Receive data from the server 
    /// </summary> 
    /// <param name="portNumber">The port on which to receive data</param> 
    /// <returns>The data received from the server</returns> 
    private string Receive(int portNumber, bool isBroadcast, SocketAsyncEventArgs socketEventArg) 
    { 
     string response = "Operation Timeout"; 

     // We are receiving over an established socket connection 
     if (_socket != null) 
     { 
      if (isBroadcast) 
       socketEventArg.RemoteEndPoint = new IPEndPoint(IPAddress.Broadcast, portNumber); 
      else 
       socketEventArg.RemoteEndPoint = new IPEndPoint(IPAddress.Any, portNumber); 

      // Setup the buffer to receive the data 
      socketEventArg.SetBuffer(new Byte[MAX_BUFFER_SIZE], 0, MAX_BUFFER_SIZE); 

      // Inline event handler for the Completed event. 
      // Note: This even handler was implemented inline in order to make this method self-contained. 
      socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(delegate(object s, SocketAsyncEventArgs e) 
      { 
       if (e.SocketError == SocketError.Success) 
       { 
        // Retrieve the data from the buffer 
        response = Encoding.UTF8.GetString(e.Buffer, e.Offset, e.BytesTransferred); 
        response = response.Trim('\0'); 
       } 
       else 
       { 
        response = e.SocketError.ToString(); 
       } 

       _clientDone.Set(); 
      }); 
      // Sets the state of the event to nonsignaled, causing threads to block 
      _clientDone.Reset(); 

      // Make an asynchronous Receive request over the socket 
      _socket.ReceiveFromAsync(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; 
    } 

但我不知道如何設置套接字超時爲無限。

是否有可能在Windows Phone上創建這種套接字?

回答

1

讓我們依次是:

  1. 請問上面的代碼工作?你說你修復了「無效參數異常」的問題,但我的猜測是它仍然無法按預期工作,這是由於下面幾點的原因:
  2. 你不能在Windows Phone 7的套接字上「偵聽」您只能從與您發起通信的IP接收安全原因,因此先發送一個空包,然後再聽一段有限的時間。
  3. 不支持UDP廣播,只有TCP,UDP單播和UDP多播。

我希望以上三點總結出您的代碼的主要問題。