【问题标题】:TCP/IP Client side loop .NETTCP/IP 客户端循环 .NET
【发布时间】: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);
            }
        }

这确实有效,我收到了来自服务器的响应,但是浏览器一直在“等待本地主机”,那么有没有更好的方法来做到这一点,而无需浏览器上的循环?

非常感谢!

【问题讨论】:

    标签: asp.net .net tcp-ip


    【解决方案1】:

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

    【讨论】:

    • 首先感谢您抽出宝贵时间@dperish。我还有一个问题(也许是一个愚蠢的问题......) Web 应用程序将“部署”在服务器上,仅在没有互联网的本地网络上使用。这会是使用信号器的问题吗?
    • 不,这无关紧要,只要您将 SignalR 包部署到您的 Intranet 服务器即可。如果您已通过 NuGet ('Install-Package Microsoft.AspNet.SignalR') 安装,则应正确打包/部署它
    猜你喜欢
    • 2018-09-19
    • 1970-01-01
    • 2017-06-11
    • 1970-01-01
    • 2013-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多