TCPListener 不接受连接

发布于 2024-12-20 13:27:15 字数 4056 浏览 2 评论 0原文

我的 TCPListener 遇到问题。我在下面创建了此代码,它适用于测试应用程序,但我无法让它接收来自生产盒的连接。在下图中,您可以看到 .44 不断尝试连接,但如控制台窗口输出中所示,除了侦听开始之外,没有收到任何连接。

我在忽略什么?

    public class TCPServer
{
    #region Privates
    private ILog log;
    private readonly string _connectionString = "";
    private readonly List<AgentState> _lAgentState;
    private readonly DateTime _lastUpdatedRecord = new DateTime();
    private readonly TcpClient _tcpClient;
    private readonly IPEndPoint _serverEndPoint;
    private int _messageNumber = 2;
    private TcpListener _tcpListener;
    private Thread _listenThread;

    #endregion

    public IEXHermes()
    {
        var methodName = new System.Diagnostics.StackTrace().GetFrame(0).GetMethod().Name;

        try
        {
            log = LogManager.GetLogger("Logger");
            log.Info("Class Starting");
            _connectionString = Properties.Settings.Default.HN_ConnectionString;
            _lAgentState = getInitialState();
            _lastUpdatedRecord = _lAgentState.Max(w => w.actionLocalTime);



            Int32 iexPort = Int32.Parse(Properties.Settings.Default.IEX_Port);
            _tcpListener = new TcpListener(IPAddress.Any, iexPort);
            log.Debug("Server Open on Server: " + IPAddress.Any);
            log.Debug("Server Open on Port: " + iexPort);
            _listenThread = new Thread(listenForClients);
            _listenThread.Start();

        }
        catch (Exception ex)
        {

            log.FatalFormat("{0} - {1} \n {2}", methodName, ex.Message, ex.StackTrace);
        }
    }

    private void listenForClients()
    {
        var methodName = new System.Diagnostics.StackTrace().GetFrame(0).GetMethod().Name;
        log.Debug(methodName + " Starting");

        try
        {

            log.Debug(methodName + " - Listening Started");
            _tcpListener.Start();

            while (true)
            {
                try
                {
                    var client = _tcpListener.AcceptSocket();
                    var clientThread = new Thread(handleClientComm);
                    clientThread.Start(client);
                }
                catch (Exception ex)
                {
                    log.FatalFormat("{0} - {1} \n {2}", methodName, ex.Message, ex.StackTrace);
                }

            }
        } catch (Exception ex)
        {
            log.FatalFormat("{0} - {1} \n {2}", methodName, ex.Message, ex.StackTrace); 
        }
        log.Debug(methodName + ": Listener Closer");
    }

    private void handleClientComm(object client)
    {
        var methodName = new System.Diagnostics.StackTrace().GetFrame(0).GetMethod().Name;
        var tcpClient = (TcpClient)client;
        log.Debug(methodName + ": New Connection Established");
        try
        {

            var clientStream = tcpClient.GetStream();
            var message = new byte[4096];

            while (true)
            {
                var bytesRead = 0;

                try
                {
                    bytesRead = clientStream.Read(message, 0, 4096);
                }
                catch
                {
                    break;
                }

                if (bytesRead == 0)
                {
                    break;
                }

                var encoder = new ASCIIEncoding();
                var a = encoder.GetString(message, 0, bytesRead);
                Console.WriteLine("Recieved: " + a.ToUpper());

                if (a.ToUpper().Contains("INIT"))
                {    
                    a = sessionInitialize(); 
                } 
                    tcpClient.Client.Send(Encoding.UTF8.GetBytes(a));

                Console.WriteLine("Sent: " + a);

            }


        } 
        catch (Exception ex)
        {
            log.FatalFormat("{0} - {1} \n {2}", methodName, ex.Message, ex.StackTrace);   
        } 
        finally
        {
            tcpClient.Close();
        }
    }
}

Wireshark 捕获

I'm having an issue with a TCPListener. I have created this code below and it works with a test application, but I can't get it to receive the connection from the production box. In the image below you can see .44 is continuously attempting to connect, but as seen in the Console window output, no connection is ever received beyond the Listening Started.

What am I overlooking?

    public class TCPServer
{
    #region Privates
    private ILog log;
    private readonly string _connectionString = "";
    private readonly List<AgentState> _lAgentState;
    private readonly DateTime _lastUpdatedRecord = new DateTime();
    private readonly TcpClient _tcpClient;
    private readonly IPEndPoint _serverEndPoint;
    private int _messageNumber = 2;
    private TcpListener _tcpListener;
    private Thread _listenThread;

    #endregion

    public IEXHermes()
    {
        var methodName = new System.Diagnostics.StackTrace().GetFrame(0).GetMethod().Name;

        try
        {
            log = LogManager.GetLogger("Logger");
            log.Info("Class Starting");
            _connectionString = Properties.Settings.Default.HN_ConnectionString;
            _lAgentState = getInitialState();
            _lastUpdatedRecord = _lAgentState.Max(w => w.actionLocalTime);



            Int32 iexPort = Int32.Parse(Properties.Settings.Default.IEX_Port);
            _tcpListener = new TcpListener(IPAddress.Any, iexPort);
            log.Debug("Server Open on Server: " + IPAddress.Any);
            log.Debug("Server Open on Port: " + iexPort);
            _listenThread = new Thread(listenForClients);
            _listenThread.Start();

        }
        catch (Exception ex)
        {

            log.FatalFormat("{0} - {1} \n {2}", methodName, ex.Message, ex.StackTrace);
        }
    }

    private void listenForClients()
    {
        var methodName = new System.Diagnostics.StackTrace().GetFrame(0).GetMethod().Name;
        log.Debug(methodName + " Starting");

        try
        {

            log.Debug(methodName + " - Listening Started");
            _tcpListener.Start();

            while (true)
            {
                try
                {
                    var client = _tcpListener.AcceptSocket();
                    var clientThread = new Thread(handleClientComm);
                    clientThread.Start(client);
                }
                catch (Exception ex)
                {
                    log.FatalFormat("{0} - {1} \n {2}", methodName, ex.Message, ex.StackTrace);
                }

            }
        } catch (Exception ex)
        {
            log.FatalFormat("{0} - {1} \n {2}", methodName, ex.Message, ex.StackTrace); 
        }
        log.Debug(methodName + ": Listener Closer");
    }

    private void handleClientComm(object client)
    {
        var methodName = new System.Diagnostics.StackTrace().GetFrame(0).GetMethod().Name;
        var tcpClient = (TcpClient)client;
        log.Debug(methodName + ": New Connection Established");
        try
        {

            var clientStream = tcpClient.GetStream();
            var message = new byte[4096];

            while (true)
            {
                var bytesRead = 0;

                try
                {
                    bytesRead = clientStream.Read(message, 0, 4096);
                }
                catch
                {
                    break;
                }

                if (bytesRead == 0)
                {
                    break;
                }

                var encoder = new ASCIIEncoding();
                var a = encoder.GetString(message, 0, bytesRead);
                Console.WriteLine("Recieved: " + a.ToUpper());

                if (a.ToUpper().Contains("INIT"))
                {    
                    a = sessionInitialize(); 
                } 
                    tcpClient.Client.Send(Encoding.UTF8.GetBytes(a));

                Console.WriteLine("Sent: " + a);

            }


        } 
        catch (Exception ex)
        {
            log.FatalFormat("{0} - {1} \n {2}", methodName, ex.Message, ex.StackTrace);   
        } 
        finally
        {
            tcpClient.Close();
        }
    }
}

Wireshark Capture

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

謌踐踏愛綪 2024-12-27 13:27:15

也许尝试用真实 IP 替换 IPAddress.Any 至少用于测试目的,只是为了确定......

maybe try to replace IPAddress.Any by the real IP at least for testing purpose, just to be sure...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文