C#基于Sockets类实现TCP通讯
程序员文章站
2022-06-10 10:16:39
本文实例为大家分享了c#基于sockets类实现tcp通讯的具体代码,供大家参考,具体内容如下最终效果tcpclientusing system;using system.collections.ge...
本文实例为大家分享了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"); } /// <summary> /// 等待客户端的连接 并且创建与之通信的socket /// </summary> /// 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 { } } } /// <summary> /// 服务器不断接受客户端发送过来的消息 /// </summary> /// <param name="o"></param> 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; } /// <summary> /// 服务器给客户端发送消息 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button3_click(object sender, eventargs e) { string str = textbox4.text; byte[] buffer = system.text.encoding.utf8.getbytes(str); socketsend.send(buffer); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
下一篇: C#实现计算器窗体程序
推荐阅读
-
使用C#实现基于TCP和UDP协议的网络通信程序的基本示例
-
基于Java的Socket类Tcp网络编程实现实时聊天互动程序:QQ聊天界面的搭建
-
基于Java的Socket类Tcp网络编程实现实时聊天互动程序(三):回车实现数据到发送(详细代码完结)
-
C#基于TCP实现简单游戏客户端的完整实例
-
C# WPF上位机实现和下位机TCP通讯的方法
-
基于Java的Socket类Tcp网络编程实现实时聊天互动程序(二):Tcp通信的过程及代码编写
-
C#基于Socket的TCP通讯例程 Server Client
-
C#基于SerialPort类实现串口通讯详解
-
C#基于Sockets类实现TCP通讯
-
使用C#实现基于TCP和UDP协议的网络通信程序的基本示例