随着Web技术的发展,Socket通信逐渐被人们遗忘。然而最近Socket应用却又越来越多。尤其是中国移动,中国联通的短信网关就是基于Socket通迅,另外随着大家对MSN、QQ等IM工具通迅协议的研究。协议内容也随处都可以找到。想要制作自己的MSN、QQ客户端的用户也大有人在。但习惯了WEB开发和简单UI开发的程序员却在这些协议面前迷糊了。
.Net的System.Net.Sockets命名空间封装了大量Socket类。使用此命名空间可以通过简单的方法进行复杂的Sockets连接、通迅。下面我就一步步教大家建立一个基于System.Net.Sockets的通用类库,并基于此举几个例子说明如何使用这个类库。
1、 首先建立一个类库项目。项目命名为 SocketLibrary,并删除自动生成的Class1.cs
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
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
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
System.Threading.Thread _serverListenThread;
1public void StartServer(int port) {
2
3 TcpListener listener = new TcpListener(IPAddress.Any, port);
4
5 listener.Start();
6
7
8
9 Server server = new Server(listener);
10
11 _serverListenThread = new System.Threading.Thread(new System.Threading.ThreadStart(server.Start));
12
13 _serverListenThread.Start();
14
15}
16
1SocketLibrary.SocketFactory factory = new SocketLibrary.SocketFactory();
2
3factory.StartServer(1979);
4
再次执行程序。可以看到程序会运行到断点处。即开始监听网络连接了。
10、 下面我们另外启动一个VS.NET2003实例,建立一个项目SocketClientTest,并通过添加已存在的项目增加SocketLibrary,增加对此项目的引用。
1using System;
2
3using System.Net;
4
5using System.Net.Sockets;
6
7
8
9namespace SocketLibrary
10
11{
12
13
14
15 public class Client
16
17 {
18
19 public const int CONNECTTIMEOUT = 10;
20
21 public Connection _connection;
22
23 public Client()
24
25 {
26
27
28
29 }
30
31 public static Connection StartClient(IPAddress ipaddress,int port) {
32
33 TcpClient client = new TcpClient();
34
35 client.SendTimeout = CONNECTTIMEOUT;
36
37 client.ReceiveTimeout = CONNECTTIMEOUT;
38
39
40
41 client.Connect(ipaddress,port);
42
43 }
44
45 }
46
47}
48
1public Connection StartClient(IPAddress ip,int port)
2
3{
4
5 return Client.StartClient(ip,port);
6
7}
8
public static System.Text.Encoding DefaultEncoding = System.Text.Encoding.GetEncoding("GB2312");
public static void SendMessage(string message,Connection connection) {
byte buffer = DefaultEncoding.GetBytes(message);
connection.NetworkStream.Write(buffer,0,buffer.Length);
}
public void Listenning() {
while(true) {
System.Threading.Thread.Sleep(200);
foreach(Connection connection in this._connections) {
if(connection.NetworkStream.CanRead && connection.NetworkStream.DataAvailable) {
byte buffer = new byte[1024];
int count = connection.NetworkStream.Read(buffer,0,buffer.Length);
Console.Write(SocketFactory.DefaultEncoding.GetString(buffer,0,count));
}
}
}
}
1public void StartListen() {
2
3 _listenningthread = new System.Threading.Thread(new System.Threading.ThreadStart(Listenning));
4
5 _listenningthread.Start();
6
7}
8
9private System.Threading.Thread _listenningthread;
10