tags:

views:

32

answers:

2

OK so what's the problem with this. I tried using MySQL JConnector for Java and people say not to because it can comprise your applet details so then I told them I'd use PHP $_GET method URLs using PHP scripts. They said it would be fine. However, I find two problems with that.

1.) They are slow. It takes at least 4-5 seconds for the URLConnection to happen. Even when I point it to localhost.

2.) It's a lot of code. Maybe I'm just doing it wrong?

Here is what I have -- which works!

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.Applet;
import java.awt.TextArea.*;
import java.util.*;
import java.net.*;
import java.applet.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class test extends JApplet
{
    public JTextArea c;

    public void init()
    {
        c = new JTextArea();
        add(c);
        c.append("Let's change the level!");
        try
        {
            URL game = new URL("http://localhost/mystikrpg/game.php?act=stats&username=Dan");
            URLConnection connection = game.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String inputLine;
            while ((inputLine = in.readLine()) != null)
            {
                String command = inputLine;
                System.out.println(command);
                String[] temp = command.split("\\|");
                c.append("\nYou're Lvl is: " + temp[1]);
            }
            in.close();

            c.append("\nTrying to update level...");
            String newLevel = "777";
            URL newGame = new URL("http://localhost/mystikrpg/game.php?act=updateLvl&username=Dan&lvl=" + newLevel);
            URLConnection levelConnection = newGame.openConnection();
            BufferedReader level_BR = new BufferedReader(new InputStreamReader(levelConnection.getInputStream()));

            URL updateLevelURL = new URL("http://localhost/mystikrpg/game.php?act=stats&username=Dan");
            URLConnection up_lvl_conn = updateLevelURL.openConnection();
            BufferedReader up_lvl_br = new BufferedReader(new InputStreamReader(up_lvl_conn.getInputStream()));
            String getLvl;

            while ((getLvl = up_lvl_br.readLine()) != null)
            {
                String[] newLvl = getLvl.split("\\|");
                c.append("\nYou're NEW Lvl is: " + newLvl[1]);
                // newLvl[1] == newLevel
            }
            c.append("\nLevel update done!");

            level_BR.close();
            up_lvl_br.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

and here is the response:

Let's change the level!
You're Lvl is: 123456
Trying to update level...
You're NEW Lvl is: 777
Level update done!

It works but it's slow and bulky - how do I fix this problem?

+1  A: 

I would make sure your MySQL database has proper indexes setup. Not having proper MySQL indexes setup can slow your code down a ton!

But if it is your PHP code that is causing the issue, you should post that as appose to the Java side. Since the PHP Code deals with the database, my bet is that is where the hold up is.

Brad F Jacobs
OK here it is. I'll try indexing. Can I index ALL of my databse?
Dan
http://www.asdasdf.pastebin.com/wc040XuV
Dan
It is not recommended to index the full database. Given you are only using `username` you should index `username` and see how that works out for you.
Brad F Jacobs
How do i UNindex something then?
Dan
OK I found out it's DROP INDEX `username` ON `user`;... but ANWYAYS should I just index the `level` because thats what I am changing?
Dan
You can try it, but you want to keep the index on `username`.
Brad F Jacobs
Nope didn't work. Indexing did nothing.
Dan
THIS IS GETTING DUMB. Seriously. #1091 - Can't DROP 'exp'; check that column/key exists `DROP INDEX exp ON user`WHAT THE HECK
Dan
OK I fixed that but anyways indexing JUST username did nothing. :(
Dan
Well, the PHP Code looks fine, other then the obvious SQL Injection possibility (not escaping the string coming in from GET). So it probably has to do with the Java code, and I am not a java programmer. So here ends my help :)
Brad F Jacobs
Ah, darn... OK. Thanks :) *waits for java experts* :P
Dan
A: 

The problem is that URLConnection is actually pretty slow. You could try passing the Proxy.NO_PROXY parameter to the openConnection, but it won't make it superfast. (1 to 3 seconds per call, so you're still using up a lot of time) An alternative to the HttpConnection could be the Jakarta Commons HttpClient.

It's actually also quite funny that the url's to update your level are left open: if someone finds out about that, you're going to have a whole lot of cheaters using your game.

Kurt Du Bois
I'm going to obscufate (sp?) the code :)
Dan
Besides what else can I do? :\ The MySQL java thing is vulnerable and the URLConnection is slow... so yeah... :|
Dan