基本纲要是......
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);
}
}
}
答案 0 :(得分:0)