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;
     }
 }
相关推荐
马克学长7 分钟前
SSM学生出国境学习交流管理87153(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面
数据库·学习·ssm 框架·学生出国境管理·在线申请
么么...11 分钟前
MySQL 存储引擎与索引深度解析:从原理到优化实践
数据库·经验分享·sql·mysql
小宇的天下12 分钟前
Synopsys Technology File and Routing Rules Reference Manual (1)
java·服务器·前端
さかた ぎんとき8826 分钟前
从SQL到磁盘的Mysql全链路解析
数据库·sql·mysql
lph00926 分钟前
mqtt broker (mosquitto)创建服务器、订阅与发布
运维·服务器
それども32 分钟前
数据库读写分离和事务的关系
数据库
TGITCIC34 分钟前
2026数据分析Agent最新落地方向解析
数据库·数据分析·ai大模型·ai智能体·ai数据·ai问数·ai sql
wangqiaowq38 分钟前
SQL Server 对非聚簇索引的 INCLUDE 列数量和大小有限制
数据库
酒醉的胡铁43 分钟前
uniapp运行到鸿蒙证书配置
服务器·uni-app·harmonyos
Coder_Boy_1 小时前
基于SpringAI的在线考试系统-核心业务流程图
java·数据库·spring boot·软件工程