Page 1 of 2 12 LastLast
Results 1 to 20 of 23
  1. #1
    henkee is offline Member
    Join Date
    Jun 2010
    Posts
    11
    Rep Power
    0

    Default java.net.SocketPermission error

    Hello,

    I am trying to make a chat for my website, and therefore I downloaded a simple client/server chat from JavaWorld - December - Building an Internet chat system.

    The server works fine, and so does the desktop based chat too. The problem is in the applet client, when I run it I get this error:
    Java Code:
    Exception in thread "Thread-4" java.security.AccessControlException: 
    access denied (java.net.SocketPermission 127.0.0.1:9830 connect,resolve)
    When I googled about this, I only found that signing the applet would solve the problem, but isn't it any other way cause it isn't very fun to sign the applet each time I want to check if it works.

    This code is from 1996, but I don't find any pretty simple that are newer.
    If you would like the source code, here you go:
    Java Code:
    /**
     * ChatApplet.java  1.00 96/11/01 Merlin Hughes
     *
     * Copyright (c) 1996 Prominence Dot Com, Inc. All Rights Reserved.
     *
     * Permission to use, copy, modify, and distribute this software
     * for non-commercial purposes and without fee is hereby granted
     * provided that this copyright notice appears in all copies.
     *
     * http://prominence.com/                  merlin@prominence.com
     */
    
    import java.net.*;
    import java.io.*;
    import java.awt.*;
    import java.applet.*;
    
    // Applet parameters:
    //   host = host name
    //   port = host port
    
    public class ChatApplet extends Applet implements Runnable {
      protected DataInputStream i;
      protected DataOutputStream o;
    
      protected TextArea output;
      protected TextField input;
    
      protected Thread listener;
    
      public void init () {
        setLayout (new BorderLayout ());
        add ("Center", output = new TextArea ());
        output.setEditable (false);
        add ("South", input = new TextField ());
        input.setEditable (false);
      }
    
      public void start () {
        listener = new Thread (this);
        listener.start ();
      }
    
      public void stop () {
        if (listener != null)
          listener.stop ();
        listener = null;
      }
    
      public void run () {
        try {
          String host = getParameter ("host");
          if (host == null)
    	host = getCodeBase ().getHost ();
          String port = getParameter ("port");
          if (port == null)
    	port = "9830";
          output.appendText ("Connecting to " + host + ":" + port + "...");
          Socket s = new Socket (host, Integer.parseInt (port));
          i = new DataInputStream (new BufferedInputStream (s.getInputStream ()));
          o = new DataOutputStream (new BufferedOutputStream (s.getOutputStream ()));
          output.appendText (" connected.\n");
          input.setEditable (true);
          input.requestFocus ();
          execute ();
        } catch (IOException ex) {
          ByteArrayOutputStream out = new ByteArrayOutputStream ();
          ex.printStackTrace (new PrintStream (out));
          output.appendText ("\n" + out);
        }
      }
    
      public void execute () {
        try {
          while (true) {
            String line = i.readUTF ();
            output.appendText (line + "\n");
          }
        } catch (IOException ex) {
          ByteArrayOutputStream out = new ByteArrayOutputStream ();
          ex.printStackTrace (new PrintStream (out));
          output.appendText (out.toString ());
        } finally {
          listener = null;
          input.hide ();
          validate ();
          try {
            o.close ();
          } catch (IOException ex) {
            ex.printStackTrace ();
          }
        }
      }
    
      public boolean handleEvent (Event e) {
        if ((e.target == input) && (e.id == Event.ACTION_EVENT)) {
          try {
            o.writeUTF ((String) e.arg);
            o.flush ();
          } catch (IOException ex) {
            ex.printStackTrace();
            listener.stop ();
          }
          input.setText ("");
          return true;
        } else if ((e.target == this) && (e.id == Event.WINDOW_DESTROY)) {
          if (listener != null)
            listener.stop ();
          hide ();
          return true;
        }
        return super.handleEvent (e);
      }
    }
    Thanks for any help!

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,435
    Rep Power
    25

    Default

    How are you loading the html with the applet code?
    From the server or from a local disk?

  3. #3
    henkee is offline Member
    Join Date
    Jun 2010
    Posts
    11
    Rep Power
    0

    Default

    Thanks for the help, when I uploaded it to a server it works. Now the only problem is that when I change the IP from 127.0.0.1 to my ip, it doesn't work anymore. I set the port to 440, and my server listen on 440 too.

    Chat Server source:
    Java Code:
    /**
     * ChatServer.java  1.00 96/11/07 Merlin Hughes
     *
     * Copyright (c) 1996 Prominence Dot Com, Inc. All Rights Reserved.
     *
     * Permission to use, copy, modify, and distribute this software
     * for non-commercial purposes and without fee is hereby granted
     * provided that this copyright notice appears in all copies.
     *
     * http://prominence.com/                  merlin@prominence.com
     */
     
    import java.net.*;
    import java.io.*;
    import java.util.*;
    
    public class ChatServer {
      public ChatServer (int port) throws IOException {
        ServerSocket server = new ServerSocket (port);
        while (true) {
          Socket client = server.accept ();
          System.out.println ("Accepted from " + client.getInetAddress ());
          ChatHandler c = new ChatHandler (client);
          c.start ();
        }
      }
    
      public static void main (String args[]) throws IOException {
        if (args.length != 1)
          throw new RuntimeException ("Syntax: ChatServer <port>");
        new ChatServer (Integer.parseInt (args[0]));
      }
    }
    If you got any idea of what's wrong, please tell me.

    Ohh, the port 440 is open too.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,435
    Rep Power
    25

    Default

    when I change the IP from 127.0.0.1 to my ip
    The applet should use get..Base() methods to get the IP to use to talk to its server.
    It can't talk to anyone else without permission, which requires signing.

  5. #5
    henkee is offline Member
    Join Date
    Jun 2010
    Posts
    11
    Rep Power
    0

    Default

    What do you mean with Base functions, I'm not very skilled with Java.

    Do you simply mean that I should test my applet with 127.0.0.1, and sign my applet when it is read for use?

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,435
    Rep Power
    25

    Default

    There are two methods in the Applet class whose names begin with 'get' and end with 'Base'
    Read the API doc to see what they do.

    If the applet tries to communicate with an IP address that is not where the applet was loaded from, then you need to sign the applet's jar. The two methods above will get the URL of the location the applet was loaded from.

  7. #7
    henkee is offline Member
    Join Date
    Jun 2010
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by Norm
    If the applet tries to communicate with an IP address that is not where the applet was loaded from, then you need to sign the applet's jar. The two methods above will get the URL of the location the applet was loaded from.
    I run both the http server(apache) and chat server on 90.227.3.106, so I shouldn't have to sign the applet?
    I get this error:
    Java Code:
    Connecting to 90.227.3.106:440...
    java.net.ConnectException: Connection timed out: connect
    	at java.net.PlainSocketImpl.socketConnect(Native Method)
    	at java.net.PlainSocketImpl.doConnect(Unknown Source)
    	at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
    	at java.net.PlainSocketImpl.connect(Unknown Source)
    	at java.net.SocksSocketImpl.connect(Unknown Source)
    	at java.net.Socket.connect(Unknown Source)
    	at java.net.Socket.connect(Unknown Source)
    	at java.net.Socket.<init>(Unknown Source)
    	at java.net.Socket.<init>(Unknown Source)
    	at ChatApplet.run(ChatApplet.java:59)
    	at java.lang.Thread.run(Unknown Source)
    It is available here:http://90.227.3.106/chat/

    Thanks again for this great help!

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,435
    Rep Power
    25

    Default

    at ChatApplet.run(ChatApplet.java:59)
    What is the code at line 59?
    Add some debugging code there to print out the values used in the constructor/method call at that location.

    <PARAM NAME="host" VALUE="90.227.3.106"><!--90.227.3.106-->
    You need to get the host address using one of the get...Base() methods I mentioned. Your applet can only talk to that address without permission.
    Last edited by Norm; 06-30-2010 at 03:13 PM.

  9. #9
    henkee is offline Member
    Join Date
    Jun 2010
    Posts
    11
    Rep Power
    0

    Default

    Java Code:
    public void run () {
        try {
          String host = getParameter ("host");
          if (host == null)
    	host = getCodeBase ().getHost ();
          String port = getParameter ("port");
          if (port == null)
    	port = "9830";
          output.appendText ("Connecting to " + host + ":" + port + "...");
          output.appendText ("\nHost: '"+host+"' Port: '"+port+"'\n");
    		Socket s = new Socket (host, Integer.parseInt (port));    //<--- This was line 59.
          i = new DataInputStream (new BufferedInputStream (s.getInputStream ()));
          o = new DataOutputStream (new BufferedOutputStream (s.getOutputStream ()));
          output.appendText (" connected.\n");
          input.setEditable (true);
          input.requestFocus ();
          execute ();
        } catch (IOException ex) {
          ByteArrayOutputStream out = new ByteArrayOutputStream ();
          ex.printStackTrace (new PrintStream (out));
          output.appendText ("\n" + out);
        }
      }
    EDIT:Host and port is correct.

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,435
    Rep Power
    25

    Default

    Add some debugging code there to print out the values used in the constructor/method call at that location. Also print out the values of getCodeBase().

  11. #11
    henkee is offline Member
    Join Date
    Jun 2010
    Posts
    11
    Rep Power
    0

    Default

    Do you mean like this?:

    Java Code:
    getCodeBase().getHost()
    That returns 90.227.3.106, which is correct.

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,435
    Rep Power
    25

    Default

    Can I see the full contents of the Java Console?

    Whoops, just noticed that the error message is now: Connection timed out: connect
    vs access denied

    The connect is working, now you need to work on debugging the server.
    Last edited by Norm; 06-30-2010 at 03:37 PM.

  13. #13
    henkee is offline Member
    Join Date
    Jun 2010
    Posts
    11
    Rep Power
    0

    Default

    I am not sure how to check the console when I uploaded the applet. If it is possible, you could try here: http://90.227.3.106/chat/ .

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,435
    Rep Power
    25

    Default

    What's happening on the server?
    When I configured your applet to run on my server, it connects and then nothing happens in the applet.
    This is what I see in the textbox: Connecting to 127.0.0.1:3306... connected.
    There is no output from the applet in the Java Console.

  15. #15
    henkee is offline Member
    Join Date
    Jun 2010
    Posts
    11
    Rep Power
    0

    Default

    Same thing happens for me if I use 127.0.0.1, but when I change the ip to my "real" ip it dosen't work. Is it possible for you to upload and test it too please?

    EDIT:
    It is meant to send the message back to all other clients, which works well. I posted the server source above.
    Last edited by henkee; 06-30-2010 at 03:50 PM.

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,435
    Rep Power
    25

    Default

    I change the ip
    What does that mean?

    My server showed that the applet connected but then didn't do anything that I could tell.
    What does the applet do after it connects? I saw "...connected." in the window.

  17. #17
    henkee is offline Member
    Join Date
    Jun 2010
    Posts
    11
    Rep Power
    0

    Default

    It is meant to be a simple chat client:
    There is a small box under where you can type, and when you press enter the message is sent to the server.

  18. #18
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,435
    Rep Power
    25

    Default

    Just downloaded a version of the ChatApplet.class and tested with my server. Still NO OUTPUT to the java console>>>>> YOU need to add some println() statements to output status to the console. Here's what I get in the window:
    Connecting to 127.0.0.1:3306...
    CB: 127.0.0.1 connected.
    dddddd
    java.io.EOFException
    at java.io.DataInputStream.readUnsignedShort(Unknown Source)
    at java.io.DataInputStream.readUTF(Unknown Source)
    at java.io.DataInputStream.readUTF(Unknown Source)
    at ChatApplet.execute(ChatApplet.java:77)
    at ChatApplet.run(ChatApplet.java:66)
    at java.lang.Thread.run(Unknown Source)
    My server echos back to callers on port 3306. Obviously your applet expects a DataStream.

  19. #19
    henkee is offline Member
    Join Date
    Jun 2010
    Posts
    11
    Rep Power
    0

    Default

    But how can I see the console when I uploaded the applet? When I am using it on localhost(127.0.0.1) it works fine, the problem is when I use an uploaded version and visit it through a domain or ip(NOT 127.0.0.1).

  20. #20
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,435
    Rep Power
    25

    Default

    how can I see the console
    The console is the browser's Java console. There are several ways to show it. Some browsers have menu items. Or click on the java icon in the task bar and select show console. Or go to the control panel and set it on in the Java Control panel item.

Page 1 of 2 12 LastLast

Similar Threads

  1. java out of memory error-heap space error
    By elsanthosh in forum NetBeans
    Replies: 4
    Last Post: 06-15-2010, 09:31 AM
  2. Java ERROR
    By d7om in forum New To Java
    Replies: 5
    Last Post: 04-12-2010, 11:31 PM
  3. Replies: 1
    Last Post: 12-05-2009, 07:28 AM
  4. Replies: 1
    Last Post: 10-26-2009, 04:38 PM
  5. Please Help new to Java and keep getting error
    By CloseQuarters in forum New To Java
    Replies: 1
    Last Post: 12-18-2008, 07:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •