随着Web技术的发展,Socket通信逐渐被人们遗忘。然而最近Socket应用却又越来越多。尤其是中国移动,中国联通的短信网关就是基于Socket通迅,另外随着大家对MSN、QQ等IM工具通迅协议的研究。协议内容也随处都可以找到。想要制作自己的MSN、QQ客户端的用户也大有人在。但习惯了WEB开发和简单UI开发的程序员却在这些协议面前迷糊了。
.net的System.Net.Sockets命名空间封装了大量Socket类。使用此命名空间可以通过简单的方法进行复杂的Sockets连接、通迅。下面我就一步步教大家建立一个基于System.Net.Sockets的通用类库,并基于此举几个例子说明如何使用这个类库。
1、 首先建立一个类库项目。项目命名为 SocketLibrary,并删除自动生成的Class1.cs
2、 在SocketLibrary中添加类:SocketFactory.cs
3、 在默认解决方案中增加一个Windows项目SocketServerTest用于测试服务器端。
并添加对SocketLibrary的引用。将此项目设为启动项目
4、 在SocketLibrary项目中新建类Connection。表示一个连接,增加两个属性NetWorkStream和ConnectionName。分别表示一个连接的名字和它包含的NetWorkStream。源代码如下:
1using System;
3using System.Net;
5using System.Net.Sockets;
6
7
8
9namespace SocketLibrary
10
11{
12
13 public class Connection
14
15 {
16
17 public NetworkStream NetworkStream {
18
19 get{return _networkStream;}
20
21 set{_networkStream = value;}
22
23 }
24
25 private NetworkStream _networkStream;
26
27 public string ConnectionName {
28
29 get{return _connectionName;}
30
31 set{_connectionName = value;}
32
33 }
34
35 private string _connectionName;
36
37 public Connection(NetworkStream networkStream,string connectionName)
38
39 {
40
41 this._networkStream = networkStream;
42
43 this._connectionName = connectionName;
44
45 }
46
47 public Connection(NetworkStream networkStream):this(networkStream,string.Empty) {
48
49 }
50
51 }
52
53}
54
3using System.Net;
5using System.Net.Sockets;
6
7
8
9namespace SocketLibrary
10
11{
12
13 public class Connection
14
15 {
16
17 public NetworkStream NetworkStream {
18
19 get{return _networkStream;}
20
21 set{_networkStream = value;}
22
23 }
24
25 private NetworkStream _networkStream;
26
27 public string ConnectionName {
28
29 get{return _connectionName;}
30
31 set{_connectionName = value;}
32
33 }
34
35 private string _connectionName;
36
37 public Connection(NetworkStream networkStream,string connectionName)
38
39 {
40
41 this._networkStream = networkStream;
42
43 this._connectionName = connectionName;
44
45 }
46
47 public Connection(NetworkStream networkStream):this(networkStream,string.Empty) {
48
49 }
50
51 }
52
53}
54
5、 新建一个继承自CollectionBase的类ConnectionCollection。用于保存Connection集合。
1using System;
2
3
4
5namespace SocketLibrary {
6
7 public class ConnectionCollection:System.Collections.CollectionBase {
8
9 public ConnectionCollection() {
10
11
12
13 }
14
15 public void Add(Connection value) {
16
17 List.Add(value);
18
19 }
20
21 public Connection this[int index] {
22
23 get {
24
25 return List[index] as Connection;
26
27 }
28
29 set{
30
31 List[index] = value;
32
33 }
34
35 }
36
37 public Connection this[string connectionName] {
38
39 get {
40
41 foreach(Connection connection in List) {
42
43 if(connection.ConnectionName == connectionName)
44
45 return connection;
46
47 }
48
49 return null;
50
51 }
52
53 }
54
55 }
56
57}
58
2
3
4
5namespace SocketLibrary {
6
7 public class ConnectionCollection:System.Collections.CollectionBase {
8
9 public ConnectionCollection() {
10
11
12
13 }
14
15 public void Add(Connection value) {
16
17 List.Add(value);
18
19 }
20
21 public Connection this[int index] {
22
23 get {
24
25 return List[index] as Connection;
26
27 }
28
29 set{
30
31 List[index] = value;
32
33 }
34
35 }
36
37 public Connection this[string connectionName] {
38
39 get {
40
41 foreach(Connection connection in List) {
42
43 if(connection.ConnectionName == connectionName)
44
45 return connection;
46
47 }
48
49 return null;
50
51 }
52
53 }
54
55 }
56
57}
58
6、
新建一个类,名字为Server,用于侦听网络连接。
1using System;
2
3using System.Net;
4
5using System.Net.Sockets;
6
7
8
9namespace SocketLibrary
10
11{
12
13 public class Server
14
15 {
16
17 public ConnectionCollection Connections {
18
19 get{return _connections;}
20
21 set{_connections = value;}
22
23 }
24
25 private ConnectionCollection _connections;
26
27
28
29 private TcpListener _listener;
30
31 public Server(TcpListener listener)
32
33 {
34
35 this._connections = new ConnectionCollection();
36
37 this._listener = listener;
38
39 }
40
41 public void Start() {
42
43 while(true) {
44
45 if(_listener.Pending()) {
46
47 TcpClient client = _listener.AcceptTcpClient();
48
49 NetworkStream stream = client.GetStream();
50
51 this._connections.Add(new Connection(stream));
52
53 }
54
55 }
56
57 }
58
59 }
60
61}
62
2
3using System.Net;
4
5using System.Net.Sockets;
6
7
8
9namespace SocketLibrary
10
11{
12
13 public class Server
14
15 {
16
17 public ConnectionCollection Connections {
18
19 get{return _connections;}
20
21 set{_connections = value;}
22
23 }
24
25 private ConnectionCollection _connections;
26
27
28
29 private TcpListener _listener;
30
31 public Server(TcpListener listener)
32
33 {
34
35 this._connections = new ConnectionCollection();
36
37 this._listener = listener;
38
39 }
40
41 public void Start() {
42
43 while(true) {
44
45 if(_listener.Pending()) {
46
47 TcpClient client = _listener.AcceptTcpClient();
48
49 NetworkStream stream = client.GetStream();
50
51 this._connections.Add(new Connection(stream));
52
53 }
54
55 }
56
57 }
58
59 }
60
61}
62
7、
在SocketFactory