英文:
Transfer Exact Same Object Sockets
问题
我有另一个问题
我有一个名为Server的类:
package server;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;
import javax.swing.DefaultListModel;
import objects.Player;
public class Servidor implements Runnable, Observer {
int puerto;
public DefaultListModel<Player> players = new DefaultListModel<>();
public ArrayList<Socket> clients = new ArrayList<>();
public static Send m;
public Servidor(int puerto, Send m) {
this.m = m;
this.m.addObserver(this);
this.puerto = puerto;
System.out.println(this.m + " from the server");
}
@Override
public void run() {
ServerSocket servidor = null;
Socket sc = null;
ObjectOutputStream output;
try {
servidor = new ServerSocket(puerto);
System.out.println("Server Started");
while (true) {
sc = servidor.accept();
System.out.println("New Cliente");
output = new ObjectOutputStream(sc.getOutputStream());
output.writeObject(m);
clientes.add(sc);
}
} catch (IOException ex) {
}
}
public void Send_Players() throws IOException {
for (Socket sc : clients) {
ObjectOutputStream output = new ObjectOutputStream(sc.getOutputStream());
output.writeObject(players);
}
}
@Override
public void update(Observable o, Object arg) {
//Stuff
}
}
然后我有我的客户端类:
package server;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;
import java.util.Observable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import objects.Player;
public class Client extends Observable implements Runnable {
String host;
int puerto;
Socket sc;
public Client(int purto, String host) {
this.puerto = purto;
this.host = host;
try {
sc = new Socket(host, puerto);
} catch (IOException ex) {
}
}
@Override
public void run() {
DataInputStream in;
ObjectInputStream input = null;
DefaultListModel<Player> players = new DefaultListModel<>();
try {
in = new DataInputStream(sc.getInputStream());
input = new ObjectInputStream(in);
} catch (IOException ex) {
}
while (true) {
Object objeto = null;
try {
objeto = input.readObject();
} catch (ClassNotFoundException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
if (objeto != null) {
System.out.println("Is: " + objeto.getClass().getName());
if (objeto instanceof DefaultListModel) {
players = (DefaultListModel<Player>) objeto;
this.setChanged();
this.notifyObservers(players);
this.clearChanged();
} else {
Send m = (Send) objeto;
this.setChanged();
this.notifyObservers(m);
this.clearChanged();
}
}
}
}
}
还有Send类:
package server;
import java.io.Serializable;
import java.util.Observable;
public class Send extends Observable implements Serializable {
private static final long serialVersionUID = 1L;
public void Send_(Object arg) {
this.setChanged();
this.notifyObservers(arg);
this.clearChanged();
}
}
现在我将解释它的工作原理:
1. 当我创建一个服务器时,我将其添加为类Send的观察者。
2. 当我创建一个新的客户端时,服务器会检测到它。
3. 当一个新的客户端连接时,它会发送一个类型为Send的对象。
4. 在客户端类中,使用可观察对象检测到有东西到达,并通知名为Main的类。
5. 当Main类的观察者收到通知时,它会执行以下操作:
this.m = (Send) arg;
System.out.println(this.m + " from the class Main");
但是来自服务器的Send类不等于来自Main类的Send类
但我需要它们相等,这样我就可以在Send类中使用Send_方法来通知观察者
输出:
server.Send@6576a5d6 from server
Server startes
New Client
Is: server.Send
Sipili
server.Send@156ecdb0 from client
我需要输出的第一行(Object 1)
需要与输出的最后一行相同
注意:我认为这与序列化版本有关。
英文:
I brought another question
I have a class Server:
package server;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;
import javax.swing.DefaultListModel;
import objects.Player;
public class Servidor implements Runnable, Observer {
int puerto;
public DefaultListModel<Player> players = new DefaultListModel<>();
public ArrayList<Socket> clients = new ArrayList<>();
public static Send m;
public Servidor(int puerto, Send m) {
this.m = m;
this.m.addObserver(this);
this.puerto = puerto;
System.out.println(this.m + " from the server")
}
@Override
public void run() {
ServerSocket servidor = null;
Socket sc = null;
ObjectOutputStream output;
try {
servidor = new ServerSocket(puerto);
System.out.println("Server Started");
while (true) {
sc = servidor.accept();
System.out.println("New Cliente");
output = new ObjectOutputStream(sc.getOutputStream());
output.writeObject(m);
clientes.add(sc);
}
} catch (IOException ex) {
}
}
public void Send_Players() throws IOException {
for (Socket sc : clients) {
ObjectOutputStream output = new ObjectOutputStream(sc.getOutputStream());
output.writeObject(players);
}
}
@Override
public void update(Observable o, Object arg) {
//Stuff
}
Then I have my client class:
package server;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;
import java.util.Observable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import objects.Player;
public class Client extends Observable implements Runnable {
String host;
int puerto;
Socket sc;
public Client(int purto, String host) {
this.puerto = purto;
this.host = host;
try {
sc = new Socket(host, puerto);
} catch (IOException ex) {
}
}
@Override
public void run() {
DataInputStream in;
ObjectInputStream input = null;
DefaultListModel<Player> players = new DefaultListModel<>();
try {
in = new DataInputStream(sc.getInputStream());
input = new ObjectInputStream(in);
} catch (IOException ex) {
}
while (true) {
Object objeto = null;
try {
objeto = input.readObject();
} catch (ClassNotFoundException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
if (objeto != null) {
System.out.println("Is: " + objeto.getClass().getName());
if (objeto instanceof DefaultListModel) {
players = (DefaultListModel<Player>) objeto;
this.setChanged();
this.notifyObservers(players);
this.clearChanged();
} else {
Send m = (Send) objeto;
this.setChanged();
this.notifyObservers(m);
this.clearChanged();
}
}
}
}
}
And also the Send class:
package server;
import java.io.Serializable;
import java.util.Observable;
public class Send extends Observable implements Serializable{
private static final long serialVersionUID = 1L;
public void Send_(Object arg){
this.setChanged();
this.notifyObservers(arg);
this.clearChanged();
}
}
Now I will explain how it works:
-
When I create a server I add it to be an observer of the class Send
-
When I create a new Client the server detects it
-
When a new client has came. It sends an object of type, Send.
-
In the client class it detects with the observable that something has came and notify a class named Main.
-
When the observer of the class Main gets notified it do this:
this.m = (Send) arg; System.out.println(this.m + " from the class Main");
But class Send from the Server is not equal to the class Send from the class Main
But I need to them to be equal so I can use the Send_ method in the class send to notify the observer
Output:
server.Send@6576a5d6 from server
Server startes
New Client
Es: server.Send
Sipili
server.Send@156ecdb0 from client
and I need that the first line of output(Object 1)
needs to be the same as the last line of the output
Note: I think that is something to do with the serialized version
答案1
得分: 1
很遗憾,你无法在 https://docs.oracle.com/javase/8/docs/api/java/io/ObjectInputStream.html 中所述的方式进行操作。
- 发送对象类型。
- 创建新对象。
- 用另一个类中的值替换数值。
因此,无法进行完全复制。
英文:
Sadly you can't, in https://docs.oracle.com/javase/8/docs/api/java/io/ObjectInputStream.html it says that the way it works is by.
- Sending the object type.
- Creating the new Object.
- Replacing the values with the ones that are on your other class.
So it's impossible to do an exact copy.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论