Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc asp.net mvc应用程序中的TCPClient_Asp.net Mvc - Fatal编程技术网

Asp.net mvc asp.net mvc应用程序中的TCPClient

Asp.net mvc asp.net mvc应用程序中的TCPClient,asp.net-mvc,Asp.net Mvc,我有一个显示varios事件的asp.net mvc应用程序。存储在数据库中的所有事件。但是现在,我必须从数据库和远程程序加载数据。这个程序有外部服务(这是一个简单的程序,它监听特定的TCP端口,接收查询并将xml发送回) 并且,我为测试编写了一个简单的页面,该页面连接到外部程序。从MSDN获取的代码: static string Connect(String server, String message) { try { // Create a TcpClient.

我有一个显示varios事件的asp.net mvc应用程序。存储在数据库中的所有事件。但是现在,我必须从数据库和远程程序加载数据。这个程序有外部服务(这是一个简单的程序,它监听特定的
TCP
端口,接收查询并将xml发送回)

并且,我为测试编写了一个简单的页面,该页面连接到外部程序。从MSDN获取的代码:

static string Connect(String server, String message) 
{
  try 
  {
    // Create a TcpClient.        
    Int32 port = 9197;
    TcpClient client = new TcpClient(server, port);

    // Translate the passed message into ASCII and store it as a Byte array.
    Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);         

    // Get a client stream for reading and writing.
   //  Stream stream = client.GetStream();

    NetworkStream stream = client.GetStream();

    // Send the message to the connected TcpServer. 
    stream.Write(data, 0, data.Length); 

    // Receive the TcpServer.response.

    // Buffer to store the response bytes.
    data = new Byte[256];

    // String to store the response ASCII representation.
    String responseData = String.Empty;

    // Read the first batch of the TcpServer response bytes.
    Int32 bytes = stream.Read(data, 0, data.Length);
    responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);                


    stream.Close();         
    client.Close();  
    return responseData;
  } 
  catch (ArgumentNullException e) 
  {
    //
  } 
  catch (SocketException e) 
  {
    //
  }  

}
这是我的行动:

public ActionResult GetData()
        {           

            string query = "some query";

            var response = Connect("192.168.0.1", query);
            var model =  ParseResponse(response);
            return View(model);
        }
我认为这个解决方案会降低性能

在ASP.NET MVC 3应用程序中使用TCPClient的最佳实践是什么?
你觉得我的代码怎么样? 欢迎提出任何建议

我认为这个解决方案会降低性能

与服务器执行的任何其他远程请求一样,这是一个I/O密集型操作。因此,您可以使用和异步版本的TcpClient方法。这样,在执行远程请求期间,您就不会危及服务器上的任何工作线程

我认为这个解决方案会降低性能

嗯。所有/大多数数据库操作都是通过套接字完成的。你没有注意到,是吗

最可能的性能问题是:

  • 你的服务器
  • 服务器位置
  • 连接设置
  • 我现在要做的唯一一件事是在客户端中建立检查,以监控响应时间,并在响应时间太长时写入日志(或发送电子邮件)

    在这之前不要尝试优化性能。

    上述问题的解决方案:

  • 重构和优化
  • 将服务器置于同一lan上或创建缓存代理服务器
  • 使用连接池,而不是每次都断开连接