C# 无法从对服务器的rcon请求获得完整响应
我使用C#查询任务召唤4 rcon以获取玩家的状态,它工作正常,但似乎没有收到超过1303个字符的响应C# 无法从对服务器的rcon请求获得完整响应,c#,.net,sockets,C#,.net,Sockets,我使用C#查询任务召唤4 rcon以获取玩家的状态,它工作正常,但似乎没有收到超过1303个字符的响应 public string sendCommand(string rconCommand, string gameServerIP, string password, int gameServerPort) { Socket client = new Socket(AddressFamily.InterNetwork,
public string sendCommand(string rconCommand, string gameServerIP,
string password, int gameServerPort)
{
Socket client = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram,
ProtocolType.Udp);
client.Connect(IPAddress.Parse(gameServerIP), gameServerPort);
string command;
command = "rcon " + password + " " + rconCommand;
byte[] bufferTemp = Encoding.ASCII.GetBytes(command);
byte[] bufferSend = new byte[bufferTemp.Length + 4];
bufferSend[0] = byte.Parse("255");
bufferSend[1] = byte.Parse("255");
bufferSend[2] = byte.Parse("255");
bufferSend[3] = byte.Parse("255");
int j = 4;
for (int i = 0; i < bufferTemp.Length; i++)
{
bufferSend[j++] = bufferTemp[i];
}
IPEndPoint RemoteIpEndPoint
= new IPEndPoint(IPAddress.Parse(gameServerIP), 0);
client.Send(bufferSend, SocketFlags.None);
byte[] bufferRec = new byte[64999];
client.Receive(bufferRec);
return Encoding.ASCII.GetString(bufferRec);
}
publicstringsendcommand(stringrconcommand,stringgameserverip,
字符串密码,int gameServerPort)
{
套接字客户端=新套接字(AddressFamily.InterNetwork,
SocketType.Dgram,
ProtocolType.Udp);
client.Connect(IPAddress.Parse(gameServerIP)、gameServerPort);
字符串命令;
command=“rcon”+密码+“”+rconCommand;
byte[]bufferTemp=Encoding.ASCII.GetBytes(命令);
字节[]bufferSend=新字节[bufferTemp.Length+4];
bufferSend[0]=byte.Parse(“255”);
bufferSend[1]=byte.Parse(“255”);
bufferSend[2]=byte.Parse(“255”);
bufferSend[3]=byte.Parse(“255”);
int j=4;
对于(int i=0;i
显然,其他人似乎对此没有问题,但我对此有问题。有人有什么想法吗?我对Quake RCON协议的记忆很模糊,但我相信特殊的头是5个字节,
0xFF 0xFF 0xFF 0x02
。如果COD4的情况并非如此,则忽略此建议
此外,您不能依赖于对Socket.Receive的一次调用中接收的所有数据。而我在雷神RCON的经历并不总是最好的,因为“正如预期的那样”
什么是client.Receive
return?client.Receive returns System.Byte[]工作了好几次,但当我尝试在40插槽服务器上执行此操作时,它只能再次接收1303个字符。我经常发现rcon可能不可靠(这可能是一个丑陋的问题)您可以尝试在响应之后添加Thread.Yield
。Append
以查看客户端是否存在计时问题。可用的。Thread.Yield()没有帮助,将一些代码从2.5转换为4让我头疼。不知道什么不起作用,我也不知道如何让它起作用。啊哈,明白了。我想,我告诉线程睡眠10毫秒并删除???打印\n这更多的是解决症状而不是原因,基本上你需要阅读,直到你认为没有了,这很难说!服务器可以发送任意数量的数据报。网络堆栈也可能会将它们分解。基本上,您的线程。Sleep
为其他数据报的到达提供了足够的时间。
byte[] bufferSend = new byte[bufferTemp.Length + 5 ];
bufferSend[0] = 0xFF;
bufferSend[1] = 0xFF;
bufferSend[2] = 0xFF;
bufferSend[3] = 0xFF;
bufferSend[4] = 0x02;
Buffer.BlockCopy(bufferTemp, 0, bufferSend, 5, bufferTemp.Length);
...
StringBuilder response = new StringBuilder();
byte[] bufferRecv = new byte[65536];
do
{
// loop on receiving the bytes
int bytesReceived = client.Receive(bufferRecv);
// only decode the bytes received
response.Append(Encoding.ASCII.GetString(bufferRecv, 0, bytesReceived));
} while (client.Available > 0);
return response.ToString();