2013-02-07 41 views
0

我遇到了Windows Mobile的問題。Windows 7.x mobile中的UDP接收

我在寫一個代碼,從Windows Server Aplication(由我創建),其中廣播數據recieving UDP數據,但是,Windows的應用程序執行的時候,我encoutering一個可怕的問題:

的提供的參數無效

當插座ReceiveFromAsync()功能的作品。

任何人都可以幫助我嗎?

---這是我已經找到了解決方案的代碼

public string Receive(int portNumber) 
     { 
      string response = "Operation Timeout"; 
      // We are receiving over an established socket connection 
      registerSocket(); 
      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) 
       { 


        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(); 
        _socket.ReceiveFromAsync(socketEventArg); 
       }); 
       // 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);** // here the issue arises An  //invalid argument was supplied 
       // 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

沒有,沒有人可以幫你,除非你提供細節,像函數調用代碼,所有的參數從何而來。 –

+0

@lucian:嚴重的是,您提交了拼寫錯誤的「應用程序」,「功能」和「遇到」的編輯?此外,無論誰批准這一點都很可惜。 –

+0

對不起,我編輯這篇文章時只改進了格式,正如我在編輯摘要中所述。 –

回答

0

要接收Udp多播,您應該在接收和使用_socket.ReceiveAsync()而不是_socket.ReceiveFromAsync()之前調用_socket.Bind(IPEndpoint)。另外socketEventArg.RemoteEndpoint應該是空的。

此代碼的工作對我來說:

 _socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); 

     SocketAsyncEventArgs args = new SocketAsyncEventArgs(); 
     args.SetBuffer(new byte[MAX_BUFFER_SIZE], 0, MAX_BUFFER_SIZE); 
     args.Completed += args_Completed; 

     _socket.Bind(new IPEndPoint(IPAddress.Any, **your port**)); 
     _socket.ReceiveAsync(args);