Java UDP通信小程序到UDP服务器

问题描述 投票:5回答:1

我已经在不同的方法上工作了2个完整的编码日,我需要一些帮助:

我想用java在线创建一个多人游戏。 为此,我需要服务器和applet之间的通信

我的印象是,只要UDP服务器在托管applet的同一台机器上运行,它就可以工作。 (也许我需要纠正)

我在错误控制台(来自applet)上不断收到此错误java.security.AccessControlException:访问被拒绝(java.net.SocketPermission 127.0.0.1:5556 connect,resolve)

当尝试在applet上接收消息时,没有任何反应,没有任何内容被发送,也没有收到任何内容(udp服务器正在发送消息,applet没有收到,我知道udp发送正确,因为我单独测试它与客户端)

这是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);
    }
}

这是运行applet的HTML文件:

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

这是我正在使用的服务器

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);
    } 
}

我一直在尝试按照这里的教程:http://corvstudios.com/tutorials/udpMultiplayer.php来创建此代码。

我真的不想最终使用MINA,Tomcat或安装任何网络框架 - 但如果我必须告诉我,它会节省我很多时间

任何帮助都是先进的!

java applet udp client-server communication
1个回答
1
投票

您的applet使用的JRE需要配置为授权您的applet访问文件系统。 请参阅applet的策略或安全性

© www.soinside.com 2019 - 2024. All rights reserved.