【问题标题】:Manage Standard Input/Output in another Thread?在另一个线程中管理标准输入/输出?
【发布时间】:2011-10-12 02:43:57
【问题描述】:

基本的纲要是……

1) 我的应用程序启动一个 Java 进程并对其进行管理。

2) 我在另一个线程中有一个 TCP 服务器,用户可以连接到它并发出 Java 进程可以理解的命令。

3) 传递命令不是问题是streamWriter.WriteLine(cmd);但事实证明很难得到回应。

通过 RedirectStandardError 进入的下一行(由于某种原因它使用它而不是输出)应该是回复,但我无法弄清楚如何在我的 TCP 服务器线程中访问它。

这就是我的 TCP 服务器的功能,它需要获取回复并将其打印出来。我该怎么做?

    static void AcceptClient(IAsyncResult ar)
    {
        TcpListener rconListener = ar.AsyncState as TcpListener;
        TcpClient rconClient = rconListener.EndAcceptTcpClient(ar);
        Console.WriteLine("New client: " + rconClient.Client.RemoteEndPoint.ToString());

        NetworkStream ns = rconClient.GetStream();

        // Loop while client is Connected
        while (rconClient.Connected)
        {
            byte[] buff = new byte[4096];
            List<byte> msg = new List<byte>();
            int total = 0;

            while (true)
            {
                int read = ns.Read(buff, 0, buff.Length);
                if (read <= 0)
                    break;

                msg.AddRange(buff);
                total += read;

                if (read < buff.Length)
                    break;
            }
            if (msg.Count <= 0)
            {
                Console.WriteLine("Lost connection: " + rconClient.Client.RemoteEndPoint.ToString());
                rconClient.Close();
                break;
            }

            int len = BitConverter.ToInt32(msg.ToArray(), 0);
            int seq = BitConverter.ToInt32(msg.ToArray(), 4);

            PacketType packetType = (PacketType)BitConverter.ToInt32(msg.ToArray(), 8);
            List<byte> response = new List<byte>();


            // RCON Execute Command
            if (packetType == PacketType.ServerData_ExecCommand_AuthResponse)
            {
                string srvcmd = ReadString(msg.ToArray(), 12);
                Console.WriteLine("RCON: " + srvcmd);

                response.AddRange(BitConverter.GetBytes((int)seq));
                response.AddRange(BitConverter.GetBytes((int)PacketType.ServerData_ResponseValue));

                string[] cmdargs = srvcmd.Split(new char[] { ' ' });

                if (cmdargs[0] == "rcon_password")
                {
                    ServerSettings.RCONPassword = cmdargs[1];
                    response.AddRange(Encoding.ASCII.GetBytes("RCON Password succesfully changed to " + cmdargs[1]));
                }
                else if (cmdargs[0] == "date")
                {
                    response.AddRange(Encoding.ASCII.GetBytes(DateTime.Now.ToString()));
                }
                else
                {
                    Program.SendRCONCmd(cmdargs[0]);
                }


                response.AddRange(new byte[] { 0x20, 0x0a }); //LF
                response.Add(0x0);
                response.Add(0x0);
                response.InsertRange(0, BitConverter.GetBytes((response.Count)));
                ns.Write(response.ToArray(), 0, response.Count);
            }
        }
    }

【问题讨论】:

    标签: c# .net multithreading inputstream outputstream


    【解决方案1】:

    您检查过 SDK 文档吗?

    Process.StandardError 属性

    Gets a stream used to read the error output of the application.

    【讨论】:

    • 是的,我对此没有任何意见。正如我所提到的,问题是我不确定应该如何将它传递给我的 TCP 服务器线程以显示给该用户。
    • 那么你能收到来自服务器的响应吗?
    • 我收到来自子进程(Java 应用程序)的回复很好,但我找不到将它传递给我的 TCP 服务器以发送给客户端的好方法,因为我的服务器是在另一个线程中。
    • 假设您使用的是 .NET 4,我将使用任务并行库 - msdn.microsoft.com/en-us/library/dd537608.aspx 进行调查。在线程之间传递数据有很多不错的钩子
    猜你喜欢
    • 2013-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-22
    • 2017-02-03
    • 2017-07-24
    • 1970-01-01
    相关资源
    最近更新 更多