我一直工作在不同的方式来满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文件我运行小程序:
,这里是服务器我使用
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的) –
2011-05-20 03:20:25