C#聊天室②

客户端

桌面

 MyClient client;
 public Form1()
 {
     InitializeComponent();
 }
 // 进入聊天室按钮方法
 private void button1_Click(object sender, EventArgs e)
 {
     if (!string.IsNullOrEmpty(textBox1.Text))
     {
         // 开始连接服务器 封装一个自定义客户端类
         client = new MyClient(); 
         // 给client委托赋值updateLabel
         client.LabelInfo = updateLabel;
     }
     else
     {
         MessageBox.Show("请输入你的名字");
     }
 }
 public List<string> list1 = new List<string>() { "拼命加载中", "拼命加载中.", "拼命加载中..", "拼命加载中..." };
 int index = 0;
  // 封装一个更新label的方法
  public void updateLabel(string str)
 {
     this.Invoke((Action)(() =>
     {
         if (string.IsNullOrEmpty(str))// 正在连接中
         {
             label1.Text = list1[index];
             index++;
             if (index == list1.Count) index = 0;
         }
         else // 证明连接有结果时候
         {
             this.label1.Text = str;
             // 需要判断如果连接成功了 需要进入聊天室
            

         }
     }));
 }

MyClient.cs

 internal class MyClient
 {
     // 定义委托类型
     public delegate void UpdatLabelHandle(string str = "");
     // 声明委托变量
     public UpdatLabelHandle LabelInfo;


     // 接受和发送消息 创建连接对象写在异步里面
     Thread thConnect; // 连接服务器的线程
     TcpClient client;  // 全局客户端对象

     public bool IsConnect;
     public MyClient() 
     {
         thConnect = new Thread(ConnetServer);
         thConnect.Start();
     }
     public void ConnetServer()
     {
         client = new TcpClient();
         // 开启一个异步的连接
         // 参数1 ip地址
         // 2 端口号
         // 3 回调函数 看一判断是否连接成功
         // 4 传递回调函数的参数

         client.BeginConnect(IPAddress.Parse("192.168.107.72"),3333,requestCallBack,client);
         float num = 0;
         while (IsConnect == false)
         {
             // 证明没有连接成功
             num += 0.1f;
             if (LabelInfo != null) LabelInfo(); // 不传参数的目的
             if (num >= 10f)
             {
                 return;//超时连接 10s连接不上就连接失败
             }
             Thread.Sleep(100);
         }
     }
     // IAsyncResult 异步结果的类
     // BeginConnect 的回调函数 不管成功与否都执行
     public void requestCallBack(IAsyncResult ar)
     {
         TcpClient t = ar.AsyncState as TcpClient;// 通过AsyncState异步状态属性获取参数
         if (t.Connected) // 如果连接成功了
         {
             IsConnect = true;
             LabelInfo("连接成功");
             t.EndConnect(ar); // 结束挂起的状态
         }
         else
         {
             //  连接失败 
             LabelInfo("连接失败");
         }
         LabelInfo = null;
     }
 }

相关推荐

  1. NIO案例-聊天

    2024-06-14 13:16:01       50 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-14 13:16:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-14 13:16:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-06-14 13:16:01       87 阅读
  4. Python语言-面向对象

    2024-06-14 13:16:01       96 阅读

热门阅读

  1. 【TensorFlow深度学习】DQN(Deep Q-Network)算法详解

    2024-06-14 13:16:01       23 阅读
  2. Linux 系统中的 /etc/issue and /etc/issue.net 文件

    2024-06-14 13:16:01       75 阅读
  3. 深入探索Spring Boot的自动配置机制

    2024-06-14 13:16:01       41 阅读
  4. Kafka 如何保证消息顺序及其实现示例

    2024-06-14 13:16:01       35 阅读
  5. Day37

    2024-06-14 13:16:01       21 阅读
  6. 二分查找详解

    2024-06-14 13:16:01       38 阅读
  7. 蓝中面试(一)

    2024-06-14 13:16:01       33 阅读