C#.网络编程 Socket基础(十一)Socket将ListView用户信息以字节发送,接收端将字节转字符串,循环扫描出多个用户的信息

using KeenRayLargePC.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace KeenRayLargePC.Views
{
    /// <summary>
    /// HistoryPage.xaml 的交互逻辑
    /// </summary>
    public partial class HistoryPage : Page
    {
        //标志位占2个字节
        int iFlagByteLong = 2;
        //长度占2个字节
        int iMsgByteLong = 2;
        //public List<HistoryPageListViewModel> HistoryPatientList = null;

        public HistoryPage()
        {
            InitializeComponent();
            //*ASCII值是42,#ASCII值是35
            byte[] aaaaa = new byte[26] { 97,42, 98, 42, 99, 42, 100, 42, 101, 42, 102, 42, 35, 65, 42, 66, 42, 67, 42, 68, 42, 69, 42, 70, 42, 35 };
            GetHistoryListFunc(aaaaa);
        }

        public List<HistoryPageListViewModel> GetHistoryListFunc(byte[] byt_bufferVar)
        {
            List<HistoryPageListViewModel> histroryList = new List<HistoryPageListViewModel>();
            //Socket将用户信息按字节发送过来,接收端以字符的方式解析: 英文字符“*”表示一个属性结束。英文字符“#”表示一个用户信息结束。以此来接收用户信息,并将用户信息放到列表集合中,给列表显示。
            string str = System.Text.Encoding.Default.GetString(byt_bufferVar);

            //循环扫描,将用户信息提取出来
            while (str.Length > 0)
            {
                //找出单用户包含的字符串
                int iUserIndex = str.IndexOf('#');
                //拷贝
                string strSingleUser = str.Substring(0, iUserIndex);
                //属性
                int iPropertyNum = 0;
                string strPatientId = null;
                string strPatientName = null;
                string strPatientSex = null;
                string strPatientAge = null;
                string stStudyBodyDesc = null;
                string strPatientSize = null;

                //将该用户的属性,逐个地拷贝出来(共6个属性)
                while (strSingleUser.Length > 0)
                {   
                    int iPropertyIndex = strSingleUser.IndexOf('*');
                    string strProperty = strSingleUser.Substring(0, iPropertyIndex);
                    strSingleUser = strSingleUser.Remove(0, iPropertyIndex + 1);
                    switch (iPropertyNum)
                    {
                        case 0:
                            strPatientId    = strProperty.ToString();
                            break;
                        case 1:
                            strPatientName  = strProperty.ToString();
                            break;
                        case 2:
                            strPatientSex   = strProperty.ToString();
                            break;
                        case 3:
                            strPatientAge   = strProperty.ToString();
                            break;
                        case 4:
                            stStudyBodyDesc = strProperty.ToString();
                            break;
                        case 5:
                            strPatientSize  = strProperty.ToString();
                            break;
                        default:
                            break;
                    }
                    iPropertyNum++;
                }
                //拷贝后、删除该用户
                str = str.Remove(0, iUserIndex + 1);
                histroryList.Add(new HistoryPageListViewModel { PatientId = strPatientId, PatientName = strPatientName, PatientSex = strPatientSex, PatientAge = strPatientAge, StudyBodyDesc = stStudyBodyDesc, PatientSize = strPatientSize });
            }   
            App.Current.Dispatcher.Invoke((Action)delegate ()
            {
                this.lvSeriesList.ItemsSource = histroryList;
            });
            return histroryList;
        }
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我爱AI

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值