Results 1 to 20 of 20

Thread: tcp connection

  1. #1
    tsunamy_boy is offline Member
    Join Date
    Aug 2010
    Posts
    10
    Rep Power
    0

    Default tcp connection

    Hi there, i'm noob on java applets so...
    I'm trying to make a TCP connection with a remote server and send messages with it.

    I have this code but only works if i use apache and if i have my server working localy.

    Java Code:
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.net.*;
    
    public class ClientExample extends Applet {
       TextField nameTF, passTF, domainTF;
       Image im;
       Button sendButton;
       /** Where the Applet came from */
       URL whence;
    
       //+
       /** Initialize the GUI nicely. */
       public void init() {
          Label aLabel;
    
          setLayout(new GridBagLayout());
          int LOGO_COL = 1;
          int LABEL_COL = 2;
          int TEXT_COL = 3;
          int BUTTON_COL = 1;
          GridBagConstraints gbc = new GridBagConstraints();
          gbc.weightx = 100.0; gbc.weighty = 100.0;
    
          gbc.gridx = LABEL_COL; gbc.gridy = 0;
          gbc.anchor = GridBagConstraints.EAST;
          add(aLabel = new Label("Name:", Label.CENTER), gbc);
          gbc.anchor = GridBagConstraints.CENTER;
          gbc.gridx = TEXT_COL; gbc.gridy = 0;
          add(nameTF=new TextField(10), gbc);
    
          gbc.gridx = LABEL_COL; gbc.gridy = 1;
          gbc.anchor = GridBagConstraints.EAST;
          add(aLabel = new Label("Password:", Label.CENTER), gbc);
          gbc.anchor = GridBagConstraints.CENTER;
          gbc.gridx = TEXT_COL; gbc.gridy = 1;
          add(passTF=new TextField(10), gbc);
          passTF.setEchoChar('*');
    
          gbc.gridx = LABEL_COL; gbc.gridy = 2;
          gbc.anchor = GridBagConstraints.EAST;
          add(aLabel = new Label("Domain:", Label.CENTER), gbc);
          gbc.anchor = GridBagConstraints.CENTER;
          gbc.gridx = TEXT_COL; gbc.gridy = 2;
          add(domainTF=new TextField(10), gbc);
          sendButton = new Button("Send data");
          gbc.gridx = BUTTON_COL; gbc.gridy = 3;
          gbc.gridwidth = 3;
          add(sendButton, gbc);
    
          whence = getCodeBase();
    
          // Now the action begins...
          sendButton.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent evt) {
                String name = nameTF.getText();
                if (name.length() == 0) {
                   showStatus("Name required");
                   return;
                }
                String domain = domainTF.getText();
                if (domain.length() == 0) {
                   showStatus("Domain required");
                   return;
                }
                showStatus("Connecting to host " + whence.getHost() +
                   " as " + nameTF.getText());
    
                try {
    		Socket s = new Socket("2.81.8.38", 1332);
                   PrintWriter pf = new PrintWriter(s.getOutputStream(), true);
                   // send login name
                   pf.println(nameTF.getText());
                   // passwd
                   pf.println(passTF.getText());
                   // and domain
                   pf.println(domainTF.getText());
    
                   BufferedReader is = new BufferedReader(
                      new InputStreamReader(s.getInputStream()));
                   String response = is.readLine();
                   showStatus(response);
                } catch (IOException e) {
                   showStatus("ERROR: " + e.getMessage());
                }
             }
          });
       }
       //-
    }
    Last edited by tsunamy_boy; 08-05-2010 at 07:12 PM.

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

    Default

    Do you get any errors in the browser's Java console?
    Copy and paste them here

    Applets are not allowed to connect to any server but the one they were loaded from.

  3. #3
    tsunamy_boy is offline Member
    Join Date
    Aug 2010
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Do you get any errors in the browser's Java console?
    Copy and paste them here

    Applets are not allowed to connect to any server but the one they were loaded from.
    I get no erros.
    There are any other solution for my problem?
    I don't really want to use php : (
    Last edited by tsunamy_boy; 08-05-2010 at 07:11 PM.

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

    Default

    are any other solution for my problem?
    Until we know what is causing your problem, we can't help you solve it.

    How are you executing your code? Where are the files for it?
    Can you explain the steps you are taking when you try to execute the program?
    What browser, what server, what url in the browser?

    Can you describe all the above for when your program works?

    And then describe all of the above for when your program does not work.

  5. #5
    tsunamy_boy is offline Member
    Join Date
    Aug 2010
    Posts
    10
    Rep Power
    0

    Default

    ok, so...
    First of all, i don't know if i compiled and sign the files well.
    I'm using windows, can you provide me the lines to compile .class and .jar files and then sign them?

    I used this:
    create a .jar file
    @jar cvf JarFile.jar <my java file name>

    create a .class file
    @javac <my java file name>

    create a certificate
    @keytool -genkey -keystore %cd%\bob2.cert -alias bob

    apply the certificate:
    @jarsigner -keystore %cd%\bob2.cert -signedjar JarFile_signed.jar JarFile.jar bob
    --
    I'm using windows with firefox 3.6.8 (with the last java update)
    I'm using apache (locally) to call the file.

    The steps that i take are:
    I open the page, then i click "Run" and next i Click "NO".

    This applet only works if i have the server working localy, otherwise it does nothing don't even alert errors.

    Thanks for the help :)
    If you need more informations, let me know : )
    Last edited by tsunamy_boy; 08-05-2010 at 07:44 PM.

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

    Default

    Where are the files you load into the browser? URLs - local disk(file://) or on a server (http://)?

    I open the page, then i click "Run" and next i Click "NO".
    What does this mean? where is the page with the buttons to click???


    works if i have the server working locally
    And no messages at all in the browser's java console.
    Add some println()s to the applet code to print something in the java console to see if the code is working.

  7. #7
    tsunamy_boy is offline Member
    Join Date
    Aug 2010
    Posts
    10
    Rep Power
    0

    Default

    Where are the files you load into the browser? URLs - local disk(file://) or on a server (http://)?
    http://localhost/paginas/TCP/HTML.html


    What does this mean? where is the page with the buttons to click???
    On the java warning messages.

    "The application's digital signature can not be verified. Do you want to run the application?"
    I chose "Run"
    "Java has discovered application components that could indicate a security concern. Block potentially unsafe components from being run?"
    I chose "no"


    And no messages at all in the browser's java console.
    Add some println()s to the applet code to print something in the java console to see if the code is working.
    Where is java console? are you talking about.
    Tools - java console?
    how can i make this option work? oO

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

    Default

    Finding the Java console depends on the browser.
    Also in the Control panel, the java icon has settings you can turn on to display the Java console.

  9. #9
    tsunamy_boy is offline Member
    Join Date
    Aug 2010
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Finding the Java console depends on the browser.
    Also in the Control panel, the java icon has settings you can turn on to display the Java console.
    On:
    "advanced" - "Java console" - "show console"
    Still don't happen.
    Is this really needed? :confused:
    --

    Tell me, is that possible to create an applet that allows the users to establish connections to other IP's as a client and send data to them?

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

    Default

    Is this really needed?
    If there are errors, they are shown in the java console.
    Without that error message, we have to guess what the error might be.

    create an applet that allows the users to establish connections to other IP's as a client and send data to them?
    Yes, if the applet has "permission".

  11. #11
    tsunamy_boy is offline Member
    Join Date
    Aug 2010
    Posts
    10
    Rep Power
    0

    Default

    But this permission has to be configured computer by computer or i can do it with code?

    I spent a week searching for a solution and i just get a few lines that doesn't work : (

    I guess I found something:

    access denied.
    What i do now : (

    Java Plug-in 1.6.0_21
    Using JRE version 1.6.0_21-b07 Java HotSpot(TM) Client VM
    User home directory = C:\Documents and Settings\(.....)
    ----------------------------------------------------
    (.......)
    ----------------------------------------------------
    Exception in thread "AWT-EventQueue-2" java.security.AccessControlException: access denied (java.net.SocketPermission 2.81.8.38:1332 connect,resolve)
    at java.security.AccessControlContext.checkPermission (Unknown Source)
    at java.security.AccessController.checkPermission(Unk nown Source)
    at java.lang.SecurityManager.checkPermission(Unknown Source)
    at java.lang.SecurityManager.checkConnect(Unknown Source)
    at sun.plugin2.applet.Applet2SecurityManager.checkCon nect(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 TextRenderingExample$1.actionPerformed(TextRenderi ngExample.java:75)
    at java.awt.Button.processActionEvent(Unknown Source)
    at java.awt.Button.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

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

    Default

    access denied (java.net.SocketPermission 2.81.8.38:1332 connect,resolve)
    I'm not sure why your signing the jar didn't give your code permission to connect to an IP address.
    I believe it is possible to give the applet permission (in some browsers) by adding an entry to the .java.policy file on the client's computer. You might try this to see if you are able to give the applet permission.
    Use the policytool program to give your code permission to connect.

  13. #13
    tsunamy_boy is offline Member
    Join Date
    Aug 2010
    Posts
    10
    Rep Power
    0

    Default

    by adding an entry to the .java.policy file on the client's computer.
    Use the policytool program to give your code permission to connect.
    how do i do that?

    Can you sign this code for me and see if works? If yes, probably I'm doing anything wrong :D

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

    Default

    The policytool program is part of the JDK. The path on my system to an older JDK is:
    C:\Program Files\Java\jdk1.5.0_04\bin\policytool.exe

    The .java.policy file on my system is at:
    C:\Documents and Settings\Owner\.java.policy

    An entry from my .java.policy file:
    grant codeBase "http://somewhere.com" {
    permission java.net.SocketPermission "8013", "accept, connect, listen, resolve";
    };

  15. #15
    tsunamy_boy is offline Member
    Join Date
    Aug 2010
    Posts
    10
    Rep Power
    0

    Default

    But doing that, all of my users have to change them java permissions :\
    Is it possible to make the same thing with code?

    can you compile my source, sign it and see what happen?
    Maybe I'm doing wrong the compilation

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

    Default

    My suggestion on changing the .java.policy file was to see if it is possible for you to give a brower permission to do what you want to do. It is NOT a way to allow the general public to use your program.
    If you could code around the security of the policy file then there would be no security.

    Did you try changing your policy file and did the applet work?

  17. #17
    tsunamy_boy is offline Member
    Join Date
    Aug 2010
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    My suggestion on changing the .java.policy file was to see if it is possible for you to give a brower permission to do what you want to do. It is NOT a way to allow the general public to use your program.
    If you could code around the security of the policy file then there would be no security.

    Did you try changing your policy file and did the applet work?
    I tried different ways but i don't know if i'm doing this right :D

    Is this right?

    grant codeBase "2.81.8.38" {
    permission java.net.SocketPermission "1332", "accept, connect, listen, resolve";
    };
    I search and i found that the politicy file it's on this directory:
    C:\Programs\Java\jre6\lib\security
    I used this file, i didn't find the location that you provides me.

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

    Default

    That is NOT the correct location for the .java.policy file. Did the file you found have a leading . in its name?
    Don't change any files in the JDK/JRE folders.

    Did you try using the policytool program? It knows where the .java.policy file is.

  19. #19
    tsunamy_boy is offline Member
    Join Date
    Aug 2010
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    That is NOT the correct location for the .java.policy file. Did the file you found have a leading . in its name?
    Not really :D
    But the location that you provide me don't have that file (even with the hidden files shown).

    Did you try using the policytool program? It knows where the .java.policy file is.
    Theres a warning log on that file that says: (i'm going to translate)
    java.io.FileNotFoundException: C:\Documents and Settings\MyUser\.java.policy (The system can not find the specified file)

    --
    I guess my problem is because didn't made a correct sign.
    Can you teach me how to sign a java file?

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

    Default

    Sorry, I have only signed a couple of times following the instructions in the java tutorial.

    The policytool was telling you that there was not a .java.policy file at the location where it expected one to be. Write the path down (you'll use it when done to save the .java.policy file) and continue with the policytool to create the permission. When done, save it to the path shown in the beginning error message.

Similar Threads

  1. Java.net.socket connection :connection closed
    By veeru541 in forum Advanced Java
    Replies: 2
    Last Post: 06-27-2010, 02:14 AM
  2. FTP connection
    By java2010 in forum New To Java
    Replies: 3
    Last Post: 04-19-2010, 09:46 AM
  3. Connection
    By kotkot310 in forum JDBC
    Replies: 4
    Last Post: 03-15-2010, 12:04 PM
  4. Replies: 7
    Last Post: 10-20-2009, 10:34 AM
  5. no connection
    By even in forum JDBC
    Replies: 15
    Last Post: 01-02-2008, 01:50 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
  •