代码之家  ›  专栏  ›  技术社区  ›  RodH257

帮助为我的应用程序创建/调试C许可证服务器

  •  2
  • RodH257  · 技术社区  · 16 年前

    我有许多C软件应用程序,它们是具有.NET API的软件的插件。

    出于软件许可的目的,我需要有一个许可服务器。我有这个,现在它的作用是:

    • 当工具运行时,它使用其计算机名和用户名与许可证服务器联系。
    • 服务器接收此请求,记录请求,并检查来自该机器和/或用户的请求数是否超过了预先配置的最大值(2种不同的许可类型)。
    • 它使用一个加密的文件来完成这项工作,该文件列出了时间、机器名、用户名等。
    • 如果它正常,它将向计算机返回正常响应,然后工具运行
    • 如果它没有返回任何东西,或者如果它返回一个不正常的响应,那么工具将不会运行(软件只运行很短的时间,但正常运行,而不是长时间打开)

    这很好用,只是我有一个问题,服务器随机崩溃,用户无法进入。

    这是我做过的第一个客户机-服务器应用程序,我有点不知所措(我觉得我在重新发明一些常用的东西),有没有什么好的指南来创建类似的东西?我的应用程序非常简单,我有一个服务在服务器上运行,循环监听TCP端口,等待发送什么。

        public void StartListening()
        {
            Listening = true;
            // Set the TcpListener on port 41616.
            Int32 port = 41616;
            IPAddress localAddr = IPAddress.Any;
    
            // TcpListener server = new TcpListener(port);
            server = new TcpListener(localAddr, port);
         // Start listening for client requests.
            server.Start();
            Listen();
        }
    
        public void Listen()
        {
            try
            {
                // Buffer for reading data
                Byte[] bytes = new Byte[256];
                String data = null;
    
                // Enter the listening loop.
                while (Listening)
                {
                   TcpClient client = server.AcceptTcpClient();
    
                    // Get a stream object for reading and writing
                    NetworkStream stream = client.GetStream();
    
                    int i;
    
                    // Loop to receive all the data sent by the client.
                    while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                    {
                        // Translate data bytes to a ASCII string.
                        data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                        Console.WriteLine("Received: {0}", data);
    
                        string returnData;
                        // Process the data sent by the client.
                        data = data.ToUpper();
                        byte[] msg = null;// DO STUFF BASED ON DATA
    // set returnData to be the response (ie OKAY)
                      msg = System.Text.Encoding.ASCII.GetBytes(returnData);
    // Send back a response.
                        stream.Write(msg, 0, msg.Length);
                        Console.WriteLine("Sent: {0}", returnData);
          // Shutdown and end connection
                    client.Close();
                }
    
            }
            catch (Exception ex)
            {
               //LOG ERROR
            }
            finally
            {
                Listening = false;
                server.Stop();
            }
    
        }
    

    我感觉服务器挂在许可证日志的解密/加密上,因为服务器运行正常一段时间后就停止了,如果我重新创建许可证日志,它就会工作。但这不是这篇文章的重点(尽管有任何关于我为什么会得到这个错误的提示:

    出现错误:无法从传输连接读取数据:远程主机强制关闭了现有连接。at system.net.sockets.networkstream.read(byte[]缓冲区,int32偏移量,int32大小) 位于bwsLicenseServer.LicenseServer.Listen()。

    非常棒!

    我的问题是-如何在.NET中轻松调试?有没有好的向导可以帮助我?或者有免费的框架吗?我是否应该以完全不同的方式(WCF、Web服务或类似的方式)执行此代码?

    2 回复  |  直到 16 年前
        1
  •  1
  •   RodH257    16 年前

    最后使用了评论中建议的加密许可

        2
  •  0
  •   logicnp    16 年前

    这可能与过期的对象有关。尝试将此添加到服务器类:

        public override object InitializeLifetimeService()
        {
            return null;
        }
    
    推荐文章