【问题标题】:Java UDP communication applet to UDP ServerJava UDP 通信小程序到 UDP 服务器
【发布时间】:2011-05-20 01:44:49
【问题描述】:

在 2 个完整的编码日中,我一直在研究不同的方法来做到这一点,我需要一些帮助:

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

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

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

当试图在小程序上接收消息时,什么也没有发生,什么也没有发送,也没有收到(udp 服务器正在发送消息,小程序没有接收,我知道 udp 发送正确,因为我用客户)

这里是 UDPclient.java 小程序:

``

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

这是我运行小程序的 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 或安装任何网络框架 - 但如果我必须让我知道,它会为我节省很多时间

真诚感谢您的任何帮助!

【问题讨论】:

  • 服务器地址是否绑定到127.0.0.0?请记住,未签名的小程序只能访问加载它们的主机。
  • @Jochen:我怀疑虽然小程序和目标在同一台机器上,但 JRE 并不认为它们来自同一台服务器。 (我猜这是你要去的地方。)@OP 访问小程序时浏览器地址栏中的地址是什么?
  • 服务器正在使用本地主机。访问小程序时,我使用的是文件系统,所以对我来说它看起来像这样:file:///home/freelan/asdf16ino/explorer/index3.html(我正在使用 ubuntu)
  • 所以如果这是真的并且即使它们在同一台机器上,它也不认为它在同一台服务器上——有什么方法可以在不托管网站的情况下测试我的程序? (非常感谢您的帮助!!)
  • 我不建议在这种情况下使用文件协议。在混合中加入 Jetty 以通过 http 下载小程序。此外,签署您的小程序(使用自签名证书)

标签: java applet udp client-server communication


【解决方案1】:

您的小程序使用的 JRE 需要配置为授权访问您的小程序的文件系统。查看小程序的策略或安全性

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-28
    • 1970-01-01
    • 1970-01-01
    • 2014-07-28
    • 1970-01-01
    • 2021-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多