C#心跳机制客户端

窗体(richTextBox右显示聊天)

步骤

点击链接按钮
         tcpclient客户端步骤
         1创建客户端对象
         2连接服务器connect
         3创建网络基础流发消息 .write发消息
         4 创建网络基础流接消息 .read接消息
         5 断开连接close()

窗体代码

namespace _02_心跳机制客户端
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        TcpClient client;
        private void button1_Click(object sender, EventArgs e)
        {
            if (button1.Text == "连接")
            {
                try
                {
                    client = new TcpClient();
                    client.Connect(comboBox1.Text, int.Parse(comboBox2.Text));
                    button1.Text = "断开";
                    StartRead();
                    HeartBeat();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("连接失败");
                }
            }
            else
            {
                client.Close();
                timer.Stop();
                button1.Text = "连接";
            }
        }
        void StartRead()
        {
            byte[] bs = new byte[1024];
            Task.Run(() =>
            {
                try
                {
                    while (true)
                    {
                        int count = client.GetStream().Read(bs,0,bs.Length);
                        string msg = Encoding.UTF8.GetString(bs, 0, count);
                        richTextBox1.Invoke((Action)(() =>
                        {
                            richTextBox1.AppendText(msg + "\t\n");
                        }));
                    }
                }
                catch (Exception ex)
                {

                    button1.Text = "连接";
                }
            });
        }
        Timer timer;
        void HeartBeat()
        {
            timer = new Timer();
            timer.Interval = 10000;
            timer.Tick += Timer_Tick;
            timer.Start();
        }
        private void Timer_Tick(object sender, EventArgs e)
        {
            client.GetStream().Write(new byte[] { 1 }, 0, 1);
        }
        private void button2_Click(object sender, EventArgs e)
        {
            byte[] bs = Encoding.UTF8.GetBytes(textBox1.Text);
            byte[] bs1 = new byte[bs.Length + 1];
            bs1[0] = 0;
            bs.CopyTo(bs1,1);
            client.GetStream().Write(bs1, 0, bs1.Length) ;
        }
    }
}

早岁已知世事艰

相关推荐

  1. C#心跳机制的服务器(完整)

    2024-06-19 09:52:03       7 阅读
  2. C++客户服务器TCP创建

    2024-06-19 09:52:03       30 阅读
  3. C#实现简单同步Echo服务客户

    2024-06-19 09:52:03       18 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-19 09:52:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-19 09:52:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-19 09:52:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-19 09:52:03       18 阅读

热门阅读

  1. HTML(6)——表单

    2024-06-19 09:52:03       8 阅读
  2. 【数据结构】练习集

    2024-06-19 09:52:03       7 阅读
  3. template标签

    2024-06-19 09:52:03       8 阅读
  4. Springboot应用设置跳过SSL证书认证

    2024-06-19 09:52:03       9 阅读
  5. MySQL-DML-约束

    2024-06-19 09:52:03       8 阅读
  6. 研导AI写作:辅助创作的未来伙伴

    2024-06-19 09:52:03       7 阅读
  7. vue3基础

    2024-06-19 09:52:03       10 阅读
  8. C++ 设计模式

    2024-06-19 09:52:03       8 阅读
  9. 19、架构-虚拟化容器

    2024-06-19 09:52:03       9 阅读
  10. python 数据清洗基础教程

    2024-06-19 09:52:03       6 阅读