Socket异步编程-之客户器端.
1 using System;
2 using System.Text;
3 using System.Net;
4 using System.Net.Sockets;
5 namespace asyncSocketClient {
6 class SocketClient {
7 static System.Threading.ManualResetEvent allDone=new System.Threading.ManualResetEvent(false);
8 [STAThread]
9 static void Main(string[] args) {
10 SocketClient.Connect();
11
12 }
13 //连接方法
14 public static void Connect() {
15 IPAddress ip=IPAddress.Parse("10.0.0.153");
16 IPEndPoint ipEP=new IPEndPoint(ip,11000);
17 Socket client=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
18 client.BeginConnect(ipEP,new AsyncCallback(SocketClient.ConnectCallback),client);
19 allDone.WaitOne();
20 }
21 //异步连接
22 public static void ConnectCallback(System.IAsyncResult ar) {
23 try {
24 Socket client=(Socket)ar.AsyncState;
25 client.EndConnect(ar);
26 Console.WriteLine("Socket connected to {0}",client.RemoteEndPoint.ToString());
27 Console.WriteLine("Press any key to send date to remote server");
28 Console.ReadLine();
29 SocketClient.Send(client,"abc123");
30 }
31 catch (Exception ex) {
32 Console.WriteLine(ex.Message);
33 }
34 }
35 //发送数据的方法
36 public static void Send(Socket client,string data) {
37 byte[] byteData=System.Text.Encoding.ASCII.GetBytes(data);
38 client.BeginSend(byteData,0,byteData.Length,0,new AsyncCallback(SocketClient.SendCallback),client);
39 }
40 //异步发送方法
41 public static void SendCallback(System.IAsyncResult ar) {
42 try {
43 Socket client=(Socket)ar.AsyncState;
44 int bytesSend=client.EndSend(ar);
45 Console.WriteLine("Send {0} bytes to server.",bytesSend);
46 }
47 catch (Exception ex) {
48 Console.WriteLine(ex.Message);
49 }
50 allDone.Set();
51 }
52
53 //异步接收与服务端异步接收一样.同理,服务端异常发送与客户端异步发送一样.
54 }
55 }
56
2 using System.Text;
3 using System.Net;
4 using System.Net.Sockets;
5 namespace asyncSocketClient {
6 class SocketClient {
7 static System.Threading.ManualResetEvent allDone=new System.Threading.ManualResetEvent(false);
8 [STAThread]
9 static void Main(string[] args) {
10 SocketClient.Connect();
11
12 }
13 //连接方法
14 public static void Connect() {
15 IPAddress ip=IPAddress.Parse("10.0.0.153");
16 IPEndPoint ipEP=new IPEndPoint(ip,11000);
17 Socket client=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
18 client.BeginConnect(ipEP,new AsyncCallback(SocketClient.ConnectCallback),client);
19 allDone.WaitOne();
20 }
21 //异步连接
22 public static void ConnectCallback(System.IAsyncResult ar) {
23 try {
24 Socket client=(Socket)ar.AsyncState;
25 client.EndConnect(ar);
26 Console.WriteLine("Socket connected to {0}",client.RemoteEndPoint.ToString());
27 Console.WriteLine("Press any key to send date to remote server");
28 Console.ReadLine();
29 SocketClient.Send(client,"abc123");
30 }
31 catch (Exception ex) {
32 Console.WriteLine(ex.Message);
33 }
34 }
35 //发送数据的方法
36 public static void Send(Socket client,string data) {
37 byte[] byteData=System.Text.Encoding.ASCII.GetBytes(data);
38 client.BeginSend(byteData,0,byteData.Length,0,new AsyncCallback(SocketClient.SendCallback),client);
39 }
40 //异步发送方法
41 public static void SendCallback(System.IAsyncResult ar) {
42 try {
43 Socket client=(Socket)ar.AsyncState;
44 int bytesSend=client.EndSend(ar);
45 Console.WriteLine("Send {0} bytes to server.",bytesSend);
46 }
47 catch (Exception ex) {
48 Console.WriteLine(ex.Message);
49 }
50 allDone.Set();
51 }
52
53 //异步接收与服务端异步接收一样.同理,服务端异常发送与客户端异步发送一样.
54 }
55 }
56
一点说明:为什么在标题中要嵌入英文?原因是为了能够让国外的网友能查询到这篇文章。平常在Google上查资料的时候,经常参考国外网友的博客,帮助我解决了很多问题,所以我也想让他们能够参考我写的内容。当然文中我不可能全部译为英文,所以我尽量把代码粘全,靠代码说话吧。