C#的TCP服务器编写
.Net支持异步方式进行网络代码的编写,这样减少了线程的管理的使用。但对性能的改进还没有实际测试。下面是一个简单的TCP服务器编写,但对于接收数据的操作,我发觉只能用线程进行:
以上就是编写TCP服务器的基本模式,不知还有谁有一些更好的模式。这里的TCP接收连接,发送请求等都可以使用异步方式进行。
1public partial class MainForm : Form {
2 private Socket serSock;
3 private Dictionary<IPEndPoint, Socket> clientSockList;
4
5 public MainForm() {
6 InitializeComponent();
7 }
8
9 /// <summary>
10 /// 开始监听事件处理
11 /// </summary>
12 /// <param name="sender"></param>
13 /// <param name="e"></param>
14 private void btnStart_Click(object sender, EventArgs e) {
15 serSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
16 EndPoint ep = new IPEndPoint(IPAddress.Any, 8900);
17 serSock.Bind(ep);
18 serSock.Listen(30);
19 clientSockList = new Dictionary<IPEndPoint, Socket>();
20 serSock.BeginAccept(new AsyncCallback(AcceptEnd), null);
21 btnStart.Enabled = false;
22 }
23
24 /// <summary>
25 /// 接收客户机连接的方法
26 /// </summary>
27 /// <param name="ar"></param>
28 private void AcceptEnd(IAsyncResult ar) {
29 Socket sock = serSock.EndAccept(ar);
30 IPEndPoint ep = sock.RemoteEndPoint as IPEndPoint;
31 clientSockList[ep] = sock;
32 btnSend.Enabled = true;
33 btnSendFile.Enabled = true;
34 uList.Items.Add(sock.RemoteEndPoint);
35 Thread t = new Thread(new ParameterizedThreadStart(RecThread));
36 t.IsBackground = true;
37 t.Start(sock);
38 serSock.BeginAccept(new AsyncCallback(AcceptEnd), null); //继续接收下一个客户机连接
39 }
40
41 /// <summary>
42 /// 接收线程
43 /// </summary>
44 /// <param name="o"></param>
45 private void RecThread(object o) {
46 Socket clientSock = o as Socket;
47 NetworkStream ns = new NetworkStream(clientSock);
48 //对数据的接收写在这里
49 }
50}
2 private Socket serSock;
3 private Dictionary<IPEndPoint, Socket> clientSockList;
4
5 public MainForm() {
6 InitializeComponent();
7 }
8
9 /// <summary>
10 /// 开始监听事件处理
11 /// </summary>
12 /// <param name="sender"></param>
13 /// <param name="e"></param>
14 private void btnStart_Click(object sender, EventArgs e) {
15 serSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
16 EndPoint ep = new IPEndPoint(IPAddress.Any, 8900);
17 serSock.Bind(ep);
18 serSock.Listen(30);
19 clientSockList = new Dictionary<IPEndPoint, Socket>();
20 serSock.BeginAccept(new AsyncCallback(AcceptEnd), null);
21 btnStart.Enabled = false;
22 }
23
24 /// <summary>
25 /// 接收客户机连接的方法
26 /// </summary>
27 /// <param name="ar"></param>
28 private void AcceptEnd(IAsyncResult ar) {
29 Socket sock = serSock.EndAccept(ar);
30 IPEndPoint ep = sock.RemoteEndPoint as IPEndPoint;
31 clientSockList[ep] = sock;
32 btnSend.Enabled = true;
33 btnSendFile.Enabled = true;
34 uList.Items.Add(sock.RemoteEndPoint);
35 Thread t = new Thread(new ParameterizedThreadStart(RecThread));
36 t.IsBackground = true;
37 t.Start(sock);
38 serSock.BeginAccept(new AsyncCallback(AcceptEnd), null); //继续接收下一个客户机连接
39 }
40
41 /// <summary>
42 /// 接收线程
43 /// </summary>
44 /// <param name="o"></param>
45 private void RecThread(object o) {
46 Socket clientSock = o as Socket;
47 NetworkStream ns = new NetworkStream(clientSock);
48 //对数据的接收写在这里
49 }
50}
以上就是编写TCP服务器的基本模式,不知还有谁有一些更好的模式。这里的TCP接收连接,发送请求等都可以使用异步方式进行。