首页 > 作文

C#基于Sockets类实现TCP通讯

更新时间:2023-04-04 21:09:27 阅读: 评论:0

本文实例为大家分享了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 socketnd;    private void button1_click(object nder, eventargs e)    {      //create socket      socketnd = new socket(addressfamily.internetwork, sockettype.stream, protocoltype.tcp);      ipaddress ip = ipaddress.par(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 rver      socketnd.connect(point);      showmessages("connection succeeded");      //start a new thread and keep receiving messages nt by the rver      thread th = new thread(recivemessage什么是温度s);      th.isbackground = true;      th.start();    }    private void button2_click(object nder, eventargs e)    {      string str = textbox3.text.trim();      byte[] buffer = system.text.encoding.utf8.getbytes(str);      socketnd.nd(buffer);    }    void showmessages(string str)    {      textbo七绝诗x4.appendtext(str + "\r\n");    }    void recivemessages()    {      while (true)      {        byte[] buffer = new byte[1024 * 1024 * 3];        int r = socketnd.receive(buffer);        if (r == 0)        {          break;        }        string s = encoding.utf8.getstring(buffer, 0, r);        showmessages(socketnd.remoteendpoint + ":" + s);      }    }    private void fo天宝乐rm1_load(object nder, eventargs e)    {      control.checkforillegalcrossthreadcalls = f风力水车al;    }  }}

tcprver

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 tcprver{  public partial class form1 : form  {    public form1()    {      initializecomponent();    }    private void button1_click(object nder, eventargs e)    {      try      {        //创建一个负责监听的socket        socket socketwatch = new socket(addressfamily.internetwork, sockettype.stream, protocoltype.tcp);        //创建ip地址和端口号        //ipaddress ip = ipaddress.par(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 socketnd;    void listen(object o)    {      socket socketwatch = o as socket;      //负责监听的socket 来接收客户端的连接       //创建跟客户端通信的socket      while (true)      {        try        {          socketnd = socketwatch.accept();   你是我的春天       showmsg(socketnd.remoteendpoint.tostring() + "连接成功");          //开始一个新的线程不断接受客户端发送过来的消息          thread th = new thread(recive);          th.isbackground = true;          th.start(socketnd);        }        catch        {        }      }    }    /// <summary>    /// 服务器不断接受客户端发送过来的消息    /// </summary>    /// <param name="o"></param>    void recive(object o)    {           socket socketnd = o as socket;      while (true)      {       try        {          //客户端连接成功后,服务器应该接收客户端发来的消息          byte[] buffer = new byte[1024 * 1024 * 2];          //实际接收到的有效字节数          int bytelen = socketnd.receive(buffer);          if (bytelen == 0)          {            break;          }          string str = encoding.utf8.getstring(buffer, 0, bytelen);          showmsg(socketnd.remoteendpoint + ":" + str);        }        catch        { }                    }    }    private void textbox1_textchanged(object nder, eventargs e)    {    }    private void form1_load(object nder, eventargs e)    {      control.checkforillegalcrossthreadcalls = fal;    }   /// <summary>   /// 服务器给客户端发送消息   /// </summary>   /// <param name="nder"></param>   /// <param name="e"></param>    private void button3_click(object nder, eventargs e)    {      string str = textbox4.text;      byte[] buffer = system.text.encoding.utf8.getbytes(str);      socketnd.nd(buffer);    }  }}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持www.887551.com。

本文发布于:2023-04-04 21:09:25,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/8cac4be7d58b35b3a548113c06a27053.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:C#基于Sockets类实现TCP通讯.doc

本文 PDF 下载地址:C#基于Sockets类实现TCP通讯.pdf

标签:客户端   服务器   消息   线程
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图