Java UDP 与 UDP 服务器的通信小程序

发布于 2024-11-08 17:55:57 字数 3707 浏览 5 评论 0原文

我一直在研究不同的方法来做到这一点,花了 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 或安装任何网络框架 - 但如果我必须让我知道,这会节省我很多时间

真诚感谢任何帮助!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城泪 2024-11-15 17:55:57

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文