我是靠谱客的博主 美满眼神,这篇文章主要介绍c#网络编程之服务器篇,现在分享给大家,希望可以做个参考。

上图:制作一个简单的服务器
在这里插入图片描述
原理就用这张图把,并严格按照这张图进行编写
在这里插入图片描述

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
Socket sk; private void button1_Click(object sender, EventArgs e) { sk = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); IPAddress ip = IPAddress.Parse(textBox1.Text); int port = Int32.Parse(textBox2.Text); //IPEndPoint iep = new IPEndPoint(ip,port); //绑定监听 sk.Bind(new IPEndPoint(ip,port)); //开始监听 sk.Listen(10);//允许10客户端连接 textBox3.AppendText("服务器开始监听。。。rn"); //while(true) //{ // //重新建立一个socket,进行客户端的连接 // Socket client= sk.Accept(); // //如果连接成功 // textBox3.AppendText("有一个客户端连接到服务器:"+client.RemoteEndPoint+"-->"+client.LocalEndPoint); //} //上面是很好,但是线程阻塞了,阻塞的地方在Accept这个地方 //所以上述地方必须另外开线程了,这样程序运行才会流畅 Thread td1 = new Thread(ClientConnect); td1.IsBackground = true;//设置线程为后台进行 td1.Start(); } Dictionary<string, Socket> dic = new Dictionary<string, Socket>(); void ClientConnect() { while (true) { //重新建立一个socket,进行客户端的连接 Socket client = sk.Accept(); //如果连接成功 textBox3.AppendText("有一个客户端连接到服务器:" + client.RemoteEndPoint + "-->" + client.LocalEndPoint+"rn");//此时报警有一个跨 线程调用,怎么办,在出事化的时候,直接不要检查就可以了 //接收数据 使用receive接收 //byte[] buffer = new byte[5 * 1024 * 1024]; //int length= client.Receive(buffer); //string msg = Encoding.Default.GetString(buffer,0,length); //textBox3.AppendText(msg+"rn"); //新的问题出来了,现在的情况是,只能接收一个连接的一条信息, //怎么样能解决这个问题呢 //再开线程试试 Thread td2 = new Thread(ReceiveSendMsg); td2.IsBackground = true; td2.Start(client); } //开线程后,在来进行尝试连接,完美 } void ReceiveSendMsg(object Client) { Socket client = Client as Socket; //如果有客户端连接的话,将客户端的连接信息放入list表中,同时将信息放入信息和对应之连接放入键值对中 dic.Add(client.RemoteEndPoint.ToString(),client); listBox1.Items.Add(client.RemoteEndPoint); //如果删除已将下线的客户端连接呢? //看下面 //while(true) //{ // byte[] buffer = new byte[5 * 1024 * 1024]; // int length =client.Receive(buffer); // string msg = Encoding.Default.GetString(buffer, 0, length); // textBox3.AppendText(msg + "rn"); //} //这样的确可以实现接收多个数据的问题,但是,当客户端掉线的时候,程序即可崩溃,怎么解决呢 //开下面改进代码 while (true) { byte[] buffer = new byte[5 * 1024 * 1024]; int length = client.Receive(buffer); if(length<=0) { // listBox1.Items.Remove(client.RemoteEndPoint); DeleConnection(client); //同时在键值对中也要删除连接 break; } //这样处理看行不 string msg = Encoding.Default.GetString(buffer, 0, length); textBox3.AppendText(client.RemoteEndPoint+":"+msg + "rn"); //完美解决,现在可以监听,多客户端的多语句了 } } string Key = null; void DeleConnection(Socket client) { //找到键值 foreach (var item in dic) { if (item.Value == client) { Key = item.Key; break; } } Key = client.RemoteEndPoint.ToString(); //删除列表和集合 //listBox1.Items.Remove(Key); listBox1.Items.Remove(client.RemoteEndPoint); dic.Remove(Key); Key = null; } private void button2_Click(object sender, EventArgs e) { //获取一个连接 try { Socket client = GetSocket(); string s = textBox4.Text.Trim(); if (s != "") { byte[] buffer = Encoding.Default.GetBytes(s); client.Send(buffer); } } catch { } } private Socket GetSocket() { string s = listBox1.SelectedItem.ToString() ; return dic[s]; }

最后

以上就是美满眼神最近收集整理的关于c#网络编程之服务器篇的全部内容,更多相关c#网络编程之服务器篇内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(61)

评论列表共有 0 条评论

立即
投稿
返回
顶部