【问题标题】:Convert the received NetworkStream string into an object将接收到的 NetworkStream 字符串转换为对象
【发布时间】:2021-05-23 11:56:29
【问题描述】:

我正在尝试从服务器上的客户端接收数据并使用 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, 0, 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, 0, bytesRead));
            Console.WriteLine("Received : " + Encoding.ASCII.GetString(data, 0, bytesRead));

【问题讨论】:

  • 什么样的对象?字符串是什么格式的? JSON?你需要更清楚

标签: c# network-programming tcpclient tcplistener networkstream


【解决方案1】:

作者 = Encoding.ASCII.GetString(data, 0, 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
            };

【讨论】:

    猜你喜欢
    • 2022-01-20
    • 2014-01-14
    • 2011-08-02
    • 2016-06-10
    相关资源
    最近更新 更多