2011-05-20 62 views
5

我一直工作在不同的方式來滿2編碼天做到這一點,我需要一些幫助:Java的UDP通信的小程序,UDP服務器

我想在網上創建Java中的多人遊戲。要做到這一點,我需要服務器和小應用程序之間的通信

我的印象是,只要UDP服務器運行在同一臺機器上的小程序託管,它會工作。 (也許我需要糾正)

我一直在錯誤控制檯(從小程序)得到這個錯誤 java.security.AccessControlException:訪問被拒絕(java.net.SocketPermission 127.0.0.1:5556連接,解決)

當試圖在小程序上接收消息時,什麼也沒有發生,什麼都沒有發送,也沒有收到任何消息(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創建此代碼。

我真的didnt想得最終使用MINA,Tomcat或安裝任何網絡架構 - 但如果我必須讓我知道,這將節省我很多時間

任何幫助表示衷心感謝中高級!

+0

是否將服務器地址綁定到127.0.0.0?請記住未簽名小程序只能訪問它們從中加載的主機的小程序限制。 – 2011-05-20 02:01:47

+0

@Jochen:我懷疑當applet和目標在同一臺機器上時,JRE並不認爲它們來自同一個服務器*(我猜這就是你要去的地方。 )@OP訪問小程序時瀏覽器地址欄中的地址是什麼? – 2011-05-20 02:11:08

+0

服務器正在使用localhost。當我使用的文件系統,如此對我來說,它看起來像這樣:file:///home/freelan/asdf16ino/explorer/index3.html(我使用Ubuntu的) – user761996 2011-05-20 03:20:25

回答

1

您的小程序使用的JRE需要配置爲授權將文件系統訪問到您的小程序。請參閱策略或安全小程序