Simple Server Application and Client Applet

Ok heres my story. A long time ago(some of you might remember me) i made a java game that used sockets and I could test it all on the same machine. For some reason I cannot get it to work now even though I’m 99% sure the code has not changed.

So i made a simple example and I cannot even get this to work.

Here is the server side application:

import java.io.*;
import java.net.*;

class simpleServer
{
	public static void main(String args[])
	{
		System.out.println("Server Running...");
		new server().start();
	}
}

class server extends Thread
{
	ServerSocket ssock;
	Socket csock;

	public server()
	{
		try
		{
			ssock = new ServerSocket(4358);
		}
		catch (IOException e)
		{
			System.out.println("Couldn't access port");
			System.exit(1);
		}
	}
	
	public void run()
	{
		while (true)
		{
			if(ssock == null)
			{
				System.out.println("Port Disappeared");
				System.exit(1);
			}
			try
			{
				csock = ssock.accept();
				System.out.println("someone connected! :)");
			}
			catch (IOException e)
			{
				System.out.println("Couldn't connect player");
				System.exit(1);
			}
		}
	}

}

and the client applet:

import java.net.*;
import java.applet.*;
import java.io.*;

public class test extends Applet
{
    	Socket socket = null;

    	public void init()
    	{
       		try
		{
                 	socket = new Socket("localhost",4358);
              	}
                catch (UnknownHostException ex)
                {
                	System.out.println("Unknown host");
                  	System.exit(1);
             	}
   	        catch (IOException exx)
            	{
                 	System.out.println("Read Failed");
                  	System.exit(1);
            	}        
     			
    	}
}

All i am trying to do is create the connection but I contnue to throw the socket permission exception or something like that.

Shouldn’t this work? Again this is all on the same machine. I start the server application and then launch the applet from an html file.
Any help would be greatly appreciated.

Here is the exact exception:

java.security.AccessControlException: access denied (java.net.SocketPermission 127.0.0.1:4358 connect,resolve)
at java.security.AccessControlContext.checkPermission(Unknown Source)
at java.security.AccessController.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkConnect(Unknown Source)
at sun.plugin2.applet.Applet2SecurityManager.checkConnect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.(Unknown Source)
at java.net.Socket.(Unknown Source)
at test.init(test.java:15)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception: java.security.AccessControlException: access denied (java.net.SocketPermission 127.0.0.1:4358 connect,resolve)

it doesn’t matter if the server application is running or not. it throws the same exception.

i would think its an error in my applet code…

Do this:


ssock = new ServerSocket(4358);
System.out.println(ssock);

It’s pretty likely the server isn’t running on 127.0.0.1 but on something like 192.168..

Read that IP address, and connect to it in your applet.

this is what is printed when i do that:

ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=4358]

I tried IP 0.0.0.0 and it throws the exact same exception, even with reference to 127.0.0.1.

Any other ideas?

there isn’t any certain way I’m supposed to open the applet is there?

try testing it with your current network IP instead of 127.0.0.1 I have been having trouble with the loop back address on Macosx.

also maybe put a println call just before the server socket accept method, something like, “serversoket waiting”

as its a access control exception, you may need to run it as an application not an applet.

or else sign the applet.

I tried everything you said and still the same exception. Its running on windows not mac. I guess I could try it on mac but this is really just for testing purposes without setting up an actual server.

And I know that applets are allowed to connect back to the host that served up the applet without being signed but yes if i run this as an application it works perfectly but thats not what I am after.

thats interesting I didnt know that.

I assumed to prevent signing of an applet with networking you needed to use URLConnectionStream.

How are you serving the webpage the applet is on? A HTTP server?

You browser addressbar should look like:
http://127.0.0.1:yourport/applet.html

I’m not serving the webpage at all. I simply have an html file in a file with the .class file. I then open the html in my browser and the applet comes up.

Maybe I haven’t been clear. This is just for testing. I am trying to imitate a server and client all on one computer. In the past I could start the server application from the command line and then open two different instances of the applet and they would successfully connect to the server program using sockets.

my address bar looks like this when I open the html file in my browser:
C:\Users\Garrett\Desktop\New folder\javaTest.html

is what i am trying to do not possible? do i need to set up a server in order to test this?

Spend 2 minutes to install a http server:
http://www.analogx.com/contents/download/Network/sswww/Freeware.htm

Perfect! :slight_smile:

Thanks so much thats exactly what I needed.