凯发ag旗舰厅登录网址下载
收集整理的这篇文章主要介绍了
[c#] socket 通讯,一个简单的聊天窗口小程序
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
socket,这玩意,当时不会的时候,抄别人的都用不好,简单的一句话形容就是“笨死了”;也是很多人写的太复杂,不容易理解造成的。最近在搞erlang和c的通讯,也想试试erlang是不是可以和c#简单通讯,就简单的做了些测试用例,比较简单,觉得新手也可以接受。
1 using system;
2 using system.collections.generic;
3 using system.componentmodel;
4 using system.data;
5 using system.drawing;
6 using system.linq;
7 using system.text;
8 using system.windows.forms;
9 using system.net.sockets;
10 using system.net;
11 using system.threading;
12
13 namespace chatclient
14 {
15 public partial class form1 : form
16 {
17 private system.windows.forms.richtextbox richtextbox1;
18 private system.windows.forms.textbox textbox1;
19 private system.componentmodel.icontainer components =
null;
20
21 ///
22 /// 服务端侦听端口
23 ///
24 private const int _serverport =
8707;
25 ///
26 /// 客户端侦听端口
27 ///
28 private const int _clientport =
8708;
29 ///
30 /// 缓存大小
31 ///
32 private const int _buffersize =
1024;
33 ///
34 /// 服务器ip
35 ///
36 private const string serverip =
"172.17.47.199";
//手动修改为指定服务器ip
37
38 private thread thread;
39
40 private void form1_load(
object sender, eventargs e)
41 {
42 thread =
new thread(
new threadstart(
delegate { listenning(); }));
43 thread.start();
44 }
45
46 void textbox_keyup(
object sender, keyeventargs e)
47 {
48 if (e.keycode ==
keys.enter)
49 {
50 string msg;
51 if ((msg = textbox1.text.trim()).length >
0)
52 {
53 sendmessage(msg);
54 }
55 textbox1.clear();
56 }
57 }
58
59 void sendmessage(
string msg)
60 {
61 try
62 {
63 socket socket =
new socket(addressfamily.internetwork, sockettype.stream, protocoltype.tcp);
64 ipendpoint endpoint =
new ipendpoint(ipaddress.parse(serverip), _serverport);
65 socket.connect(endpoint);
66 byte[] buffer =
system.text.asciiencoding.utf8.getbytes(msg);
67 socket.send(buffer);
68 socket.close();
69 }
70 catch
71 { }
72 }
73
74 void listenning()
75 {
76 tcplistener listener =
new tcplistener(_clientport);
77 listener.start();
78 while (
true)
79 {
80 socket socket =
listener.acceptsocket();
81 byte[] buffer =
new byte[_buffersize];
82 socket.receive(buffer);
83 int lastnullindex = _buffersize -
1;
84 while (
true)
85 {
86 if (buffer[lastnullindex] !=
'\0')
87 break;
88 lastnullindex--
;
89 }
90 string msg = encoding.utf8.getstring(buffer,
0, lastnullindex
1);
91 writeline(msg);
92 socket.close();
93 }
94 }
95
96 void writeline(
string msg)
97 {
98 this.invoke(
new methodinvoker(
delegate
99 {
100 this.richtextbox1.appendtext(msg
environment.newline);
101 }));
102 }
103
104 public form1()
105 {
106 this.richtextbox1 =
new system.windows.forms.richtextbox();
107 this.textbox1 =
new system.windows.forms.textbox();
108 this.suspendlayout();
109 //
110 // richtextbox1
111 //
112 this.richtextbox1.location =
new system.drawing.point(
1,
2);
113 this.richtextbox1.name =
"richtextbox1";
114 this.richtextbox1.size =
new system.drawing.size(
282,
188);
115 this.richtextbox1.tabindex =
3;
116 this.richtextbox1.text =
"";
117 this.richtextbox1.keyup =
new system.windows.forms.keyeventhandler(
this.textbox_keyup);
118 //
119 // textbox1
120 //
121 this.textbox1.location =
new system.drawing.point(
3,
196);
122 this.textbox1.multiline =
true;
123 this.textbox1.name =
"textbox1";
124 this.textbox1.size =
new system.drawing.size(
277,
65);
125 this.textbox1.tabindex =
2;
126 this.textbox1.keyup =
new system.windows.forms.keyeventhandler(
this.textbox_keyup);
127 //
128 // form1
129 //
130 this.autoscaledimensions =
new system.drawing.sizef(6f, 12f);
131 this.autoscalemode =
system.windows.forms.autoscalemode.font;
132 this.clientsize =
new system.drawing.size(
284,
262);
133 this.controls.add(
this.richtextbox1);
134 this.controls.add(
this.textbox1);
135 this.name =
"form1";
136 this.text =
"form1";
137 this.load =
new system.eventhandler(
this.form1_load);
138 this.resumelayout(
false);
139 this.performlayout();
140 }
141
142 protected override void dispose(
bool disposing)
143 {
144 if (disposing && (components !=
null))
145 {
146 components.dispose();
147 }
148 base.dispose(disposing);
149 }
150 }
151 }
client端 1 using system;
2 using system.collections.generic;
3 using system.componentmodel;
4 using system.data;
5 using system.diagnostics;
6 using system.linq;
7 using system.serviceprocess;
8 using system.text;
9 using system.net;
10 using system.net.sockets;
11 using system.collections;
12
13 namespace chatservice
14 {
15 public partial class service1 : servicebase
16 {
17 ///
18 /// 服务端侦听端口
19 ///
20 private const int _serverport =
8707;
21 ///
22 /// 客户端侦听端口
23 ///
24 private const int _clientport =
8708;
25 ///
26 /// 缓存大小
27 ///
28 private const int _buffersize =
1024;
29 ///
30 /// 服务器ip
31 ///
32 private const string serverip =
"172.17.47.199";
//手动修改为指定服务器ip
33 ///
34 /// 客户端ip
35 ///
36 //private hashtable serverips = new hashtable(); //存放ip port
37 private list<
string> serverips =
new list<
string>
();
38
39 public service1()
40 {
41 //initializecomponent(); //从控制台改为服务,需要注释下面的一行,打开本行
42 listenning();
43 }
44
45 void sendmessage(
string from, string msg)
46 {
47
48 for (int i = serverips.count - 1; i >= 0; i--)
49 {
50 try
51 {
52 socket socket = new socket(addressfamily.internetwork, sockettype.stream, protocoltype.tcp);
53 ipendpoint endpoint = new ipendpoint(ipaddress.parse(serverips[i]), _clientport);
54 socket.connect(endpoint);
55 byte[] buffer = system.text.asciiencoding.utf8.getbytes((from " say: " msg).trim());
56 socket.send(buffer);
57 socket.close();
58 }
59 catch
60 {
61 serverips.removeat(i);
62 }
63 }
64 }
65
66 void listenning()
67 {
68
69 tcplistener listener = new tcplistener(_serverport);
70 listener.start();
71 while (true)
72 {
73 socket socket = listener.acceptsocket();
74 var s = socket.remoteendpoint.tostring();
75 var ipstr = s.substring(0, s.indexof(":"));
76
77 if (!serverips.contains(ipstr))
78 {
79 serverips.add(ipstr);
80 }
81
82 byte[] buffer = new byte[_buffersize];
83 socket.receive(buffer);
84 int lastnullindex = _buffersize - 1;
85
86 while (true)
87 {
88 if (buffer[lastnullindex] != '\0')
89 break;
90 lastnullindex--;
91 }
92 string msg = encoding.utf8.getstring(buffer, 0, lastnullindex 1);
93 sendmessage(ipstr,msg);
94 console.writeline(msg);//服务模式下关闭
95 }
96 }
97
98 protected override void onstart(string[] args)
99 {
100 listenning();
101 }
102
103 protected override void onstop()
104 {
105
106 }
107 }
108 } server端
逻辑视图(比较简单):
1.左上角的是一个远程客户端,右上角是本地客户端,右下角是本地服务端(暂时改为控制台程序)
2.这段程序只给不懂得如何用socket玩玩
3.这里面剔除了数据加密、异步获取等繁杂的过程,想要学习可以参照:http://yunpan.cn/ckt2zhdkrmilz 访问密码 b610
转载于:https://www.cnblogs.com/preacher/p/4287793.html
总结
以上是凯发ag旗舰厅登录网址下载为你收集整理的[c#] socket 通讯,一个简单的聊天窗口小程序的全部内容,希望文章能够帮你解决所遇到的问题。
如果觉得凯发ag旗舰厅登录网址下载网站内容还不错,欢迎将凯发ag旗舰厅登录网址下载推荐给好友。