本文实例为大家分享了c#基于sockets类实现tcp通讯的具体代码,供大家参考,具体内容如下
最终效果
tcpclient
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms; using system.net; using system.net.sockets; using system.threading; namespace tcpclient02 { public partial class form1 : form { public form1() { initializecomponent(); } socket socketsend; private void button1_click(object sender, eventargs e) { //create socket socketsend = new socket(addressfamily.internetwork, sockettype.stream, protocoltype.tcp); ipaddress ip = ipaddress.parse(textbox1.text); ipendpoint point = new ipendpoint(ip, convert.toint32(textbox2.text)); idinfo idinfo = new idinfo(); //read id number information //get the ip address and port number of the remote server socketsend.connect(point); showmessages("connection succeeded"); //start a new thread and keep receiving messages sent by the server thread th = new thread(recivemessages); th.isbackground = true; th.start(); } private void button2_click(object sender, eventargs e) { string str = textbox3.text.trim(); byte[] buffer = system.text.encoding.utf8.getbytes(str); socketsend.send(buffer); } void showmessages(string str) { textbox4.appendtext(str "\r\n"); } void recivemessages() { while (true) { byte[] buffer = new byte[1024 * 1024 * 3]; int r = socketsend.receive(buffer); if (r == 0) { break; } string s = encoding.utf8.getstring(buffer, 0, r); showmessages(socketsend.remoteendpoint ":" s); } } private void form1_load(object sender, eventargs e) { control.checkforillegalcrossthreadcalls = false; } } }
tcpserver
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms; using system.net.sockets; using system.net; using system.threading; namespace tcpserver { public partial class form1 : form { public form1() { initializecomponent(); } private void button1_click(object sender, eventargs e) { try { //创建一个负责监听的socket socket socketwatch = new socket(addressfamily.internetwork, sockettype.stream, protocoltype.tcp); //创建ip地址和端口号 //ipaddress ip = ipaddress.parse(textbox1.text); ipaddress ip = ipaddress.any; ipendpoint point = new ipendpoint(ip, convert.toint32(textbox2.text)); //让负责监听的socket绑定ip地址和端口号 socketwatch.bind(point); showmsg("监听成功"); //设置监听队列(某一时刻连接客户端的最大数目) socketwatch.listen(10); //线程执行的方法 thread th = new thread(listen); //服务器开始监听 th.isbackground = true; th.start(socketwatch); } catch { } } void showmsg(string str) { textbox3.appendtext(str "\r\n"); } ////// 等待客户端的连接 并且创建与之通信的socket /// /// socket socketsend; void listen(object o) { socket socketwatch = o as socket; //负责监听的socket 来接收客户端的连接 //创建跟客户端通信的socket while (true) { try { socketsend = socketwatch.accept(); showmsg(socketsend.remoteendpoint.tostring() "连接成功"); //开始一个新的线程不断接受客户端发送过来的消息 thread th = new thread(recive); th.isbackground = true; th.start(socketsend); } catch { } } } ////// 服务器不断接受客户端发送过来的消息 /// /// void recive(object o) { socket socketsend = o as socket; while (true) { try { //客户端连接成功后,服务器应该接收客户端发来的消息 byte[] buffer = new byte[1024 * 1024 * 2]; //实际接收到的有效字节数 int bytelen = socketsend.receive(buffer); if (bytelen == 0) { break; } string str = encoding.utf8.getstring(buffer, 0, bytelen); showmsg(socketsend.remoteendpoint ":" str); } catch { } } } private void textbox1_textchanged(object sender, eventargs e) { } private void form1_load(object sender, eventargs e) { control.checkforillegalcrossthreadcalls = false; } ////// 服务器给客户端发送消息 /// /// /// private void button3_click(object sender, eventargs e) { string str = textbox4.text; byte[] buffer = system.text.encoding.utf8.getbytes(str); socketsend.send(buffer); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。