Sockets wp7中的UDP套接字接收失败
我是WP7和Socket编程的新手。我已经阅读了msdn示例代码并进行了使用测试。发送工作正常,但无法接收,这是我用于接收udp数据包数据的代码 在这种情况下,我的断点总是失败@if e.SocketError==SocketError.SuccessSockets wp7中的UDP套接字接收失败,sockets,windows-phone-7,Sockets,Windows Phone 7,我是WP7和Socket编程的新手。我已经阅读了msdn示例代码并进行了使用测试。发送工作正常,但无法接收,这是我用于接收udp数据包数据的代码 在这种情况下,我的断点总是失败@if e.SocketError==SocketError.Success 您是否尝试过在WP7/Silverlight中使用特定的UDP支持?使用或取决于您的场景和需求。这里有一篇关于Silverlight中UDP的介绍文章@:你能给出UDP接收的工作示例吗?没有。根据你要完成的工作,有很多Silverlight 4博
您是否尝试过在WP7/Silverlight中使用特定的UDP支持?使用或取决于您的场景和需求。这里有一篇关于Silverlight中UDP的介绍文章@:你能给出UDP接收的工作示例吗?没有。根据你要完成的工作,有很多Silverlight 4博客文章解释了使用我刚才提到的两个类实现UDP的更优点。
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;
}