TCPListener的worker角色停止工作

时间:2013-11-14 19:17:01

标签: c# asp.net azure

我正在运行这个非常基本的工作者角色,监听传入的TCP连接并处理它们。但由于某些原因,现在失败了一段时间..似乎它不再接收数据......在跟踪中没有看到异常。 现在让它同步......仍然是同样的问题。

我们的想法是接受HTTP请求并提供302重定向。

当我通过端口80上的telnet连接到它时,它可以工作。

一旦我使用浏览器,它就会很快失败。

当我在端口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();
    }

1 个答案:

答案 0 :(得分:0)

我认为问题不在于您的应用程序是如何实现的。

您是否考虑过Azure负载均衡超时?

这是一篇很好的博客文章,详细介绍了它:

Windows Azure Load Balancer Timeout Details

Azure将在一分钟左右的时间内停止您的应用程序。

看一下,看看你面临的问题是否存在。

此致