Results 1 to 2 of 2
  1. #1
    mjgood91 is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default Applet doesn't connect to server over internet

    So I'm trying to build a basic chat program where people can log in and talk with each other. I've got my code written, my applet signed, and my ports (port 7005) forwarded, and a DNS entry set up for my non-static IP address.

    What works:
    - When I run the client from my compiler over the local network, everything works fine.
    - When I run the client from an applet in a browser from the local network, everything works fine.
    - When I run the client from my compiler on a computer at a remote location, everything works fine.

    The problem is when I run the client from an applet in a browser from a remote location, I get an IOException error in the Connection() method.

    I already tried adding an exception to the java.policy file in the security folder; it still does not connect to the internet from a remote location in an applet, even though it works fine from the compiler.

    I get no runtime errors running this program, just the exceptions that you'll see if you go to my website I listed below.

    Java Code:
    //Chat Program - Sends and recieves messages from a server over the network/internet
    
    import java.awt.*;
    import java.applet.*;
    import java.net.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.io.*;
    
    public class ChatClient extends JApplet
    {
    	protected TextArea output;
    	protected TextField username;
    	protected TextField input;
    	protected final String newLine = System.getProperty("line.separator");
    	
    	protected PrintWriter out;
    	protected Socket host;
    
    	//Start program
    	public void init()
    	{
    		setSize(300, 600);
    		
    		setLayout (new BorderLayout());
    		
    		output = new TextArea();
    		output.setEditable(false);
    		
    		add(output, BorderLayout.CENTER);
    		add(input(), BorderLayout.SOUTH);
    		
    		Connection chat = new Connection();
    		Thread chatProcess = new Thread(chat);
    		chatProcess.start();
    	}
    	
    	//Setup input panel
    	private Panel input()
    	{
    		Panel me = new Panel();
    		
    		input = new TextField();
    		username = new TextField();
    		
    		username.setColumns(10);
    		input.addActionListener(new textMessage());
    		
    		me.setLayout (new BorderLayout());
    		me.add(input, BorderLayout.CENTER);
    		me.add(username, BorderLayout.WEST);
    		
    		return me;
    	}
    	
    	//Handles the 'enter' event from the textbox; send texts to server
    	private class textMessage implements ActionListener
    	{
    		public void actionPerformed (ActionEvent e)
    		{
    			out.println(username.getText() + ": " + input.getText());
    			input.setText("");
    		}
    	}
    	
    	//Handles connection with server
    	private class Connection implements Runnable
    	{
    		protected BufferedReader in;
    		protected Socket host;
    		protected String fromHost;
    		
    		//Setup connection
    		public Connection()
    		{
    			try
    			{
    				InetAddress hostPlace = InetAddress.getByName("mjgood91-chat.servehttp.com");
    				host = new Socket(hostPlace, 7005);
    				out = new PrintWriter(host.getOutputStream(), true);
    				in = new BufferedReader(new InputStreamReader(host.getInputStream()));
    			}
    			catch (IOException e)
    			{
    				output.setText("ERROR: initialization I/O error!" + newLine);
    			}
    		}
    		
    		//Get texts from server
    		public void run()
    		{
    			boolean notified = false;
    		
    			while (true)
    			{
    				try
    				{
    					fromHost = in.readLine();
    					if (fromHost != "")
    					{
    						output.append(fromHost + newLine);
    					}
    				}
    				catch (IOException e)
    				{
    					output.append("ERROR: get data I/O error!" + newLine);
    				}
    				catch (NullPointerException e)
    				{
    					if (!notified)
    					{
    						output.append("ERROR: bad connection!" + newLine);
    						notified = true;
    					}
    				}
    			}
    		}
    	}
    }
    At the moment I have the applet up at mjgood91-chat.servehttp.com:7004/CodeSamples/ChatProgram.htm, where you can see the glorious initialization I/O error message display in my applet window. Additionally, you should be able to compile the code yourself and connect to my server.

    If you think it'll help I can post the host's code as well.

    Thanks in advance.

  2. #2
    mjgood91 is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default Re: Applet doesn't connect to server over internet

    Nevermind, I found out what was wrong... turns out I accidentally uploaded an older version of code to the site. It works just fine now. :P

Similar Threads

  1. Accessing a Java server from the Internet
    By mozly in forum Networking
    Replies: 2
    Last Post: 01-06-2012, 08:31 AM
  2. Application does not connect over internet.
    By eLancaster in forum Networking
    Replies: 6
    Last Post: 06-28-2011, 08:16 PM
  3. Internet Explorer not update applet
    By billlin in forum New To Java
    Replies: 24
    Last Post: 07-18-2010, 01:45 AM
  4. How to authenticate code with no internet connect.
    By beardawg in forum Java Applets
    Replies: 1
    Last Post: 04-14-2010, 08:31 PM
  5. Replies: 0
    Last Post: 04-13-2010, 09:16 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
  •