开发者

Manage Standard Input/Output in another Thread?

The basic rundown is...

1) My application starts a Java process and manages it.

2) I have a TCP Server in another thread that users can connect to and issue commands that the Java process understands.

3) Passing the commands is not a problem is 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.

This is what my TCP Server's function looks like that needs to get the reply and print it out. 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?

Process.StandardError Property

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜