Windows Phone 7 下 Socket(TCP) 与 PC 通讯,使用 WP7 模拟器与 PC 上的 Simple TCP 服务进行通讯。
TCP 客户端主要实现 Socket 连接的建立、数据的发送与接收和关闭已经建立的 Socket。
1 using System; 2 using System.Net; 3 using System.Windows; 4 using System.Windows.Controls; 5 using System.Windows.Documents; 6 using System.Windows.Ink; 7 using System.Windows.Input; 8 using System.Windows.Media; 9 using System.Windows.Media.Animation; 10 using System.Windows.Shapes; 11 12 // 手动增加的 13 using System.Net.Sockets; 14 using System.Threading; 15 using System.Text; 16 17 namespace SocketClientTest 18 { 19 public class SocketClient 20 { 21 Socket socket = null; 22 23 // 当一个异步操作完成时, 用于通知的事件对象 24 static ManualResetEvent socketDone = new ManualResetEvent(false); 25 26 // 异步操作超过时间定义. 如果在此时间内未接收到回应, 则放弃此操作. 27 const int TIMEOUT_MILLISECONDS = 5000; 28 29 // 最大接收缓冲 30 const int MAX_BUFFER_SIZE = 2048; 31 32 // Socket 连接到指定端口的服务器 33 // hostName 服务器名称 34 // portNumber 连接端口 35 // 返回值: 连接结果返回的字符串 36 public string Connect(string hostName, int portNumber) 37 { 38 string result = string.Empty; 39 40 // 创建 DnsEndPoint. 服务器的端口传入此方法 41 DnsEndPoint hostEntry = new DnsEndPoint(hostName, portNumber); 42 43 socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 44 45 // 创建一个 SocketAsyncEventArgs 对象用于连接请求 46 SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs(); 47 socketEventArg.RemoteEndPoint = hostEntry; 48 49 // Inline event handler for the Completed event. 50 // Note: This event handler was implemented inline in order to make this method self-contained. 51 socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(delegate(object s, SocketAsyncEventArgs e) 52 { 53 result = e.SocketError.ToString(); 54 // 标识请求已经完成, 不阻塞 UI 线程 55 socketDone.Set(); 56 } 57 ); 58 59 socketDone.Reset(); 60 socket.ConnectAsync(socketEventArg); 61 socketDone.WaitOne(TIMEOUT_MILLISECONDS); 62 63 return result; 64 } 65 66 public string Send(string data) 67 { 68 string response = "Operation Timeout"; 69 70 if (socket != null) 71 { 72 // 创建 SocketAsyncEventArgs 对象、并设置对象属性 73 SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs(); 74 socketEventArg.RemoteEndPoint = socket.RemoteEndPoint; 75 socketEventArg.UserToken = null; 76 77 socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(delegate(object s, SocketAsyncEventArgs e) 78 { 79 response = e.SocketError.ToString(); 80 socketDone.Set(); 81 } 82 ); 83 84 // 将需要发送的数据送入发送缓冲区 85 byte[] payload = Encoding.UTF8.GetBytes(data); 86 socketEventArg.SetBuffer(payload, 0, payload.Length); 87 88 socketDone.Reset(); 89 socket.SendAsync(socketEventArg); 90 socketDone.WaitOne(TIMEOUT_MILLISECONDS); 91 } 92 else 93 { 94 response = "Socket is not initialized"; 95 } 96 97 return response; 98 } 99 100 public string Receive() 101 { 102 string response = "Operation Timeout"; 103 104 if (socket != null) 105 { 106 SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs(); 107 socketEventArg.RemoteEndPoint = socket.RemoteEndPoint; 108 socketEventArg.SetBuffer(new Byte[MAX_BUFFER_SIZE], 0, MAX_BUFFER_SIZE); 109 110 socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(delegate(object s, SocketAsyncEventArgs e) 111 { 112 if (e.SocketError == SocketError.Success) 113 { 114 // 从接收缓冲区得到数据 115 response = Encoding.UTF8.GetString(e.Buffer, e.Offset, e.BytesTransferred); 116 response = response.Trim('