转移完全相同的对象插座

huangapple go评论69阅读模式
英文:

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&lt;Player&gt; players = new DefaultListModel&lt;&gt;();
public ArrayList&lt;Socket&gt; clients = new ArrayList&lt;&gt;();
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 + &quot; from the server&quot;)
}
@Override
public void run() {
ServerSocket servidor = null;
Socket sc = null;
ObjectOutputStream output;
try {
servidor = new ServerSocket(puerto);
System.out.println(&quot;Server Started&quot;);
while (true) {
sc = servidor.accept();
System.out.println(&quot;New Cliente&quot;);
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&lt;Player&gt; players = new DefaultListModel&lt;&gt;();
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(&quot;Is: &quot; + objeto.getClass().getName());
if (objeto instanceof DefaultListModel) {
players = (DefaultListModel&lt;Player&gt;) 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:

  1. When I create a server I add it to be an observer of the class Send

  2. When I create a new Client the server detects it

  3. When a new client has came. It sends an object of type, Send.

  4. In the client class it detects with the observable that something has came and notify a class named Main.

  5. When the observer of the class Main gets notified it do this:

    this.m = (Send) arg;
    System.out.println(this.m + &quot; from the class Main&quot;);
    

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 中所述的方式进行操作。

  1. 发送对象类型。
  2. 创建新对象。
  3. 用另一个类中的值替换数值。

因此,无法进行完全复制。

英文:

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.

  1. Sending the object type.
  2. Creating the new Object.
  3. Replacing the values with the ones that are on your other class.

So it's impossible to do an exact copy.

huangapple
  • 本文由 发表于 2020年9月29日 10:35:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/64112053.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定