我正在编写一个与服务器连接的简单应用程序.但是我想发送简单的聊天命令(见Console.ReadLine下文).但是这个脚本无法访问, string Message = Console.ReadLine();因为它被阻止了bytesRead = clientStream.Read(message, 0, 4096);.
我想继续这个脚本,但如果有字节传入,它应该处理它们(就像它现在正在做的那样),如果没有字节传入,它应该通过脚本并等待用户输入).怎么能实现这一目标?
TcpClient tcpClient = (TcpClient)client;
NetworkStream clientStream = tcpClient.GetStream();
byte[] message = new byte[4096];
int bytesRead;
while (true)
{
bytesRead = 0;
try
{
// Blocks until a client sends a message
bytesRead = clientStream.Read(message, 0, 4096);
}
catch (Exception)
{
// A socket error has occured
break;
}
if (bytesRead == 0)
{
// The client has disconnected from the server
break;
}
// Message …Run Code Online (Sandbox Code Playgroud) c# ×1