【发布时间】:2017-01-26 06:39:38
【问题描述】:
我实际上是在一个 Web 应用程序 (ASP.NET MVC) 上工作,它通过 TCP/IP 向本地网络上的胖客户端应用程序(我无法修改)发送命令。我需要胖客户端应用程序响应我的命令或给我信息,所以连接需要一直打开。我创建了一个循环来检查服务器响应:
public static async void getMessage()
{
while(true)
{
// Buffer to store the response bytes.
Byte[] data = new Byte[100000];
// String to store the response ASCII representation.
String responseData = String.Empty;
// Read the first batch of the TcpServer response bytes.
Int32 bytes = await stream.ReadAsync(data, 0, data.Length);
if (bytes == 0) continue;
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
Debug.Write(responseData);
await stream.ReadAsync(data, 0, data.Length);
}
}
这确实有效,我收到了来自服务器的响应,但是浏览器一直在“等待本地主机”,那么有没有更好的方法来做到这一点,而无需浏览器上的循环?
非常感谢!
【问题讨论】: