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;
     }
 }
相关推荐
qq_33003799几秒前
uni-app怎么实现App端蓝牙搜索与连接 uni-app低功耗蓝牙开发【代码】
jvm·数据库·python
SilentSamsara几秒前
ConfigMap 与 Secret:配置注入的四种姿势与安全边界
linux·运维·服务器·安全·微服务·kubernetes·k8s
qq_654366981 分钟前
SQL中如何通过JOIN实现级联删除_利用外键约束与JOIN辅助
jvm·数据库·python
2301_764150562 分钟前
layui form禁用表单元素 layui如何disable一个输入框
jvm·数据库·python
Lentou3 分钟前
部署项目之systemd部署
linux·运维·服务器
赛恩斯5 分钟前
adb 的源代码分析,以及如何改造为外网远程连接的方式
数据库·adb
weixin_458580125 分钟前
CSS如何为Bootstrap模态框增加进入动画_利用transition实现
jvm·数据库·python
2401_827560207 分钟前
【电脑和手机系统】win11镜像离线修复命令(六)
windows
minxihou9 分钟前
Windows 11 OpenSSH Server 完整排错指南(微软账号场景)
windows·microsoft·ssh·openssh·troubleshooting·windowswsl2
公子小六9 分钟前
基于.NET的Windows窗体编程之WinForms数据表格
windows·c#·.net·winforms