您必须先从TcpClient获取网络流。之后开始阅读。
使用下面的代码。
TcpClient tcpClient = new TcpClient();
// Uses the GetStream public method to return the NetworkStream.
NetworkStream netStream = tcpClient.GetStream();
if (netStream.CanRead)
{
// Reads NetworkStream into a byte buffer.
byte[] bytes = new byte[tcpClient.ReceiveBufferSize];
// Read can return anything from 0 to numBytesToRead.
// This method blocks until at least one byte is read.
netStream.Read (bytes, 0, (int)tcpClient.ReceiveBufferSize);
// Returns the data received from the host to the console.
string returndata = Encoding.UTF8.GetString (bytes);
Console.WriteLine ("This is what the host returned to you: " + returndata);
}
你想完全解决什么问题? – Leo
它在“ReadLine()”中“粘住”,因为该操作会阻塞,直到它遇到换行符,这可能不会被发送或接收。 – CodeCaster