Object problems using java sockets

Asked

Viewed 94 times

2

I’m trying to pass the person object that was instantiated on the server to the client. But I’m not getting it. Below are the classes I used:

Server

public class ServidorTCPBasico {
public static void main(String[] args) {
    try {

        ServerSocket servidor = new ServerSocket(12345);
        System.out.println("Servidor ouvindo a porta 12345");
        while (true) {

            Pessoa pessoa = new Pessoa("José", "Zé");

            Socket cliente = servidor.accept();
            System.out.println("Cliente conectado: " + cliente.getInetAddress().getHostAddress());
            ObjectOutputStream saida = new ObjectOutputStream(cliente.getOutputStream());
            saida.flush();
            saida.writeObject(pessoa);
            saida.close();
            cliente.close();
        }
    } catch (Exception e) {
        System.out.println("Erro: " + e.getMessage());
    } finally {

    }
}}

Client

public class ClienteTCPBasico {
  public static void main(String[] args) {
    try {
      Socket cliente = new Socket("127.0.0.1",12345);
      ObjectInputStream entrada = new ObjectInputStream(cliente.getInputStream());
      Pessoa pessoa = (Pessoa)entrada.readObject();
      JOptionPane.showMessageDialog(null,"Dados da pessoa:" + pessoa.toString());
      entrada.close();
      System.out.println("Conexão encerrada");
    }
    catch(Exception e) {
      System.out.println("Erro: " + e.getMessage());
    }
  }
}  

Classe Pessoa

public class Pessoa {

    String nome;
    String apelido;

    public Pessoa(String nome, String apelido) {
        this.nome = nome;
        this.apelido = apelido;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getApelido() {
        return apelido;
    }

    public void setApelido(String apelido) {
        this.apelido = apelido;
    }

    @Override
    public String toString() {
        return "Pessoa{" + "nome=" + nome + ", apelido=" + apelido + '}';
    }
}

When I spin the class ServidorTCPBasico everything OK, but when I turn the class ClienteTCPBasico of the following error:

Error: Connection reset

I used as an example to develop this code the article from Devmedia
I used the classes of the same name only replaces the Date class with the Person class. I intended to pass an object created by me using socket , but unfortunately it did not work! Where there is an error in this application?

I think the mistake is in using the class ObjectOutputStream to read some primitive type, but I’m not sure.

  • I’m testing here (I’m downloading the JDK), but looking over seems a problem in the way it’s sending the writing to the client. The download takes a while here, but as soon as I can I post a reply ;)

  • @Guilhermenascimento Wow man, thank you so much for the strength, I’m trying ! I’m waiting!

  • @Guilhermenascimento The friend Leonardo realized my mistake, had forgotten to implement the seriaalizable class. Thanks man!! Thank you very much for the consideration!!

  • Cool, I saw, even helped me :)

1 answer

2


try to implement the interface Serializeble in your class Pessoa, when you implement this interface your class becomes "serializable".

  • Thanks, just as I forgot one of these things,I’ll try it here!! Thanks

  • All right, if it helped you, don’t forget to mark the answer as right, and good studies :)

  • It worked, I got it. Thank you very much man !!!!!

  • Great, congratulations +1

  • Thanks guys, I’m really happy to be contributing to stackoverflow.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.