C# - Unable to get server response from UDP - Black Ops Rcon
I'm creating an RCON web application for Call of Duty Black Ops. COD uses rcon and udp packets to send and receive information. Using the following code, I've been able to send and receive information with a COD4 server. Now that COD7 is out, 开发者_如何学JAVAI'm no longer receiving responses back.
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
client.Connect(IPAddress.Parse(gameServerIP), gameServerPort);
string command;
command = password + " " + rconCommand;
byte[] bufferTemp = Encoding.ASCII.GetBytes(command);
byte[] bufferSend = new byte[bufferTemp.Length + 5];
//intial 5 characters as per standard
bufferSend[0] = byte.Parse("255");
bufferSend[1] = byte.Parse("255");
bufferSend[2] = byte.Parse("255");
bufferSend[3] = byte.Parse("255");
bufferSend[4] = byte.Parse("02");
int j = 5;
for (int i = 0; i < bufferTemp.Length; i++)
{
bufferSend[j++] = bufferTemp[i];
}
//send rcon command and get response
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
client.Send(bufferSend, SocketFlags.None);
//big enough to receive response
byte[] bufferRec = new byte[65000];
client.Receive(bufferRec);
Does anyone have any ideas? Black Ops ships with its own Rcon tool that I've tried using Wireshark to capture the outgoing packets to copy. The outgoing packets between my application and theirs are next to identical, except I get no replies back when I use mine.
i knwo why cause i make a tool myself.
What's wrong in your code is that : bufferSend[4] = byte.Parse("02");
the good one is : bufferSend[4] = byte.Parse("00");
Try it, works for me!
Here my piece of code, i use a thread to run it:
how to do:
in your class set the private command, then call the worker, when thread finish, just read the private response.
private IPEndPoint ipendpoint;
private string command;
private string response;
private void worker()
{
UdpClient client = new UdpClient();
var result1 = string.Empty;
var result2 = string.Empty;
bool sent = false;
bool DoubleTrame = false;
Byte[] bufferTemp = Encoding.ASCII.GetBytes(this.command);
Byte[] bufferSend = new Byte[bufferTemp.Length + 5];
Byte[] bufferRec;
Byte[] bufferRec2;
bufferSend[0] = Byte.Parse("255");
bufferSend[1] = Byte.Parse("255");
bufferSend[2] = Byte.Parse("255");
bufferSend[3] = Byte.Parse("255");
bufferSend[4] = Byte.Parse("00");
for (int i = 0; i < bufferTemp.Length; i++)
{
bufferSend[i + 5] = bufferTemp[i];
}
while (!sent)
{
client.Send(bufferSend, bufferSend.Length, ipendpoint);
Thread.Sleep(200);
if (client.Available > 0)
{
sent = true;
if (client.Available > 1200)
{
DoubleTrame = true;
}
}
}
bufferRec = client.Receive(ref ipendpoint);
if (DoubleTrame)
{
bufferRec2 = client.Receive(ref ipendpoint);
result2 = Encoding.ASCII.GetString(bufferRec2);
if (result2.Contains("\n\n"))
{
result2 = result2.Remove(result2.IndexOf("\n\n"));
}
result2 = result2.Substring(12);
}
result1 = Encoding.ASCII.GetString(bufferRec);
this.response = result1 + result2;
}
I am doing a similar thing using vb.net I have written rcon tools for COD MW and COD WW no problem however so far I have not been able to get a response back from my black ops server.
In fact, i have done the saame thing as you, i used WireShark to look the byte sent and received with the default rconTool provide in steam for Black Ops.
Other tip:
The command "status" give you many information about the current players but not the team of each.
You better use "teamstatus" instead, this one give you the same information but with the team of each player.
I have trouble right now to perfectly parse the response, but to give a readeable answer use this:
replace :
byte[] bufferRec = new byte[65000];
client.Receive(bufferRec);
by:
byte[] bufferRec;
while (client.Available == 0)
{
Thread.Sleep(10);
}
client.Receive(bufferRec);
string response=Encoding.ASCII.GetString(bufferRec);
var list= response.Split('\n');
This way you'll have an array with each player separate in a row.
Btw: i'm mack, i'm now registered, wasn't this night
Edit: oups, i didn't notice you've already tried the teamstatus command.
You need to look at the number return by client.Avalaible, because the server only send 1168 byte at once, so if client.Avalaible>1168, you need a second buffer to get the second flow with client.receive.
In fact there's only two possible number for client.avalaible : 1168 and 2336 (yes, the double) i don't know why, but they didn't managed to send the exact number of data, the buffer is always full or empty.
I noticed also that the second receive() is like "paste" on the first buffer.
YOu'll have at hte begginig the complementary information of the first Receive(), then the "noise" of the old one.
Just take a look, you will see what i mean.
I'm at work now, but this evening i will post my code to help.
精彩评论