5

I've been working on different ways to do this for 2 full coding days, i need some help:

I want to create a multiplayer game in java online. To do this i need communication between the server and the applet

I was under the impression that as long as the UDP server is being ran on the same machine the applet is being hosted on, it would work. (perhaps i need to be corrected on that)

I continually get this error on the error console (from applet) java.security.AccessControlException: access denied (java.net.SocketPermission 127.0.0.1:5556 connect,resolve)

When trying to receive messages on the applet, nothing happens, nothing is sent and nothing is received (the udp server is sending a message,applet is not receiving, i know the udp is sending correctly as i tested separately it with a client)

Here is the UDPclient.java applet:

``

import java.io.*;
import java.net.*;
import java.applet.*;
public class UDPClient extends Applet
{
    protected DatagramSocket socket = null;
    protected DatagramPacket packet = null; 
    String ipAddress;
    public void init()
    {
        try{
        System.out.println("got username");
        String username = getParameter("username");
        System.out.println("got ip");
        ipAddress = getParameter("ip"); 
        System.out.println("makingsocket");
        socket = new DatagramSocket();
        System.out.println("sending packet");
        sendPacket();
        System.out.println("receiving packet");
        receivePacket();
        System.out.println("closing socket");
            socket.close();
        }catch(Exception e){e.printStackTrace();}
    }
    public void sendPacket() throws IOException
    {
         byte[] buf ="hihihi".getBytes(); // send hihihi
        InetAddress address = InetAddress.getByName(ipAddress);
        packet = new DatagramPacket(buf, buf.length, address, 5556);
        System.out.println("sending packet");   
     socket.send(packet);
         int port = packet.getPort();

    } 
    public void receivePacket() throws IOException
    {
        byte[] buf = new byte[256];
            packet = new DatagramPacket(buf, buf.length);
        System.out.println("getting packet--- calling socket.receive");
            socket.receive(packet);
        System.out.println("got here, receiving packet");
            String modifiedSentence =new String(packet.getData());
            System.out.println("FROM SERVER:" + modifiedSentence);
    }
}

Here is the HTML file i run the applet with:

<applet code="UDPClient.class" width="640" height="480">
<param name="username" value="Guest">
<param name="ip" value="localhost">
</applet> 

And here is the server i'm using

import java.io.*;
import java.net.*;
public class multiTest
{
    static protected DatagramSocket socket = null;
    static protected DatagramPacket packet = null; 
    public static void main(String [] args) throws IOException
    {
        socket = new DatagramSocket(5556); 
        while(true)
        {
            receivePacket();            
            sendPacket();           


        }
    }
    public static void receivePacket() throws IOException
    {
         //receive message from client
         byte[] buf = new byte[256];
         packet = new DatagramPacket(buf, buf.length);
         socket.receive(packet);

         //translate message in a thread
         String message = new String(packet.getData(), 0, packet.getLength());
         System.out.println("received" + message);
    // should really make thread;
    } 
    public static void sendPacket() throws IOException
    {

        byte[] buf = "ack".getBytes();
         //send the message to the client to the given address and port
          InetAddress address = packet.getAddress();
         int port = packet.getPort();
         packet = new DatagramPacket(buf, buf.length, address, port);
         socket.send(packet);
    } 
}

I have been trying to follow the tutorial here :http://corvstudios.com/tutorials/udpMultiplayer.php to create this code.

i really didnt wanna have to end up using MINA, Tomcat or install any network framework - but if i have to let me know, it'll save me a lot of time

Any help is sincerely appreciated in advanced!

user761996
  • 83
  • 1
  • 8
  • is the server address bound to 127.0.0.0? Remember the applet restrictions that unsigned applets may only access the host they were loaded from. – Jochen Bedersdorfer May 20 '11 at 02:01
  • @Jochen: I suspect that while the applet and target are on the same *machine,* the JRE does not consider them to be coming from the same *server.* (Which I'm guessing is where you were going with that.) @OP What is the address in the browser address bar when visiting the applet? – Andrew Thompson May 20 '11 at 02:11
  • server is using localhost. when visting the applet i am using the file system, so for me it looks like this: file:///home/freelan/asdf16ino/explorer/index3.html (i am using ubuntu) – user761996 May 20 '11 at 03:20
  • so if thats true and it doesnt consider it on the same server even though they are on the same machine-- is there any way i can test my program without hosting a website? (thank you very much for the help!!) – user761996 May 20 '11 at 03:22
  • 2
    I wouldn't recommend using the file protocol in this case. Throw in Jetty in the mix to download the applet via http. Also, sign your applet (with a self-signed certificate) – Jochen Bedersdorfer May 20 '11 at 03:24
  • HAAHHAHA!!!! it works! i'm toooooo happy thank you so much!! if you post what you said previously as the answer i can give u the credit for it and mark as answered :) – user761996 May 20 '11 at 15:29

1 Answers1

1

The JRE used by your applet need to be configured to authorize access thé file system to your applet. See policy or security for applet

EricParis16
  • 809
  • 5
  • 9