TCP侦听器在负载均衡器后面时无法正常工作

时间:2013-05-06 18:35:43

标签: c# tcp load-balancing router tcplistener

我在Windows服务中有一个TCP侦听器,它侦听特定端口上的任何传入TCP请求并处理该消息。直接访问它时工作正常。但是一旦它在负载均衡器(在Intranet中)后面运行,那么它就不接受任何请求。我收到“无法连接到远程服务器”或“操作超时”等错误。一段时间后,服务以“内存不足”异常终止。请让我知道这可能是什么原因。粘贴下面的代码。我甚至尝试过异步模式(以避免显式线程启动)。但这没有帮助。

public class SampleListener: IDisposable
{
    public delegate void JobRecieved(HttpMessage msg);
    public event JobRecieved OnJobRecieved;

    #region Property

    private TcpListener _tcpListener;
    private Thread _listenerThread;

    public int Port { get; private set; }

    public string Url
    {
        get
        {
            return new UriBuilder { Scheme = "http", Port = Port, Host = Dns.GetHostName() }.ToString();
        }
    }

    #endregion

    public SampleListener(int port)
    {
        Port = port;
    }

    ~SampleListener()
    {
        DisposeImpl(false);
    }

    public void Start()
    {
        _tcpListener = new TcpListener(IPAddress.Any, Port);
        _tcpListener.Start();

        _listenerThread = new Thread(ListenCallback);
        _listenerThread.Start();
    }

    public void ListenCallback()
    {
        try
        {
            while (true)
            {
                using (TcpClient client = _tcpListener.AcceptTcpClient())
                using (var clientStream = client.GetStream())
                {
                    var msg = new HttpMessage();
                    msg.Receive(clientStream);
                    SendOKResponse(client, "");
                    OnJobRecieved(msg);
                    client.Close();
                }
            }
        }
        catch (System.Net.Sockets.SocketException e)
        {
            // Expected, TcpClient.Stop called                
        }
        catch (System.Threading.ThreadAbortException)
        {
            // Expected, thread going away
        }
        catch (System.IO.IOException)
        {
            // Expected, shutdown while reading
        }
    }

    private void SendOKResponse(TcpClient tcpClient, String responseBody)
    {
        var response = new HttpMessage
        {
            Status = "200",
            Reason = "OK",
            Version = "HTTP/1.1"
        };
        response.Send(tcpClient.GetStream(), responseBody);
    }

    public void Shutdown()
    {
        lock (this)
        {
            if (_listenerThread != null)
            {
                _listenerThread.Abort();
                _listenerThread = null;
            }

            if (_tcpListener != null)
            {
                _tcpListener.Stop();
                _tcpListener.Server.Close();
                _tcpListener = null;
            }                
        }
    }

    #region IDisposable Members

    private void DisposeImpl(Boolean bDisposing)
    {
        lock (this)
        {
            Shutdown();
        }
    }

    public void Dispose()
    {
        GC.SuppressFinalize(this);
        DisposeImpl(true);
    }

    #endregion

}

1 个答案:

答案 0 :(得分:1)

这是因为Windows上的NLB默认需要您的应用程序是群集的。如果不是(你的情况就是如此)你必须使用Sticky Sessions。显然你的NLB没有使用Sticky Sessions,所以请求可能会在每次传递时转到不同的服务器。这就是你得到这些例外的原因(看看this)。

在我自己的一个项目(一个高性能TCP服务器 - 与你正在做的事情相反)上发生了这件事。