Пример #1
0
    private void TcpClient() {
      TcpClient tcpClient;
      byte[] buffer = new byte[250];
      int bytesRead;
      string cmdStr;

      tcpListener.Start();
      prompt.WriteLine("Waiting for control clients on port " + controlPort + ". ");
      while (true) {
        try { tcpClient = tcpListener.AcceptTcpClient(); } catch (Exception) { return; }
        prompt.WriteLine("Control client connected", prompt.eventTextColor);
        controlClient = tcpClient.GetStream();
        prompt.EnableTCPClientEcho(controlClient);
        var encoder = new UTF8Encoding();
        byte[] hello = encoder.GetBytes(helloMsg + '\n');
        controlClient.Write(hello, 0, hello.Length);

        while (true) {
          try {
            bytesRead = controlClient.Read(buffer, 0, 250);
            cmdStr = this.Parse(buffer, 0, bytesRead);
            prompt.ControlClientCmd(cmdStr);
            string cmd = cmdStr.ToLower();
            if (cmd.Equals("exit") && activeListeners.Count == 0) 
              continue;

            ExecCommand(cmd);
          } catch (Exception e) {
            Debug.WriteLine(e.Message);
            CloseControlClient();
            break;
          } 
        }
      }
    }