Skip to content
Advertisement

Send Object Over Java Socket

I have a class name player

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;
}

(It is for creating a new player)

I also have a client class:

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);
    }
}
}

And im using objectOutputStream but it says that it

“java.io.NotSerializableException: objects.Player”

And I want to send my player to the server but it says that exception!

Also if you need here is the server class

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) {
    }
}
}

and also if you want here are the lines of code that send the request to the client class

    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();

Advertisement

Answer

Looks like you forgot to make the Player object Serializable, thus code is throwing java.io.NotSerializableException

If you need to send some object over network then the object need to be Serializable.

Serialization is the process of taking the memory data structure of an object and encoding it into a serial (hence the term) sequence of bytes. This encoded version can then be saved to disk, sent across a network connection, or otherwise communicated to a recipient. (from Wikipedia.org)

I have updated the code

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();
    }
}

Compiling the code:

javac.exe -cp . Player.java

javac.exe -cp . Servidor.java

javac.exe -cp . Cliente.java

Run:

java.exe -cp . Servidor
server started

java.exe -cp . Cliente

Output appearing on Servidor console:

server started
Holis Studios
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement