Author | Message | Time |
---|---|---|
Spilled[DW] | Still getting familiar with Java, so bare with me guys. I'm getting an error when I attempt to connect to a battle.net server. Here's some code. [code] import java.io.*; import java.net.*; public class Connection extends Main implements Runnable { private Socket wSock = null; private String s; private int p; public Connection() { s = "uswest.battle.net"; p = 6112; } public Connection(String server, int port) { s = server; p = port; } public void run() { try { wSock = new Socket(s,p); System.out.println("Connected to: " + wSock); }catch(Exception e){ System.out.println("Error: " + e); } } } [/code] I Construct connection when File->Connect is performed. [code] public void actionPerformed(ActionEvent e) { if(e.getSource() == connect) { c = new Connection("63.241.83.8", 6112); c.run(); } } [/code] Any Ideas? Thanks in advance guys! Edit: Heres my error java.security.AccessControlException: access denied <java.net.SocketPermission 63.241.83.8:6112 connect,resolve> I've looked into SocketPermission due to Hdx sending me in that dirrection, but I still have failed to get this working. Here's how I am doing the SocketPermissions: [code] try { SocketPermission f = new SocketPermission(s, "connect"); wSock = new Socket(s,p); System.out.println("Connected to: " + wSock); }catch(Exception e){ System.out.println("Error: " + e); } [/code] Any ideas? | December 25, 2005, 9:08 PM |
Myndfyr | Change: [code] SocketPermission f = new SocketPermission(s, "connect"); [/code] to: [code] SocketPermission f = new SocketPermission(s, "connect,resolve"); [/code] Note that you're not passing an InetAddress to the constructor, so the constructor needs to resolve the address too. Note also that "resolve" is part of the responding error. | December 28, 2005, 1:39 AM |
Ender | Hm, I've never used the SocketPermission class, I just use Socket and it works fine for me. I can't see why the AccessControlException is being called. So I'll do my best to help while getting around that main issue! (sorry) Few suggestions: 1) Try this code out and tell us whether it works: [code] public class Connection { // IO objects private Socket sock; private InputStream in; private OutputStream out; // server and port private String server; private int port; public Connection(String server, int port, boolean connect) { this.server = server; this.port = port; if (connect) connect(); } public void connect() { try { sock = new Socket(server, port); in = sock.getInputStream(); out = sock.getOutputStream(); } catch (IOException e) { // ... } } public void disconnect() { try { sock.close(); in.close(); out.close(); } catch (IOException e) { // ... } sock = null; in = null; out = null; } public Socket getSocket() { return sock; } public InputStream getInStream() { return in; } public OutputStream getOutStream() { return out; } public String getServer() { return server; } public int getPort() { return port; } } [/code] 2) You extend Main which I'm guessing is a Thread. There is no reason to both extend Thread and implement Runnable, the reason being because Thread already implements Runnable, as shown here. So every Thread object will already have a run method. It's the programmers job to define this run method, and the way to do this on the fly is to override Thread's built-in Runnable interface with a new one: [code] Thread t = new Thread(new Runnable() { public void run() { // implement run method here } }); [/code] When you're extending Thread, you only need to override the run method by supplying one: [code] Class A extends Thread { public void run() { // implement run method here } } [/code] 3) When you call e.getSource() you get an Object in return. I'm not sure exactly what will happen when you do if ( (Object) o == (JMenuItem) menueItem). There are two possibilities: a) It will be true for everything you set equal to on the right side, so if you set it equal to a JButton it will return true even when the JButton was never clicked, and instead a JMenuItem was clicked b) It will never be true, because to check if two objects "==" eachother the two objects need to be of the same type So the way to do it is by casting the e.getSource() (which returns an Object) back to the class of object that you want to check. The way that I often do it, if I have many graphical components, is by grouping my components together by interface. Then I say if the event generated is an instance of this interface type, tell whichever one acted upon to execute. This is bad wording on my part, but basically the beauty of it is you don't have to know which component was acted upon, you just have to know its type, and since all (or a certain group) of components have the same type, you can do: [code] public class Ears implements ActionListener, MouseListener, KeyListener { public void actionPerformed(ActionEvent aEvt) { if (aEvt.getSource() instanceof CommandComponent) { CommandComponent cmd = (CommandComponent)aEvt.getSource(); cmd.execute(); // every command remembers the implementation of it's execute method (polymorphism). The program // stores the type of every CommandComponent in a table to remember. // this is how the instanceof operator works: even though the getSource() returns an object, // the Object remembers its immediate type and is able to answer the instanceof operator // You have to cast the Object back to a CommandComponent since CommandComponent has the execute // method whereas Object doesn't. } } // rest of interface } [/code] [code] public interface CommandComponent { public void execute(); } [/code] And now you create classes for each of your important controls (graphical components) and make them each extend the type of component they are (i.e. JButton, JMenuItem) and implement the CommandComponent interface. Example: [code] public class ConnectButton extends JButton implements CommandComponent { ... public void execute() { // implement execute method here } } [/code] [quote author=Spilled[DW] link=topic=13649.msg139245#msg139245 date=1135544885] I Construct connection when File->Connect is performed. [code] public void actionPerformed(ActionEvent e) { if(e.getSource() == connect) { c = new Connection("63.241.83.8", 6112); c.run(); } } [/code] [/quote] 4) Instantiating objects in threads (yea, technically everything is a thread, there is a "Main" thread, I mean outside of this) which will need to be used throughout the program is a bad idea because then you get into the issue of threads racing eachother... basically one thread can be ahead of another thread, and call for an object who is going to be instantiated in that other thread but has not been instantiated yet. I learnt this the hard way, and spent ~45 minutes debugging a program for a null pointer exception when I found out that one thread had gotten ahead of the other. 5) Eh, this is just a minor note, maybe you didn't implement this in your actual code but just put it in the code you posted. After connecting you do System.out.println("Connected to: " + wSock) where wSock is a Socket. The println method doesn't take Sockets as a parameter. You need to put your server String in there. Same thing where you print out your 'e' Exception, you need to put e.toString(), which returns a String representation of the error. EDIT: http://java.sun.com/j2se/1.4.2/docs/api/java/security/AccessControlException.html | December 28, 2005, 8:56 PM |
kamakazie | Are you running this as an applet? If so, that would explain the permissions errors. You are going to have a hard time getting around all the sandbox restrictions applets must obey. | December 28, 2005, 11:54 PM |
Spilled[DW] | [quote author=dxoigmn link=topic=13649.msg139673#msg139673 date=1135814079] Are you running this as an applet? If so, that would explain the permissions errors. You are going to have a hard time getting around all the sandbox restrictions applets must obey. [/quote] eh? So do you suggest I just do this in a JFrame? MyndFyre: I Tried your sollution and still doesn't want to work. Due to what dxoigmn has replied, maybe I should move to a JFrame? | December 29, 2005, 5:39 AM |
kamakazie | [quote author=Spilled[DW] link=topic=13649.msg139716#msg139716 date=1135834764] [quote author=dxoigmn link=topic=13649.msg139673#msg139673 date=1135814079] Are you running this as an applet? If so, that would explain the permissions errors. You are going to have a hard time getting around all the sandbox restrictions applets must obey. [/quote] eh? So do you suggest I just do this in a JFrame? MyndFyre: I Tried your sollution and still doesn't want to work. Due to what dxoigmn has replied, maybe I should move to a JFrame? [/quote] A JFrame would probably work. You also need to create a static main method which will create an instance of the JFrame, and show it. | December 29, 2005, 8:52 AM |
Ender | Do it in an application instead of an applet. | December 29, 2005, 3:09 PM |
Spilled[DW] | What do you mean by 'application' , still new to java so bare with me on my questions. | December 30, 2005, 7:01 AM |
Ender | There are two kinds of Java programs, applications and applets. A JFrame is not a type of Java program, it's just a class you can use for your GUI. For distinction between the two: applications have the public static void main() method to start up, whereas applets have the init method for startup. If you are doing an applet, switch to an application, because:[quote author=dxoigmn link=topic=13649.msg139673#msg139673 date=1135814079] Are you running this as an applet? If so, that would explain the permissions errors. You are going to have a hard time getting around all the sandbox restrictions applets must obey. [/quote] Also, try the code I provided earlier. If you want to try it out with a bnet telnet client, you can message me on aim for the code: sparky71989 | January 2, 2006, 2:55 AM |
Myndfyr | Yeah, you'll always generate permissions errors when you're using an embedded Java program. You can configure Java to grant these permissions to applets, but that's not recommended. I find it hard to believe that deriving from JFrame would make a difference; are you hosting the applet in a web browser? Applications are typically only fully-trusted when run from the local machine. | January 2, 2006, 5:24 PM |
Spilled[DW] | [quote author=MyndFyre link=topic=13649.msg140108#msg140108 date=1136222654] Yeah, you'll always generate permissions errors when you're using an embedded Java program. You can configure Java to grant these permissions to applets, but that's not recommended. I find it hard to believe that deriving from JFrame would make a difference; are you hosting the applet in a web browser? Applications are typically only fully-trusted when run from the local machine. [/quote] Yea eventually i wanted to host it in a web browser eventually, but for now im just getting familiar. Thanks Myndfyre Ender: Im interested in that, ill be messaging you on AIM soon but for now i must head to work. Thanks all. | January 3, 2006, 12:13 AM |