简体   繁体   English

在另一个线程中管理标准输入/输出?

[英]Manage Standard Input/Output in another Thread?

The basic rundown is... 基本的概要是...

1) My application starts a Java process and manages it. 1)我的应用程序启动一个Java进程并对其进行管理。

2) I have a TCP Server in another thread that users can connect to and issue commands that the Java process understands. 2)我在另一个线程中有一个TCP服务器,用户可以连接到该线程,并发出Java进程可以理解的命令。

3) Passing the commands is not a problem is streamWriter.WriteLine(cmd); 3)传递命令不是问题,是streamWriter.WriteLine(cmd); but getting the response is proving to be difficult. 但是事实证明很难得到回应。

The next line that comes in through RedirectStandardError (it uses this instead of Output for some reason) should be the reply, but I am having trouble figuring out how to access that in my TCP Server's thread. 通过RedirectStandardError(由于某种原因而不是使用Output)输入的下一行应该是答复,但是我在弄清楚如何在TCP Server线程中访问它时遇到了麻烦。

This is what my TCP Server's function looks like that needs to get the reply and print it out. 这就是我的TCP Server功能的样子,需要获取答复并打印出来。 How can I go about doing this? 我该怎么做呢?

    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);
            }
        }
    }

have you checked the SDK documentation? 您是否查看过SDK文档?

Process.StandardError Property Process.StandardError属性

Gets a stream used to read the error output of the application. 获取用于读取应用程序错误输出的流。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM