【问题标题】:Send Object Over Java Socket通过 Java 套接字发送对象
【发布时间】:2021-01-11 07:56:28
【问题描述】:

我有一个班级名称播放器

public JLabel imagen;

public String Nombre;

public Player(int x, int y, int width, int height, Icon icono, String name){
    imagen = Player(x, y, width, height, icono);
    Nombre = name;
}
public JLabel Player(int x, int y, int width, int height, Icon icono){
    JLabel imagen = new JLabel(icono);
    imagen.setLocation(x, y);
    imagen.setSize(width, height);
    return imagen;
}

(用于创建新播放器)

我还有一个客户端类:

public class Cliente implements Runnable {

    String host;
    int puerto;
    Player mensaje;

public Cliente(int purto, Player mensaje, String host){
    this.puerto = purto;
    this.mensaje = mensaje;
    this.host = host;
}
@Override
public void run() {
    DataOutputStream out;

    try {
        Socket sc = new Socket(host, puerto);
        out = new DataOutputStream(sc.getOutputStream());
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(out);
        
        objectOutputStream.writeObject(mensaje);
        
        sc.close();

    } catch (IOException ex) {
        System.out.println(ex);
    }
}
}

我正在使用 objectOutputStream 但它说它

“java.io.NotSerializableException:objects.Player”

我想将我的播放器发送到服务器,但它显示异常!

如果你需要这里还有服务器类

public class Servidor extends Observable implements Runnable {

    int puerto;

public Servidor(int puerto) {
    this.puerto = puerto;
}

@Override
public void run() {
    ServerSocket servidor = null;
    Socket sc = null;
    DataInputStream in;

    try {
        servidor = new ServerSocket(puerto);
        System.out.println("server started");

        while (true) {
            sc = servidor.accept();

            in = new DataInputStream(sc.getInputStream());
            ObjectInputStream input = new ObjectInputStream(in);

            Player players = null;
            try {
                players = (Player) input.readObject();
                System.out.println(players.Nombre);
            } catch (ClassNotFoundException ex) {
            }

            this.setChanged();
            this.notifyObservers(players);
            this.clearChanged();

            sc.close();
        }
    } catch (IOException ex) {
    }
}
}

如果你想要这里是发送请求到客户端类的代码行

    Cliente c = new Cliente(5000, new Player(x, y, width, height, icon, "name of the player"), "the ip");
    Thread t = new Thread(c);

    t.start();

【问题讨论】:

    标签: java sockets exception


    【解决方案1】:

    看起来您忘记使 Player 对象可序列化,因此代码正在抛出 java.io.NotSerializableException

    如果您需要通过网络发送一些对象,那么该对象需要是可序列化的。

    序列化是获取一个内存数据结构的过程 对象并将其编码为一个串行(因此称为)序列 字节。然后可以将此编码版本保存到磁盘,通过 网络连接,或以其他方式传达给接收者。 (从 维基百科)

    我已经更新了代码

    Player.java

    import java.io.Serializable;
    
    import javax.swing.Icon;
    import javax.swing.JLabel;
    
    public class Player implements Serializable {
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
    
        public JLabel imagen;
    
        public String Nombre;
    
        public Player(int x, int y, int width, int height, Icon icono, String name) {
            imagen = Player(x, y, width, height, icono);
            Nombre = name;
        }
    
        public JLabel Player(int x, int y, int width, int height, Icon icono) {
            JLabel imagen = new JLabel(icono);
            imagen.setLocation(x, y);
            imagen.setSize(width, height);
            return imagen;
        }
    }
    

    Cliente.java

    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.io.ObjectOutputStream;
    import java.net.Socket;
    
    public class Cliente implements Runnable {
    
        String host;
        int puerto;
        Player mensaje;
    
        public Cliente(int purto, Player mensaje, String host) {
            this.puerto = purto;
            this.mensaje = mensaje;
            this.host = host;
        }
    
    //  @Override
        public void run() {
            DataOutputStream out;
    
            try {
                Socket sc = new Socket(host, puerto);
                out = new DataOutputStream(sc.getOutputStream());
                ObjectOutputStream objectOutputStream = new ObjectOutputStream(out);
    
                objectOutputStream.writeObject(mensaje);
    
                sc.close();
    
            } catch (IOException ex) {
                System.out.println(ex);
            }
        }
    
        public static void main(String[] args) {
            Cliente c = new Cliente(5000, new Player(1, 2, 3, 4, null,
                    "Holis Studios"), "localhost");
            Thread t = new Thread(c);
    
            t.start();
        }
    }
    

    Servidor.java

    import java.io.DataInputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.Observable;
    
    public class Servidor extends Observable implements Runnable {
    
        int puerto;
    
        public Servidor(int puerto) {
            this.puerto = puerto;
        }
    
    //  @Override
        public void run() {
            ServerSocket servidor = null;
            Socket sc = null;
            DataInputStream in;
    
            try {
                servidor = new ServerSocket(puerto);
                System.out.println("server started");
    
                while (true) {
                    sc = servidor.accept();
    
                    in = new DataInputStream(sc.getInputStream());
                    ObjectInputStream input = new ObjectInputStream(in);
    
                    Player players = null;
                    try {
                        players = (Player) input.readObject();
                        System.out.println(players.Nombre);
                    } catch (ClassNotFoundException ex) {
                    }
    
                    this.setChanged();
                    this.notifyObservers(players);
                    this.clearChanged();
    
                    sc.close();
                }
            } catch (IOException ex) {
            }
        }
        
        public static void main(String[] args) {
            Servidor server = new Servidor(5000);
            Thread t = new Thread(server);
    
            t.start();
        }
    }
    

    编译代码:

    javac.exe -cp . Player.java
    
    javac.exe -cp . Servidor.java
    
    javac.exe -cp . Cliente.java
    

    运行:

    java.exe -cp . Servidor
    server started
    
    java.exe -cp . Cliente
    

    出现在服务端控制台上的输出:

    server started
    Holis Studios
    

    【讨论】:

      猜你喜欢
      • 2014-01-11
      • 2012-08-29
      • 2012-03-13
      • 2015-08-31
      • 2014-03-24
      • 2017-03-20
      • 1970-01-01
      • 2019-04-21
      相关资源
      最近更新 更多