【问题标题】:worker role with TCPListener stops workingTCPListener 的工作角色停止工作
【发布时间】:2014-04-17 04:38:30
【问题描述】:

我有这个非常基本的工作角色正在运行,监听传入的 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();
    }

【问题讨论】:

  • 您尝试调试它吗?当它“停止工作”时它在做什么?
  • 也许你得到一个“正常”的异常?尝试添加:catch (Exception se) { Trace.WriteLine("服务器停止。异常:" + se.Message);返回; }
  • 当它“停止工作”时,它不能再接受连接。代码中已经有一个 try/catch,但实际上只是针对 SocketException。让我改变它。
  • 只是一个一般性问题.. 此代码显示它正在 Azure 管理器中“运行”,但仍然没有接受单个 TCP 连接..
  • 我不确定你所说的运行是什么意思,但也许你需要通过尝试排除 while 来“保护”你的监听循环(并且只是记录它而不是停止监听)。 acceptcpclient 阻塞直到有连接,所以不需要 Sleep(10)。请注意,当进程(客户端)运行时,没有新连接除外,也许您应该为此使用单独的线程。在服务器停止的情况下,您可以通过检查取消阻塞异常 (10004) 来跳出 while 循环。

标签: c# asp.net azure


【解决方案1】:

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

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

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

Windows Azure Load Balancer Timeout Details

Azure 将在一分钟左右后停止您的应用程序,与您的实现无关。

看看那是不是你面临的问题。

问候

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-20
    • 2015-08-26
    • 2012-11-29
    • 1970-01-01
    • 2015-01-02
    • 2012-06-14
    • 2019-02-07
    相关资源
    最近更新 更多