首页 > 代码库 > SocketAsyncEventArgs
SocketAsyncEventArgs
SocketAsyncEventArgs是.net提供的关于异步socket类,封装了IOCP的使用,可以用它方便的实现NIO(non-blocking IO)
NIO对于提升某些场景下Server性能和吞吐量有很大益处,准备在服务框架中使用它来编写简易rpc的部分
不过例子还是感觉复杂了点,我只是需要知道SocketAsyncEventArgs本身如何使用而已,于是自行简化了一下:
1: using System;
2: using System.Net;
3: using System.Net.Sockets;
4: using System.Text;
5:
6: namespace SocketAsyncServer
7: {
8: public static class Program
9: {
10: public static void Main(String[] args)
11: {
12: IPAddress[] addressList = Dns.GetHostEntry(Environment.MachineName).AddressList;
13: new TcpListener().Listen(new IPEndPoint(addressList[addressList.Length - 1], 9900));
14:
15: Console.ReadKey();
16: }
17: }
18:
19: public class TcpListener
20: {
21: private SocketAsyncEventArgs Args;
22: private Socket ListenerSocket;
23: private StringBuilder buffers;
24: public TcpListener() { }
25: public void Listen(EndPoint e)
26: {
27: //buffer
28: buffers = new StringBuilder();
29: //socket
30: ListenerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
31: ListenerSocket.Bind(e);
32: ListenerSocket.Listen(10);
33: //异步socket事件
34: Args = new SocketAsyncEventArgs();
35: Args.Completed += new EventHandler<SocketAsyncEventArgs>(ProcessAccept);
36: BeginAccept(Args);
37: Console.WriteLine("server run at {0}", e.ToString());
38: }
39:
40: //开始接受
41: void BeginAccept(SocketAsyncEventArgs e)
42: {
43: e.AcceptSocket = null;
44: if (!ListenerSocket.AcceptAsync(e))
45: ProcessAccept(ListenerSocket, e);
46: }
47: //接受完毕 开始接收和发送
48: void ProcessAccept(object sender, SocketAsyncEventArgs e)
49: {
50: Socket s = e.AcceptSocket;
51: e.AcceptSocket = null;
52:
53: int bufferSize = 10;//1000 * 1024;
54: var args = new SocketAsyncEventArgs();
55: args.Completed += new EventHandler<SocketAsyncEventArgs>(OnIOCompleted);
56: args.SetBuffer(new byte[bufferSize], 0, bufferSize);
57: args.AcceptSocket = s;
58: if (!s.ReceiveAsync(args))
59: this.ProcessReceive(args);
60:
61: BeginAccept(e);
62: }
63:
64: //IOCP回调
65: void OnIOCompleted(object sender, SocketAsyncEventArgs e)
66: {
67: switch (e.LastOperation)
68: {
69: case SocketAsyncOperation.Receive:
70: this.ProcessReceive(e);
71: break;
72: case SocketAsyncOperation.Send:
73: this.ProcessSend(e);
74: break;
75: default:
76: throw new ArgumentException("The last operation completed on the socket was not a receive or send");
77: }
78: }
79:
80: //接收完毕
81: void ProcessReceive(SocketAsyncEventArgs e)
82: {
83: if (e.BytesTransferred > 0)
84: {
85: if (e.SocketError == SocketError.Success)
86: {
87: //读取
88: var data=http://www.mamicode.com/Encoding.ASCII.GetString(e.Buffer, e.Offset, e.BytesTransferred);
89: buffers.Append(data);
90: Console.WriteLine("Received:{0}", data);
91:
92: if (e.AcceptSocket.Available == 0)
93: {
94: //读取完毕
95: Console.WriteLine("Receive Complete.Data:{0}", buffers.ToString());
96: //重置
97: buffers = new StringBuilder();
98: //发送反馈
99: Byte[] sendBuffer = Encoding.ASCII.GetBytes("result from server");
100: e.SetBuffer(sendBuffer, 0, sendBuffer.Length);
101: if (!e.AcceptSocket.SendAsync(e))
102: {
103:
104: this.ProcessSend(e);
105: }
106: }
107: else if (!e.AcceptSocket.ReceiveAsync(e))
108: {
109: this.ProcessReceive(e);
110: }
111: }
112: else
113: {
114: //this.ProcessError(e);
115: }
116: }
117: else
118: {
119: //this.CloseClientSocket(e);
120: }
121: }
122: //发送完毕
123: void ProcessSend(SocketAsyncEventArgs e)
124: {
125: if (e.SocketError == SocketError.Success)
126: {
127:
128: if (!e.AcceptSocket.ReceiveAsync(e))
129: {
130: this.ProcessReceive(e);
131: }
132: }
133: else
134: {
135:
136: }
137: }
138: }
139:
140: }
上述代码run起来之后,打开cmd用telnet测试下即可
telnet 127.0.0.1 9900
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。