Since im working on the android app i am stuck on the sockets part in java.
This is an example that works in C#, i tried to manually convert it to java but no luck so far..
Anyone with some Java knowledge that can help me?
The app is just "hanging" and not responding, cant even find an error in the console.
C#
public string sendCommand(string rconCommand, string gameServerIP, string password, int gameServerPort)
{
//connecting to server
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 + 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);
return Encoding.ASCII.GetString(bufferRec);
}
Java
public String sendCommand(String rconCommand, String gameServerIP, String password, int gameServerPort) throws Exception {
/* connecting to server */
DatagramSocket clientSocket = new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName(gameServerIP);
String command;
command = "rcon " + password + " " + rconCommand;
byte[] bufferTemp = command.getBytes();
byte[] bufferSend = new byte[bufferTemp.length + 5];
/* initial 5 characters as per standard */
bufferSend[0] = (byte)0xff;
bufferSend[1] = (byte)0xff;
bufferSend[2] = (byte)0xff;
bufferSend[3] = (byte)0xff;
bufferSend[4] = (byte)0x1;
int j = 5;
for (int i = 0; i < bufferTemp.length; i++) {
bufferSend[j++] = bufferTemp[i];
}
DatagramPacket sendPacket = new DatagramPacket(bufferSend, bufferSend.length, IPAddress, gameServerPort);
clientSocket.send(sendPacket);
/* big enough to receive response */
byte[] bufferRec = new byte[65000];
DatagramPacket receivePacket = new DatagramPacket(bufferRec, bufferRec.length);
clientSocket.receive(receivePacket);
String modifiedSentence = new String(receivePacket.getData());
return modifiedSentence;
}