🔒 Locked
[java] Saving in a Java Applet
Advertisement
Original Post
I have a java applet which should allow for "hours of gameplay" but chances are nobody wants to play the whole game in one go. Is it possible to add a save feature to a java applet? aka Can I write to a text file? If yes, how? If no, can you recommend an alternative that could serve a similar perpose. Thanks in advance. [Edited by - eat2thepieseye on August 8, 2006 11:59:30 AM]
I had already tryed that and I always catch an IOException. When I display the Exception it says there was a null pointer error. Do you know why this might be?
Generally speaking you cannot save files from a java applet. Applets have very strict permissions. However, you may be able to save to the server the applet is running from by accessing another program. If you are running your applet on a server that supports php you can communicate with php from a java applet and use php to save onto the server (and load). It should be possible to save to the server using only java also, but it would probably require you have a java server program actively running on the server and it is likely to be more difficult than using a different language. Java applets can read web pages(only on the same server they are running from) and send form data(POST). So any serverside language that could pick this up could work.
This article explains POSTing with java:
http://www.javaworld.com/javaworld/javatips/jw-javatip34.html
If users are downloading and then playing your game then I suggest it not be an applet. If you are interested in attempting the completely java based "catch and save", I can provide more information and links(but it's more hassle than it's worth in my opinion).
This article explains POSTing with java:
http://www.javaworld.com/javaworld/javatips/jw-javatip34.html
If users are downloading and then playing your game then I suggest it not be an applet. If you are interested in attempting the completely java based "catch and save", I can provide more information and links(but it's more hassle than it's worth in my opinion).
One problem with storing the files server side though is that you don't know what file belongs to what user. So they should have some sort of username/password to save, or you should provide them with a code they need to enter as soon as they want to continue playing.
Furthermore you should probably save the gamestate in a database (which can easily be done in php, that is if you have a database). That will keep it more organized and it is easier than opening files on the server, since the usermode that php is running in usually doesnt have access to your folders (in which case you need to make an ftp connection to chmod a directory to 777 and write your file and chmod it again to 755 or whatever ,which is considered a hack)
Greetings.
Furthermore you should probably save the gamestate in a database (which can easily be done in php, that is if you have a database). That will keep it more organized and it is easier than opening files on the server, since the usermode that php is running in usually doesnt have access to your folders (in which case you need to make an ftp connection to chmod a directory to 777 and write your file and chmod it again to 755 or whatever ,which is considered a hack)
Greetings.
Hi,
I might have a solution although I haven't tested it fully. For a single player game where you want to save the current state of the game on the client side you could use property files.
I use property files in my own applet to know what images to load. Since the java applet is downloaded to the client machine in order to run it I don't see why the property files would not remain so that the next time you load the applet you would be able to get the last state of the game from the property file.
I might have a solution although I haven't tested it fully. For a single player game where you want to save the current state of the game on the client side you could use property files.
I use property files in my own applet to know what images to load. Since the java applet is downloaded to the client machine in order to run it I don't see why the property files would not remain so that the next time you load the applet you would be able to get the last state of the game from the property file.
It is possible to save to or read from a file using applets, but it requires you to write a java .policy file. But even if you do manage to read/save files using your applet, you're still left with the problem of organizing the progress data by each user. You can easily do that using a single XML file on your server, but if you have a lot of "traffic" on your website to play your game, this solution won't be optimal.
Quote:
Original post by Limitz
One problem with storing the files server side though is that you don't know what file belongs to what user. So they should have some sort of username/password to save, or you should provide them with a code they need to enter as soon as they want to continue playing.
Furthermore you should probably save the gamestate in a database (which can easily be done in php, that is if you have a database). That will keep it more organized and it is easier than opening files on the server, since the usermode that php is running in usually doesnt have access to your folders (in which case you need to make an ftp connection to chmod a directory to 777 and write your file and chmod it again to 755 or whatever ,which is considered a hack)
Greetings.
Giving the user a code for their saved game that they would enter after pressing a load game option in your applet would be a very good solution. The only drawback would be that you would be required to have a server either SQL or a custom Java Server to be running in order to handle the saving and loading of the saved game. You could do something as simple as having the Applet send the server an Object which it stores and sends to the client upon request.
public class SaveGameServer implements Runnable { public void run() { Socket inComing = null; while( (inComing = myServerSocket.accept()) != null ) { Thread handleRequest = new Thread( InComingRequest( inComing ) ); handleRequest.start(); } }}public class InComingRequest implements Runnable { public void run() { Object inComing = null; try { inComing = myObjectInputStream.readObject(); GameState gs = (GameState)inComing; String code = gs.getSaveCode(); GameStore.savedGames.put( code, gs ); myObjectOutputStream.writeObject(code); }catch( ClassCastException cce ) { try { String code = (String)inComing; GameState savedGame = (GameState)GameStore.savedGames.get(code); myObjectOutputStream.sendObject(savedGame); }catch( ClassCastException cce2 ) { throw new InvalidRequestException(); } } } }public class GameState { //...All the current game state information }public final GameStore { public static HashMap savedGames = new HashMap();}Don't know how simple that is to everyone! But it's one option.
[Edited by - 5MinuteGaming on August 9, 2006 7:59:31 PM]
5MinuteGaming, I've got a few questions about that code. First off, what is myServerSocket. Is that a new ServerSocket or something else? Also, what are myObjectInputStream and myObjectOutputStream? You also have the line "throw new InvalidRequestException();". Whats a InvalidRequestException(); and why do I need to throw it? Thanks for the help/ideas everyone. I think I'm going to get this to work.
Quote:
Original post by eat2thepieseye
5MinuteGaming, I've got a few questions about that code. First off, what is myServerSocket. Is that a new ServerSocket or something else? Also, what are myObjectInputStream and myObjectOutputStream? You also have the line "throw new InvalidRequestException();". Whats a InvalidRequestException(); and why do I need to throw it? Thanks for the help/ideas everyone. I think I'm going to get this to work.
Sorry about that eat2thepieseye I left most of the declarations and other code out for simplicity's sake.
You are correct myServerSocket is a ServerSocket object set to whatever port that you want to use on your machine or whereever you are running it from.
myObjectOutputStream is an ObjectOutputStream wrapped around the Sockets OutputStream.
Example Declaration:
ObjectOutputStream myObjectOutputStream = new ObjectOutputStream( inComing.getOutputStream() );
In the code snippet above inComing is a Socket object that is returned by the ServerSocket.accept() method.
And myObjectInputStream is similar except that it is wrapped around the Sockets InputStream.
InvalidReqeustException I should have left out of the example since you don't really want it to be thrown up the call stack while the server is running even though it is in a separate thread. But I thought that it might be a good way to notify you that you are receiving garbage through whatever port you decide to use. Oh and by the way it would be a custom Exception object extended from java.lang.Exception
Thanks. I haven't gotten a chance to test it yet but it looks like its going to work. I think, but I'm not sure, that you ment myObjectOutputStream.writeObject(code); instead of .sendObject(code); because sendObject(Object) is not a method. Please correct me if I am wrong about that.
Also, how do I know what port to use for the serversocket, and what's a backlog?
Thanks again for your help.
Also, how do I know what port to use for the serversocket, and what's a backlog?
Thanks again for your help.
eat2thepieseye: no your right I meant writeObject() instead of sendObject.
P.S. You also might want to serialize all the saved games at some point on the server side in case the server crashes or even if the number of saved games gets really large.
P.S. You also might want to serialize all the saved games at some point on the server side in case the server crashes or even if the number of saved games gets really large.
Can Java applets read/write cookies, either directly or via client-side javascript? If so, you could save the game in a cookie.<br/><br/>Another option is to save the game on a server-side database (via php or whatever) and require a username/password to get onto the page that loads the applet. Then you can add the option of cookie login so the players won't have to manually login each time.
Um... I don't know any php, so could you give me a brief summary of how I could do that?
Go to php.net and start reading. It's dead easy to get started. Database communication is a snap.
The easiest way to do this would be send a URL to the php script with all your variables either in the URL itself (GET method) or in the request body (POST method). You will also need to send the username and password. And of course, you will need to encode the data in such a way that it's not easy for cheaters (they will ALWAYS find a way, though). Don't forget to check the data for validity, otherwise you risk SQL injection hacking, which is a very bad thing.
EDIT: I should add here that it's easy for cheaters to sniff the raw http requests and responses. In fact, Microsoft provides a free tool called fiddler that achieves just that. I think it's at fiddler.com.
The easiest way to do this would be send a URL to the php script with all your variables either in the URL itself (GET method) or in the request body (POST method). You will also need to send the username and password. And of course, you will need to encode the data in such a way that it's not easy for cheaters (they will ALWAYS find a way, though). Don't forget to check the data for validity, otherwise you risk SQL injection hacking, which is a very bad thing.
EDIT: I should add here that it's easy for cheaters to sniff the raw http requests and responses. In fact, Microsoft provides a free tool called fiddler that achieves just that. I think it's at fiddler.com.
You'll need to sign the applet, only then may the user of the applet choose to grant the permissions it may require (i.e IO).
I didn't read the other replies, maybe this was already said.
I didn't read the other replies, maybe this was already said.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement