C#的TCP服务器编写
.Net支持异步方式进行网络代码的编写,这样减少了线程的管理的使用。但对性能的改进还没有实际测试。下面是一个简单的TCP服务器编写,但对于接收数据的操作,我发觉只能用线程进行:
以上就是编写TCP服务器的基本模式,不知还有谁有一些更好的模式。这里的TCP接收连接,发送请求等都可以使用异步方式进行。
1
public partial class MainForm : Form {
2
private Socket serSock;
3
private Dictionary<IPEndPoint, Socket> clientSockList;
4![](/Images/OutliningIndicators/InBlock.gif)
5
public MainForm() {
6
InitializeComponent();
7
}
8![](/Images/OutliningIndicators/InBlock.gif)
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![](/Images/OutliningIndicators/InBlock.gif)
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![](/Images/OutliningIndicators/InBlock.gif)
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
}
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
2
![](/Images/OutliningIndicators/InBlock.gif)
3
![](/Images/OutliningIndicators/InBlock.gif)
4
![](/Images/OutliningIndicators/InBlock.gif)
5
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
6
![](/Images/OutliningIndicators/InBlock.gif)
7
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
8
![](/Images/OutliningIndicators/InBlock.gif)
9
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
10
![](/Images/OutliningIndicators/InBlock.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
14
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/InBlock.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
25
![](/Images/OutliningIndicators/InBlock.gif)
26
![](/Images/OutliningIndicators/InBlock.gif)
27
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
28
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
29
![](/Images/OutliningIndicators/InBlock.gif)
30
![](/Images/OutliningIndicators/InBlock.gif)
31
![](/Images/OutliningIndicators/InBlock.gif)
32
![](/Images/OutliningIndicators/InBlock.gif)
33
![](/Images/OutliningIndicators/InBlock.gif)
34
![](/Images/OutliningIndicators/InBlock.gif)
35
![](/Images/OutliningIndicators/InBlock.gif)
36
![](/Images/OutliningIndicators/InBlock.gif)
37
![](/Images/OutliningIndicators/InBlock.gif)
38
![](/Images/OutliningIndicators/InBlock.gif)
39
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
40
![](/Images/OutliningIndicators/InBlock.gif)
41
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
42
![](/Images/OutliningIndicators/InBlock.gif)
43
![](/Images/OutliningIndicators/InBlock.gif)
44
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
45
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
46
![](/Images/OutliningIndicators/InBlock.gif)
47
![](/Images/OutliningIndicators/InBlock.gif)
48
![](/Images/OutliningIndicators/InBlock.gif)
49
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
50
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
以上就是编写TCP服务器的基本模式,不知还有谁有一些更好的模式。这里的TCP接收连接,发送请求等都可以使用异步方式进行。