1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Net.Sockets; 6 using System.Net; 7 using System.Threading; 8 9 namespace ConsoleApplication2 10 { 11 public class StateObject 12 { 13 public Socket newsocket = null; 14 public const int bufferSize = 256; 15 public byte[] buffer=new byte[bufferSize]; 16 public StringBuilder sb = new StringBuilder(); 17 18 } 19 public class AsynchronousClient 20 { 21 private const int port = 20150; 22 private static ManualResetEvent connectDone = new ManualResetEvent(false); 23 private static ManualResetEvent sendDone = new ManualResetEvent(false); 24 private static ManualResetEvent ReceiveDone = new ManualResetEvent(false); 25 private static String response = String.Empty; 26 27 28 public static void StartClient() 29 { 30 try 31 { 32 IPAddress ipAddress = IPAddress.Parse("192.168.20.215"); 33 IPEndPoint remoteEP = new IPEndPoint(ipAddress, port); 34 //生成一个TCP/IP socket 35 Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 36 //与目标终端连接 37 client.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), client); 38 //等待,直到连接程序完成。在ConnectCallback中适当位置有connecDone.Set()语句 39 connectDone.WaitOne(); 40 byte[] b = new byte[] { 0x00, 0x00, 0x00, 0x29, 0x00, 0x00, 0x00, 0x13, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x30, 0x00, 0x04 }; 41 //发送数据 42 Send(client, b); 43 sendDone.WaitOne(); 44 Receive(client); 45 ReceiveDone.WaitOne(); 46 Console.WriteLine("Response{0}", response); 47 client.Shutdown(SocketShutdown.Both); 48 //Release the socket 49 client.Close(); 50 Console.ReadLine(); 51 } 52 catch (Exception e) 53 { 54 Console.WriteLine(e.ToString()); 55 } 56 } 57 public static void ConnectCallback(IAsyncResult ar) 58 { 59 try 60 { 61 //从state对象获取Socket 62 Socket client = (Socket)ar.AsyncState; 63 //完成连接 64 client.EndConnect(ar); 65 Console.WriteLine("Socket connected to {0}", client.RemoteEndPoint.ToString()); 66 connectDone.Set(); 67 } 68 catch (Exception e) 69 { 70 Console.WriteLine(e.ToString()); 71 } 72 73 } 74 private static void Receive(Socket client) 75 { 76 try 77 { 78 //构造容器state 79 StateObject state = new StateObject(); 80 state.newsocket = client; 81 //从远程目标接收数据 82 client.BeginReceive(state.buffer,0,StateObject.bufferSize,0,new AsyncCallback(ReceiveCallback),state); 83 } 84 catch(Exception e) 85 { 86 Console.WriteLine(e.ToString()); 87 } 88 } 89 private static void ReceiveCallback(IAsyncResult ar) 90 { 91 try 92 { 93 //从输入参数异步state对象中获取state和socket对象 94 StateObject state = (StateObject)ar.AsyncState; 95 Socket client = state.newsocket; 96 int bytesRead = client.EndReceive(ar); 97 if (bytesRead > 0) 98 { 99 //有数据存储 100 state.sb.Append(BitConverter.ToString(state.buffer, 0, bytesRead)); 101 //继续读取 102 client.BeginReceive(state.buffer, 0, StateObject.bufferSize, 0, new AsyncCallback(ReceiveCallback), state); 103 } 104 else 105 { 106 //所有数据读取完毕 107 if (state.sb.Length > 1) 108 { 109 response = state.sb.ToString(); 110 } 111 //所有数据读取完毕的指示信号 112 ReceiveDone.Set(); 113 } 114 } 115 catch (Exception e) 116 { 117 Console.WriteLine(e.ToString()); 118 } 119 } 120 private static void Send(Socket client, byte[] b) 121 { 122 //格式转换 123 byte[] byteData = b; 124 //开始发送数据到远程设备 125 client.BeginSend(byteData,0,byteData.Length,0,new AsyncCallback(SendCallback),client); 126 } 127 private static void SendCallback(IAsyncResult ar) 128 { 129 try 130 { 131 //从state对象中获取socket 132 Socket client = (Socket)ar.AsyncState; 133 //完成数据发送 134 int bytesSend = client.EndSend(ar); 135 Console.WriteLine("Send {0} bytes to sever",bytesSend); 136 //指示数据已经发送完成,主线程继续 137 sendDone.Set(); 138 } 139 catch (Exception e) 140 { 141 Console.WriteLine(e.ToString()); 142 } 143 } 144 public static int Main(string[] args) 145 { 146 StartClient(); 147 return 0; 148 } 149 } 150 }