TCP/IP 客户端 loopNET

TCP/IP Client side loop .NET

我实际上是在开发 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);
            }
        }

这确实有效,我收到了服务器的响应,但浏览器一直是 "waiting for localhost",那么有没有更好的方法来做到这一点,而不是在浏览器上循环?

非常感谢!

这听起来像是一个非常好的 websocket 案例。在 MVC 应用程序中,您通常会使用 SignalR:http://www.asp.net/signalr