【发布时间】: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