2012-01-23 9 views
1

私はWP7とソケットプログラミングの初心者です。私はmsdnサンプルコードhttp://msdn.microsoft.com/en-us/library/hh202864(v=VS.92).aspx#Y4537を使い、使用のためにテストしました。送信は正常ですが受信できませんでしたが、これはudpパケットデータの受信に使用したコードです。UDPソケット受信がwp7で失敗する

この中で私のブレークポイントは、常にif (e.SocketError == SocketError.Success)

public string Receive(int portNumber) 
    { 
     string response = "Operation Timeout"; 

     // We are receiving over an established socket connection 
     if (_socket != null) 
     { 
      // Create SocketAsyncEventArgs context object 
      SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs(); 
      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) 
      { 
       try 
       { 
        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(); 
       } 
       catch (Exception ex) 
       { 
        ex.ToString(); 
       } 

      }); 

      // 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; 
    } 

答えて

0

@失敗は、あなたはWP7/Silverlightの特定のUDPのサポートを使用してみましたか?シナリオや要件に応じてUdpSingleSourceMulticastClientまたはUdpAnySourceMulticastClientを使用してください。ここでは、SilverlightのUDPに関するイントロ記事です。Working with Multicast

+0

:UDP受信のための作業サンプルはありますか? –

+0

いいえ、何を達成しようとしているかによって、先ほど触れた2つのクラスを使ってUDP実装の細かい点を説明する多くのSilverlight 4のブログ記事があります。 – JustinAngel

関連する問題