狼烟辉
2022-04-01 22:46
采纳率: 0%
浏览 11

关于ssl socket通讯的假死问题,求解!

在Linux上启动socket服务,启动后正常,但是经常一两天就假死了,服务状态显示还在,但是访问不到服务。

代码如下

public static void RunServer()
        {
            TcpListener listener = new TcpListener(IPAddress.Parse("0.0.0.0"), 3306);//183.220.145.83
            //TcpListener listener = new TcpListener(IPAddress.Parse("127.0.0.1"), 3306);
            listener.Start();
            while (true)
            {
                try
                {
                    Console.WriteLine("Waiting for a client to connect...");
                    TcpClient client = listener.AcceptTcpClient();
                    ProcessClient(client);
                }
                catch
                {
                }
            }
        }

        /// <summary>
        /// 处理来自客户端的连接
        /// </summary>
        /// <param name="client"></param>
        static void ProcessClient(TcpClient client)
        {
            SslStream sslStream = new SslStream(client.GetStream(), false);
            try
            {
                sslStream.AuthenticateAsServer(serverCertificate, false, SslProtocols.Tls, true);
                DisplaySecurityLevel(sslStream);
                DisplaySecurityServices(sslStream);
                DisplayCertificateInformation(sslStream);
                DisplayStreamProperties(sslStream);

                sslStream.ReadTimeout = 5000;
                sslStream.WriteTimeout = 5000;

                //byte[] message = Encoding.UTF8.GetBytes("Hello from the server.");
                //Console.WriteLine("Sending hello message.");
                //sslStream.Write(message);
                Console.WriteLine("Waiting for client message...");
                while (true)
                {
                    string messageData = ReadMessage(sslStream);
                    //Console.WriteLine(messageData);
                    //Console.WriteLine(Comm.translateAndDo1(messageData));

                    byte[] resultBytes = Comm.translateAndDo(messageData);
                    sslStream.Write(resultBytes);

                    //Console.WriteLine(messageData);
                    //int len = Convert.ToInt32(messageData.Substring(2, 8));
                    //Console.WriteLine(len.ToString());
                    //string serverCode = messageData.Substring(10, 12);
                    //Console.WriteLine(serverCode);
                    //string messageRes = messageData.Substring(22, len);
                    //Console.WriteLine(messageRes);
                    if (messageData.ToUpper() == "EXIT")
                        break;
                }
            }
            catch (AuthenticationException e)
            {
                Console.WriteLine("Exception: {0}", e.Message);
                if (e.InnerException != null)
                {
                    Console.WriteLine("Inner exception: {0}", e.InnerException.Message);
                }
                Console.WriteLine("Authentication failed - closing the connection.");
                sslStream.Close();
                client.Close();
                return;
            }
            finally
            {
                sslStream.Close();
                client.Close();
            }
        }

求解!

1条回答 默认 最新

相关推荐 更多相似问题