我实际上在开发一个网络应用程序(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);
}
}
实际上它正在工作,我已经收到了来自服务器的响应,但是浏览器一直在“等待localhost”,所以有没有更好的方法来完成这个任务,而不需要在浏览器上循环?
非常感谢!