C#聊天室②

客户端

桌面

cs 复制代码
 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

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;
     }
 }
相关推荐
这个DBA有点耶12 小时前
2026年分布式数据库有哪些主流选择?先评估这5个维度再决定
数据库·分布式·dba
这个DBA有点耶15 小时前
从MySQL 5.7到8.0:JSON查询性能差在哪?虚拟列索引vs多值索引怎么选
数据库·mysql·架构
胡写代码15 小时前
数据库审计字段,别再每张表各写一套了——我统一成这 6 个字段
数据库
JustCheng17 小时前
Dispacher(1/2)深入解析-详细使用
c#
SelectDB19 小时前
为什么 JSON 正在成为分析数据库新的竞争点?
数据库·json·agent
ClouGence20 小时前
开源数据库管理工具 CloudDM 4.2.0 发布,新增 GoldenDB、KingbaseES 等数据源
数据库·dba·devops
发霉的馒头20 小时前
ORA-00845: MEMORY_TARGET not supported on this system的解决方法
数据库
BreezeJiang20 小时前
从域名到 Node.js:一次云服务器请求经过了哪些层?
服务器
草莓熊Lotso1 天前
【Redis 初阶】Set 类型深度解析:去重集合的运算能力与实战场景
linux·网络·数据库·windows·redis·tcp/ip·缓存
煎饼皮皮侠1 天前
【设计】设计一个web版的数据库管理平台后端(之五) --借鉴mybatis
数据库·mybatis