我创建了一个通过RMI进行通信的Java Server和Client应用程序。为了让客户满足"服务器,我发送组播DatagramPacket到客户端。发生的事情是我使用属性localDirectory实例化类,当我收到它时,它来自null。重要的是,我尝试发送的对象实现了Seriaizable。
你觉得我做错了什么?
(如果您需要aditional代码,请告诉我)
public static void main(String[] args) throws RemoteException {
localDirectory = new File("c:\\tempfiles".trim());
serverToTransmit = (ServerInterface) new Server(localDirectory);
if (!localDirectory.exists()) {
System.out.println("Directory " + localDirectory + " does NOT exist!");
return;
}
if (!localDirectory.isDirectory()) {
System.out.println("The path " + localDirectory + " is NOT a directory!");
return;
}
if (!localDirectory.canRead()) {
System.out.println("WITHOUT reading permissions on " + localDirectory + "!");
return;
}
try {
System.out.println("Vou criar o registry");
Registry registry;
try {
System.out.println("Registry lauching try in port " + 7609 + "...");
registry = LocateRegistry.createRegistry(7609);
System.out.println("Registry launched!");
} catch (RemoteException e) {
System.out.println("Registry probably in execution already!");
registry = LocateRegistry.getRegistry();
}
System.out.println("Será que criei?");
//Create Service
//ServerInterface service = new Server(localDirectory);
Server server = new Server(localDirectory);
registry.bind(SERVICE_NAME, server);
System.out.println("Service " + SERVICE_NAME + " registered on registry.");
MulticastSocket sock = new MulticastSocket(7000); //225.15.15.15 e o porto 7000.
InetAddress addr = InetAddress.getByName("225.15.15.15");
sock.joinGroup(addr);
//sock.setTimeToLive(1);
DatagramSocket psock;
while (true) {
try {
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bOut);
out.writeObject(serverToTransmit);
//out.writeUnshared(objectToTransmit) in order to avoid caching issues
out.flush();
byte[] data = bOut.toByteArray();
DatagramPacket packet = new DatagramPacket(data, data.length, addr, 7000);
System.out.println("sizeeee: " + bOut.size());
sleep(3000);
sock.send(packet);
} catch (SocketException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
}
/* if (args.length > 0 && args[0].equals("leader")) {
playLeader(sock, psock, addr);
} else {
playFollower(sock, psock, addr);
}
sock.leaveGroup(addr);*/
} catch (RemoteException e) {
System.out.println("Remote error - " + e);
System.exit(1);
} catch (Exception e) {
e.printStackTrace();
}
并且我在客户端上收到这样的信息(只提供重要的代码):
MulticastSocket sock = new MulticastSocket(7000); //225.15.15.15 e o porto 7000.
InetAddress addr = InetAddress.getByName("225.15.15.15");
sock.joinGroup(addr);
sock.setTimeToLive(1);
System.out.println("PASSOU AQUI_!");
DatagramPacket packet = new DatagramPacket(new byte[1024], 1024, addr, 7000);
sock.receive(packet);
System.out.println("PASSOU AQUI_1!");
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(packet.getData(), 0,
packet.getLength()));
System.out.println("PASSOU AQUI_2!");
ServerInterface returnedObject = (ServerInterface) in.readObject();
System.out.println("Returned Object LOCAL DIR -> " + returnedObject.getLocalDirectory());
谢谢!
答案 0 :(得分:3)
因此,为了涵盖所有可能性,包括一些被您的问题排除的可能性,属性localDirectory
:
static
transient
writeObject()
方法readObject()
方法分配writeReplace()/readResolve()
megillah。