简体   繁体   English

TCPListener的辅助角色停止工作

[英]worker role with TCPListener stops working

I have this very basic worker role running, listening to a incoming TCP connections and processing them. 我正在运行这个非常基本的工作角色,监听传入的TCP连接并进行处理。 But for some reason now fails after a while.. seems like it is no longer receiving data... no exception seen in the trace. 但是由于某种原因,现在过了一会儿就失败了..似乎不再接收数据了……在跟踪中没有发现异常。 Now make it synchronous... still the same problem. 现在使其同步...仍然是同样的问题。

The idea is to accept an HTTP request and give a 302 redirection. 这个想法是接受一个HTTP请求并给出302重定向。

When I connect to it via a telnet on port 80 it works. 当我通过端口80上的telnet连接到它时,它可以工作。

As soon as I use a browser, it starts failing quickly. 一旦我使用浏览器,它就会开始快速失败。

When I try again afterwards on port 80, no response anymore. 之后,我在端口80上再次尝试时,不再响应。

public class WorkerRole : RoleEntryPoint
{
    public override void Run()
    {
        TcpListener server = null;
        IPEndPoint ipin = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["Server"].IPEndpoint;
        server = new TcpListener(ipin);
        server.ExclusiveAddressUse = false;
        server.Start();

        try
        {
            while (true)
            {
                TcpClient client = server.AcceptTcpClient();
                HandleSyncConnection(client);
            }
        }
        catch (Exception ex)
        {
            Trace.WriteLine("Server stopped. Exception:" + ex.Message);
            return;
        }
    }
    private void HandleSyncConnection(TcpClient client)
    {
        try
        {
            // Setup reader/writer 
            NetworkStream netStream = client.GetStream();
            StreamReader reader = new StreamReader(netStream, Encoding.ASCII);
            StreamWriter writer = new StreamWriter(netStream, Encoding.ASCII);
            writer.AutoFlush = true;

            byte[] baBuffer = new byte[10000];
            byte[] baHeader = { 13, 10, 13, 10 };
            Int32 iTotalBytesReceived = 0;
            Int32 iHeaderFound = 0;

            while (iTotalBytesReceived < baBuffer.Length)
            {
              Int32 iBytesReceived = netStream.Read(baBuffer, iTotalBytesReceived, baBuffer.Length - iTotalBytesReceived);
              if (iBytesReceived == 0)
                   break;
              for (int i = 0; i < iBytesReceived; i++)
              {
                  if (baBuffer[iTotalBytesReceived + i] == baHeader[iHeaderFound])
                        iHeaderFound++;
                  else
                      iHeaderFound = 0;
              }
              iTotalBytesReceived += iBytesReceived;
              if (iHeaderFound == baHeader.Length)
                 break;
              Thread.Sleep(50);
            }

            String strResponse;
            Trace.TraceInformation("Request received");
            strResponse = "HTTP/1.1 302 Redirect" + EOL + "Location: http://www.google.com");
            strResponse += EOL + "Content-Type: text/html" + EOL + "Cache-Control: no-cache" + EOL + "Connection: close" + EOL + "Content-Length: 0" + EOL + EOL;
            writer.Write(strResponse);
            writer.Close();
            reader.Close();
            // Done! 
            client.Close();
          }
          catch (Exception e)
          {
            Trace.TraceError("Server stopped in the handling of sync " + e.Message);
          }
    }
    public override bool OnStart()
    {
        return base.OnStart();
    }

I think the problem is not on how your application is implemented. 我认为问题不在于您的应用程序如何实现。

Have you consider the Azure load balance timeout? 您是否考虑过Azure负载平衡超时?

Here is a good blog post detailing it: 这是一篇很好的博客文章,详细介绍了它:

Windows Azure Load Balancer Timeout Details Windows Azure负载平衡器超时详细信息

Azure will stop your application after a minute or so independent of your implementation. 一分钟左右,Azure将停止您的应用程序,而与您的实现无关。

Take a look at that and see if that is problem that you are facing. 看一看,看看这是否是您面临的问题。

Regards 问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM