一步一步教你使用.net进行Socket通信3
一步一步教你使用.net进行Socket通信
新建一个类,名字为Server,用于侦听网络连接。
7、
在SocketFactory中声明一个私有变量
System.Threading.Thread _serverListenThread;
新建一个类,名字为Server,用于侦听网络连接。
+展开
62-C#
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}
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}
7、
在SocketFactory中声明一个私有变量
System.Threading.Thread _serverListenThread;
加支付宝好友偷能量挖...