2012-03-15 114 views
-1

我正在编写一个服务器,它在特定端口侦听来自浏览器的请求,例如http://23.10.222.151:20000/mac=0080440EEE71。我得到浏览器套接字,然后检查它的请求,在这种情况下mac=2280440FFF23。我看看有没有什么设备连接,如果不是我想显示一个页面,上面写着“设备:{0}无法与服务器进行注册,请稍候......”写入C#套接字

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显示它。但该页面不会立即显示。它保持循环,并在设备连接时快速连续显示两页。如何立即显示第一页?

+0

您是否尝试过'ClientStream.Flush();'? – 2012-03-15 17:05:42

+0

是的,我做到了。没有工作... – batrulz 2012-03-15 17:08:24

回答

0

HTTP是一个涉及的协议。您不只是将HTML内容发送到客户端,而是首先需要发送消息标题。

从一些现成的,而不是裸露的插座开始。看看,例如,here