Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: [Java] Sockets

  1. #1
    Client Beta Testers Appelpitje's Avatar
    Join Date
    Jan 2012
    Location
    Belgium
    Posts
    571

    Default [Java] Sockets

    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;
    }
    Last edited by Appelpitje; October 22nd, 2015 at 12:16 PM.

  2. #2
    Administrator James's Avatar
    Join Date
    May 2010
    Location
    on the intraweb
    Posts
    3,180

    Default

    The app is just "hanging" and not responding, cant even find an error in the console.


    Do you know where it's hanging? Try doing some outputs either printf() or messageBox() (or whatever equivalent in java there is)...

    I usually do it before and after stuff gets declared, assigned or some sort of calculation is done to see if it's hanging...

    It could possibly be a buffer over flow, maybe you're passing incorrect values to another variable, maybe it's hanging because it's failing somewhere so you could also try returning the exception to see what's going on.

    BTW, I hate java, so I won't be too much help, but since programming is similar in debugging, those are some things I would start with in terms of troubleshooting.

    maybe try something like:

    int size = Integer.MAX_VALUE;
    byte[] bufferRec = new byte[size];

  3. #3
    Client Beta Testers Appelpitje's Avatar
    Join Date
    Jan 2012
    Location
    Belgium
    Posts
    571

    Default

    When i call the method that i put here i get ANR (Not Responding dialog):
    An ANR happens when some long operation takes place in the "main" thread. This is the event loop thread, and if it is busy, Android cannot process any further GUI events in the application, and thus throws up an ANR dialog.
    I may have found some code that works, will test it out!

  4. #4
    Über Prodigy & Developer Razo[R]apiD's Avatar
    Join Date
    May 2010
    Location
    Poland, Lublin
    Posts
    3,257

    Default

    You can't make HTTP or any other socket calls on main thread on Android. It will always fail.

    Create an AsyncTask, or a Thread, and run your logic there. Also you set header to be 0xFFFFFF01, while your C# example has 02 in the end.

  5. #5
    Client Beta Testers Appelpitje's Avatar
    Join Date
    Jan 2012
    Location
    Belgium
    Posts
    571

    Default

    Quote Originally Posted by Razo[R]apiD View Post
    You can't make HTTP or any other socket calls on main thread on Android. It will always fail.

    Create an AsyncTask, or a Thread, and run your logic there. Also you set header to be 0xFFFFFF01, while your C# example has 02 in the end.
    Oo, didn't know that, thanks for the info!
    And yup made a typo there.

  6. #6
    Client Beta Testers Appelpitje's Avatar
    Join Date
    Jan 2012
    Location
    Belgium
    Posts
    571

    Default

    I started making a Java API for rcon calls and finally found how to connect to a moh server.. lol

    I'll put it here in case anyone lands here or when i lose it

    import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    import java.net.SocketException;
    import java.net.UnknownHostException;

    public class RCONconnector {
    private final String IP;
    private final int port;
    private final String rconpassword;

    private final DatagramSocket clientSocket;

    private final byte pre[] = {(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x02};


    public RCONconnector(String IP, int port, String rconpassword) throws SocketException, UnknownHostException{
    this.IP = IP;
    this.port = port;
    this.rconpassword = rconpassword;

    clientSocket = new DatagramSocket();
    }

    public void sendRcon(String command) throws UnknownHostException, IOException{
    String total = new String(pre) + "rcon " + rconpassword + " " + command;

    DatagramPacket sendPacket = new DatagramPacket(total.getBytes(), total.length(), InetAddress.getByName(IP), port);
    clientSocket.connect(InetAddress.getByName(IP), port);
    clientSocket.send(sendPacket);
    System.out.println("SENT");

    DatagramPacket receivePacket = new DatagramPacket(new byte[65565], 65565);
    clientSocket.receive(receivePacket);

    String received = new String(receivePacket.getData(),0,receivePacket.getLength());
    String output = received.split(" ")[2].trim();
    clientSocket.close();
    System.out.println(received);
    }
    }


    Might also return to the Android app and finish it..

  7. #7

    Default

    Nice work Appelpitje

  8. #8
    Senior Member Ancient Order's Avatar
    Join Date
    Aug 2015
    Location
    Paris, Fr.
    Posts
    256

    Default

    For java apps dev, you want to use Eclipse that has a cool debug mode.

  9. #9
    Client Beta Testers Appelpitje's Avatar
    Join Date
    Jan 2012
    Location
    Belgium
    Posts
    571

    Default

    I prefer Netbeans over IntelliJ and eclipse, less problems with Netbeans than the other 2.

  10. #10

    Default

    Nice work!
    Can u post the link of the app?
    I am looking for some tool to remotely send commands to my server

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •