28
Oct
Black Art of Java Game Programming by Joel
public void finalize() { try { ps.close(); dis.close(); theSock.close(); } catch(Exception e); theSock = null; } public void setAlias(String str) { alias = str; } public String getAlias() { return alias; } } Input all of this, and compile. It should compile just fine, although it certainly won t run too well. Now that it is compiled, take a moment and go back and compile the other three classes we just wrote. Once you re done with that, we are all set, because now we have compiled versions of all four classes. Now we can work on sClientThread without worrying about crashing the compiler. With that out of our way, let s take sClientThread from the top! Writing the Real sClientThread Code Let s start with the basics. Remember that sClientGroup is going to be creating sClientThreads and passing them messages. The parent Variable The sClientGroup class expects that these sClientThreads will pass messages back by calling handleInput(). Therefore, not only does sClientGroup need to have a reference to sClientThread, but sClientThread must know who its parent sClientGroup is. That is why we have a variable called parent, which is an sClientGroup (although in the code above, it was an Object. This only served to allow compilation from now on, parent will be an sClientGroup). The initialization code is really simple. All we do is declare a group of global variables, and then remember the values passed to us by our parent: import java.net.*; import java.lang.*; import java.io.*; import java.util.*; import sClientGroup; public class sClientThread extends Thread { sClientGroup parent; Socket theSock; DataInputStream dis;
Note: If you are looking for cheap and quality provider to host and run your java application check Astra java hosting services
