TCP/IP客户端循环.NET

6

我实际上在开发一个网络应用程序(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”,所以有没有更好的方法来完成这个任务,而不需要在浏览器上循环?

非常感谢!

1个回答

5

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


首先感谢您的时间@dperish。我有一个问题(也许是个愚蠢的问题...)这个Web应用程序将被“部署”在服务器上,仅在本地网络中使用而没有互联网。使用signalr会有问题吗? - Atloka
不,只要您已将SignalR软件包部署到您的内部服务器上,这就不应该成为问题。如果您是通过NuGet(“Install-Package Microsoft.AspNet.SignalR”)安装的,则应正确打包/部署。 - dperish

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接