Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (481)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (548)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Simple Server Application and Client Applet  (Read 2663 times)
0 Members and 1 Guest are viewing this topic.
Offline neoskunk

Junior Member





« Posted 2010-07-17 22:00:37 »

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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
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.

Offline neoskunk

Junior Member





« Reply #1 - Posted 2010-07-17 22:04:15 »

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.<init>(Unknown Source)
   at java.net.Socket.<init>(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....
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 781
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #2 - Posted 2010-07-17 23:05:36 »

Do this:
1  
2  
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.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline neoskunk

Junior Member





« Reply #3 - Posted 2010-07-18 03:26:36 »

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?
Offline bobjob

JGO Knight


Medals: 10
Projects: 4


David Aaron Muhar


« Reply #4 - Posted 2010-07-18 04:52:00 »

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.

My Projects
Games, Webcam chat, Video screencast, PDF tools.

Javagaming.org with chat room
Offline neoskunk

Junior Member





« Reply #5 - Posted 2010-07-18 09:45:42 »

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.
Offline bobjob

JGO Knight


Medals: 10
Projects: 4


David Aaron Muhar


« Reply #6 - Posted 2010-07-18 09:53:26 »

And I know that applets are allowed to connect back to the host that served up the applet without being signed
thats interesting I didnt know that.

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

My Projects
Games, Webcam chat, Video screencast, PDF tools.

Javagaming.org with chat room
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 781
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #7 - Posted 2010-07-18 10:57:55 »

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

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline neoskunk

Junior Member





« Reply #8 - Posted 2010-07-18 17:48:50 »

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?
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 781
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #9 - Posted 2010-07-18 19:17:03 »

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

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline neoskunk

Junior Member





« Reply #10 - Posted 2010-07-18 23:23:30 »

Perfect!  Smiley 

Thanks so much thats exactly what I needed.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

atombrot (26 views)
2014-08-19 09:29:53

Tekkerue (24 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (14 views)
2014-08-16 06:20:21

Tekkerue (20 views)
2014-08-16 06:12:11

Rayexar (59 views)
2014-08-11 02:49:23

BurntPizza (38 views)
2014-08-09 21:09:32

BurntPizza (30 views)
2014-08-08 02:01:56

Norakomi (37 views)
2014-08-06 19:49:38

BurntPizza (67 views)
2014-08-03 02:57:17
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!