wpf mvvm框架下创建一个TCP 客户端

在C#中,可以使用TcpClient类来创建一个TCP客户端,并接收字符串数据。在wpf mvvm框架下,我们创建一个ViewModel,在项目中添加类:
在这里插入图片描述
之后具体代码如下:

using Newtonsoft.Json;
using Prism.Commands;
using QZ.General;
using System;
using System.ComponentModel;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

namespace JV_SFC.ViewModels
{
   
    public class TCPClientViewmodel:BaseNotify
    {
   
        private TcpClient visionClient = null;
        private int ErrorCount = 0;
        private bool Vision_ThreadRunFlag = true;
        private string _IpAddress = "127.0.0.1";
        [Description("IP")]
        public string IpAddress
        {
   
            get {
    return _IpAddress; }
            set {
    SetProperty(ref _IpAddress, value); }
        }

        private int _port;
        [Description("端口")]
        public int port
        {
   
            get {
    return _port; }
            set {
    SetProperty(ref _port, value); }
        }
        
        private bool _visionConnected = false;
        [Description("是否为连接状态")]
        [JsonIgnore]
        public bool visionConnected
        {
   
            get {
    return _visionConnected; }
            set {
    SetProperty(ref _visionConnected, value); }
        }
        
        private DelegateCommand _visionConnetion;
        [JsonIgnore]
        [Description("客户端连接命令")]
        public DelegateCommand VisionConnetionCmd =>
            _visionConnetion ?? (_visionConnetion = new DelegateCommand(ExecuteVisionConnetionCmd));
        [Description("执行命令")]
        void ExecuteVisionConnetionCmd()
        {
   
            Vision_ThreadRunFlag = true;
            VisionClientInit(msg => {
    });
        }

        private DelegateCommand _Close;
        [JsonIgnore]
        [Description("断开客户端命令")]
        public DelegateCommand VisionDisConnectedCmd =>
            _Close ?? (_Close = new DelegateCommand(ExecuteCloseCommandName));
        [Description("执行命令")]
        void ExecuteCloseCommandName()
        {
   
            if (visionClient != null)
            {
   
                Vision_ThreadRunFlag = false;
                visionClient.Close();
                visionClient?.Dispose();
                visionConnected = false;
            }
        }

        /// <summary>
        /// 初始化客户端
        /// </summary>
        /// <param name="ReceiveMsgCallback"></param>
        public void VisionClientInit(Action<string> ReceiveMsgCallback)
        {
   
            Thread thread = new Thread(() =>
            {
   
                while (Vision_ThreadRunFlag)
                {
   
                    try
                    {
   
                        if (ErrorCount > 70)
                        {
   
                            MessageBox.Show("TCP客户端长时间连接不上视觉软件,中断连接!!");
                            break;
                        }
                        if (!visionConnected)
                        {
   
                            visionClient = new TcpClient();
                            visionClient.Connect(IpAddress, port);
                            Console.WriteLine("TCP客户端初始化,连接OK!");
                            visionConnected = visionClient.Connected;
                            NetworkStream stream = visionClient.GetStream();
                            byte[] buffer = new byte[1024];
                            int bytesRead;
                            while (visionConnected && (bytesRead = stream.Read(buffer, 0, buffer.Length)) != 0)
                            {
   
                                string responseData = Encoding.UTF8.GetString(buffer, 0, bytesRead);
                                ReceiveMsgCallback(responseData);
                                ErrorCount = 0;
                                Thread.Sleep(5);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
   
                        ErrorCount++;
                        visionConnected = false;
                        Console.WriteLine("异常:"+ex);                   
                        Task.Delay(1000);
                    }
                }
            });
            thread.Start();
            thread.IsBackground = true;
        }

        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool VisionClientSendData(string msg)
        {
   
            if (visionClient == null || !visionClient.Connected)
            {
   
                return false;
            }
            using (NetworkStream stream = visionClient.GetStream())
            {
   
                if (stream.CanWrite)
                {
   
                    byte[] buffer = Encoding.UTF8.GetBytes(msg);
                    stream.Write(buffer, 0, buffer.Length);
                }
            }
            return true;
        }
    }
}

代码通过不断从网络流中读取数据,这里做了断线重连的机制,可以在指定的时间退出。
当前端需要调用时,可以在前端绑定端口号和IP地址,Command绑定连接VisionConnetionCmd 和断开连接VisionDisConnectedCmd即可。

相关推荐

  1. netty创建tcp服务+客户

    2023-12-31 06:24:04       22 阅读
  2. C++客户服务器TCP创建

    2023-12-31 06:24:04       53 阅读
  3. TCP服务器和客户创建步骤

    2023-12-31 06:24:04       51 阅读
  4. TCP、UDP客户

    2023-12-31 06:24:04       40 阅读
  5. Python3 TCP 客户

    2023-12-31 06:24:04       49 阅读

最近更新

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

    2023-12-31 06:24:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-31 06:24:04       106 阅读
  3. 在Django里面运行非项目文件

    2023-12-31 06:24:04       87 阅读
  4. Python语言-面向对象

    2023-12-31 06:24:04       96 阅读

热门阅读

  1. 在vim中映射类似于Windows编辑器的快捷键

    2023-12-31 06:24:04       59 阅读
  2. vscode文章汇总

    2023-12-31 06:24:04       64 阅读
  3. [蓝桥杯 2018 ]激光样式

    2023-12-31 06:24:04       60 阅读
  4. HTML5 `<audio>` 面试题

    2023-12-31 06:24:04       59 阅读
  5. 20231230 SQL基础50题打卡

    2023-12-31 06:24:04       49 阅读
  6. 算法每日一题:保龄球游戏的获胜者

    2023-12-31 06:24:04       52 阅读
  7. 【AI】人工智能爆发推进器之生成对抗网络

    2023-12-31 06:24:04       55 阅读
  8. flask web学习之flask与http(二)

    2023-12-31 06:24:04       50 阅读
  9. gitee上的vue大屏项目

    2023-12-31 06:24:04       59 阅读