• Welcome to Valhalla Legends Archive.
 

Connection Problem

Started by Spilled, December 25, 2005, 03:08 PM

Previous topic - Next topic

Spilled

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.


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);
}
}
}


I Construct connection when File->Connect is performed.

public void actionPerformed(ActionEvent e)
{
    if(e.getSource() == connect)
    {
         c = new Connection("63.241.83.8", 6112);
        c.run();
    }
}


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:


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);
}


Any ideas?

MyndFyre

Change:

SocketPermission f = new SocketPermission(s, "connect");

to:

SocketPermission f = new SocketPermission(s, "connect,resolve");


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.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Ender

#2
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:

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;
        }
}


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:

Thread t = new Thread(new Runnable() {
       public void run() {
              // implement run method here
       }
});


When you're extending Thread, you only need to override the run method by supplying one:

Class A extends Thread {
      public void run() {
             // implement run method here
      }
}


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:

       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
       }


public interface CommandComponent {
       public void execute();
}


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:

public class ConnectButton extends JButton implements CommandComponent {
      ...
      public void execute() {
             // implement execute method here
      }
}


Quote from: Spilled[DW] on December 25, 2005, 03:08 PM
I Construct connection when File->Connect is performed.

public void actionPerformed(ActionEvent e)
{
    if(e.getSource() == connect)
    {
         c = new Connection("63.241.83.8", 6112);
        c.run();
    }
}


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


dxoigmn

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.

Spilled

Quote from: dxoigmn on December 28, 2005, 05:54 PM
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.

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?

dxoigmn

Quote from: Spilled[DW] on December 28, 2005, 11:39 PM
Quote from: dxoigmn on December 28, 2005, 05:54 PM
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.

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?

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.

Ender

Do it in an application instead of an applet.

Spilled

What do you mean by 'application' , still new to java so bare with me on my questions.

Ender

#8
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 from: dxoigmn on December 28, 2005, 05:54 PM
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.

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

MyndFyre

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.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Spilled

Quote from: MyndFyre on January 02, 2006, 11:24 AM
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.

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.