微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将接收到的 NetworkStream 字符串转换为对象

如何解决将接收到的 NetworkStream 字符串转换为对象

我正在尝试从服务器上的客户端接收数据并使用 TcpListener 接收数据我已成功从客户端获取数据。所以我将数据接收为字符串,但我想使用 C# Visual Studio 将该数据转换为对象。 listener.Start();

            // Is someone trying to call us? Well answer!
            TcpClient ourTCP_Client = listener.AcceptTcpClient();

            //A network stream object. We'll use this to send and receive our data,so create a buffer for it...
            NetworkStream ourStream = ourTCP_Client.GetStream();
            byte[] data = new byte[ourTCP_Client.ReceiveBufferSize];

            // read the incoming data stream - note that Read() is a blocking call...
            int bytesRead = ourStream.Read(data,System.Convert.ToInt32(ourTCP_Client.ReceiveBufferSize));
            // echo the data we got to the console until the newline,and delay closing our window.
            //MessageBox.Show("Received : " + Encoding.ASCII.GetString(data,bytesRead));
            Console.WriteLine("Received : " + Encoding.ASCII.GetString(data,bytesRead));

解决方法

authors = Encoding.ASCII.GetString(data,bytesRead);

            string[] multiArray = authors.Split(new Char[] { '|' });

            string stdID = multiArray[0];
            string subjID = multiArray[1];
            string ChangeNo = multiArray[2];
            string IpAddress = multiArray[3];
            string Section = multiArray[4];
            string markAttend = multiArray[5];
            string currentDate = multiArray[6];
            string currentTime = multiArray[7];

            StudentAttendance stdAtd = new StudentAttendance
            {
                Sid = stdID,subjectId = subjID,CNo = ChangeNo,IP = IpAddress,section = Section,mark = markAttend,date = currentDate,time = currentTime
            };

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。