您必須先從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