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;
     }
 }
相关推荐
~央千澈~10 分钟前
抖音弹幕游戏开发之第12集:添加冷却时间机制·优雅草云桧·卓伊凡
java·服务器·前端
dinga1985102620 分钟前
mysql之联合索引
数据库·mysql
微风中的麦穗30 分钟前
【SQL Server 2019】企业级数据库系统—数据库SQL Server 2019保姆级详细图文下载安装完全指南
大数据·数据库·sqlserver·云计算·个人开发·运维必备·sqlserver2019
聂 可 以35 分钟前
Windows环境Git安装教程(下载Git安装包、安装Git、验证Git是否安装成功)
windows·git
无心水1 小时前
5、微服务快速启航:基于Pig与BladeX构建高可用分布式系统实战
服务器·分布式·后端·spring·微服务·云原生·架构
沃码1 小时前
【Lively Wallpaper 】插件开发:LivelyProperties.json 全流程实战教程
c#·json·livelywallpaper·视频壁纸
zjttsh1 小时前
MySQL加减间隔时间函数DATE_ADD和DATE_SUB的详解
android·数据库·mysql
顾北122 小时前
SpringCloud 系列 04:Gateway 断言 / 过滤器 / 限流 一站式落地指南
java·开发语言·数据库
禹凕2 小时前
MYSQL——基础知识(NULL 值处理)
数据库·mysql
码云数智-大飞2 小时前
SQL Server 无法启动?常见原因及详细解决方法指南
数据库