如何为 Windows Phone 创建和使用 UDP 套接字客户端应用程序
2014-05-09 08:51
639 查看
原文地址:http://msdn.microsoft.com/zh-cn/library/hh202864(v=vs.92).aspx
/// <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; }
/// <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> 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) { 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; } /// <summary> /// Closes the Socket connection and releases all associated resources /// </summary> public void Close() { if (_socket != null) { _socket.Close(); } }
相关文章推荐
- 如何为 Windows Phone 创建和使用 TCP 套接字客户端应用程序【msdn】
- 使用NetBeans IDE 5.5创建应用程序客户端
- 如何使用 eMbedded Visual C++ 4.0 创建用于 Windows CE 4.1 仿真程序 (Windows CE5.0) 的应用程序
- 如何在 Windows Phone 的代码中创建应用程序栏
- 如何使用C#创建一个三层的数据库应用程序
- 使用NetBeans IDE 5.5创建应用程序客户端
- 如何创建供客户端使用的服务器别名(SQL Server 配置管理器)
- 如何使用完整客户端创建自己的instant client
- 如何使用C# WEBSERVICE 创建一个三层的数据库应用程序 http://www.xxju.net/article/200412/29_0004326567.htm
- 使用 Eclipse 和 Java SE 6 创建独立 Web Services 应用程序,第 2 部分: Web 服务客户端应用程序
- 如何创建供客户端使用的服务器别名(SQL Server 配置管理器)
- 使用NetBeans IDE 5.5创建应用程序客户端
- 如何使用Firebase创建Android聊天应用程序
- 如何创建和使用wxApp应用程序对象
- 使用NetBeans IDE 5.5创建应用程序客户端
- 如何使用LTFrame去创建一个应用程序?
- 如何在 Windows Phone 应用程序的一个 Pivot 控件中使用不同的应用程序栏
- 在Maximo7中如何使用日志记录应用程序创建一个日志文件?
- 如何使用XCode 4创建ipa文件及提交应用程序
- 如何使用XCode 4创建ipa文件及提交应用程序