问题描述
我正在尝试从服务器上的客户端接收数据并使用 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
};