why the hmmm?
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
Show posts Menu[/][/]
import java.io.*;
import java.net.*;
/**
* Sends commands to a Moh game server via UDP and recieves result
*
* @author James Luck
* @version 1.1 26/11/05
*/
public class ServerComm
{
//initialize vars
private int port;
private String command;
private InetAddress ipAddress;
private DatagramPacket dps;
private DatagramPacket dpr;
private String password;
/**
* * Consturctor for the ServerComm Class
*/
public ServerComm(String address, int port, String password)
{
this.port = port;
this.password = password ;
parseAddress(address);
}
/**
* Method to parse the string address into a useable, valid java-net ip address
*/
private void parseAddress(String address)
{
try {
ipAddress = InetAddress.getByName(address);
System.out.println(ipAddress);
}
catch(IOException ex){
}
}
/**
* Method that sends rcon commands to a specified Moh server and retrieves the result
*/
public String sendCommand(String rconCommand)
{
//Command to be sent to server
command = "xxxxxrcon " + password + " " + rconCommand + "xx";
try{
//Create new UDP connection and connect to specified ip address + port
DatagramSocket dp = new DatagramSocket();
dp.connect(ipAddress, port);
dp.setSoTimeout(2000);
//Return the command as bytes
byte[] sender = command.getBytes();
//Replace first 5 chars to create OOB (out of band) header
byte oob = (byte)0xff;
for(int i = 0; i < 4; i++){
sender[i] = oob;
}
sender[4] = (byte)0x02;
//Replace last 2 chars to create packet terminating chars
sender[command.length() - 2 ] = (byte)0x0a;
sender[command.length() - 1] = (byte)0x00;
//Initialize and send command as UDP packet to specified ip address and port
dps = new DatagramPacket(sender, sender.length, ipAddress, port);
dp.send(dps);
System.out.println("Sending command to " + ipAddress + ":" + port);
//Create recieve buffer
byte[] buffer = new byte[4000];
//recieve command
dpr = new DatagramPacket(buffer,buffer.length);
dp.receive(dpr);
System.out.println("Recieved command from " + ipAddress + ":" + port);
}
catch(IOException ex){
//Catch failed command send/recieve
System.out.println("error: Send command failed");
}
//Return recieved packet as substring if command sent is "status"
if (rconCommand.equals("status")){
int sendFrom = new String(dpr.getData(),0,dpr.getLength()).indexOf("-----n");
return new String(dpr.getData(),0,dpr.getLength()).substring(sendFrom + 7);
}
//Else- return recieved packet in full
else{
return new String(dpr.getData(),0,dpr.getLength());
}
}
}