我正在编写一个服务器,它在特定端口侦听来自浏览器的请求,例如http://23.10.222.151:20000/mac=0080440EEE71
。我获取浏览器套接字,然后检查其请求,在本例中为mac=2280440FFF23
。我看到该设备是否已连接,如果不是,我想显示一个页面,上面写着“设备:{0}尚未向服务器注册!请等待......”
TcpClient client = listener.AcceptTcpClient();
Console.WriteLine("Connection Accepted from " + client.Client.RemoteEndPoint);
NetworkStream ClientStream = client.GetStream();
if( !devDictionary.isDevicePresent(mac))
{
Thread onRequestThread = new Thread(new ParameterizedThreadStart(OnRequesstHandler));
onRequestThread.Start(new OnRequestHandlerObject(mac, ClientStream));
String notfound = String.Format("<html><body><h1>Device: {0} has NOT registered with the Server! Please wait...</h1></body></html>", mac);
byte[] response = encoder.GetBytes(notfound);
ClientStream.Write(response, 0, response.Length);
}
线程会一直检查设备是否已连接。如果是,那么它使用相同的ClientStream显示它。但该页面不会立即显示。它保持循环,当设备连接时,它会快速连续显示两个页面。如何立即显示第一页?