Java UDP communication applet for UDP server

I worked on different ways to do this in 2 full days of coding, I need help:

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

I got the impression that as long as the UDP server runs on the same computer on which the applet is running, it will work. (maybe I need to fix this)

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

When I try to receive messages on the applet, nothing happens, nothing is sent and nothing is received (the udp server sends a message, the applet does not receive, I know that udp sends correctly, since I tested it separately by the 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 in which I run the applet:

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

And here is the server that I use

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 am trying to follow the tutorial here: http://corvstudios.com/tutorials/udpMultiplayer.php to create this code.

I really didn’t have to end up using MINA, Tomcat or any kind of network infrastructure, but if I have to tell me it will save me a lot of time

Any help is truly appreciated in an advanced environment!

+5
1

JRE, , . .

+1

All Articles