如何为 Windows Phone 创建和使用 UDP 套接字客户端应用程序
来源:互联网 发布:酷炫js效果 编辑:程序博客网 时间:2024/05/16 14:15
原文地址: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(); } }
0 0
- 如何为 Windows Phone 创建和使用 UDP 套接字客户端应用程序
- 如何为 Windows Phone 创建全景应用
- 如何在 Windows Phone 的代码中创建应用程序栏
- Windows Phone 学习 创建和使用数据库
- 如何保留和还原 Windows Phone 应用程序状态
- Windows套接字编程:基于TCP和UDP协议
- Windows套接字编程:基于TCP和UDP协议
- 03、Windows Phone 套接字(Socket)实战之WP客户端设计
- 基本套接字:UDP 客户端/服务器端
- [翻译]创建第一个Windows Phone应用程序
- 创建一个Windows Phone 应用程序工程
- 如何为 Windows Phone 保留和还原页面状态
- 在Windows Azure上为iOS、 Android 和 Windows Phone构建可扩展的移动应用程序
- 如何在 Windows Phone 应用程序的一个 Pivot 控件中使用不同的应用程序栏
- windows套接字(socket)UDP篇
- 使用 Windows Phone 应用程序中的相机功能
- 使用异步客户端套接字
- 如何为 Windows Phone 解码 JPEG
- hive中partition如何使用
- 核密度估计
- samba安装
- NString常用方法
- 工厂工作
- 如何为 Windows Phone 创建和使用 UDP 套接字客户端应用程序
- 今天解决了一个C++中动态二维数组的问题
- 第六章作业
- 多网卡导致的应用集群访问部分节点登陆慢问题
- 生活不像电影,生活比电影难多了
- linux time 和/usr/bin/time
- Floyd求最小环并求不同最小环的个数
- Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (四) —— ContentProvider
- 使用JFreeChart生成热点图表实现交互作用