How do I access a private attribute of a class in another class?

1

I created a chat in Java. In the Servidor class, the user passes as input the port on which the server will roam. In order for the chat to work correctly, I need to access this variable of class Cliente , passing it as a parameter so the client can connect to the server.

But I'm not able to access it, I try to create a new server, but it does not work. It was the only way I thought of accessing, but I think it would be wrong, since the server is already created inside the class itself.

 public class Servidor {
 private Scanner scan = new Scanner(System.in);
 private ServerSocket servidor;
 private int numero_porta;

 public static void main(String args[]) throws IOException {
    new Servidor().iniciarServidor();
 }
 public int pegarPorta(){
    System.out.print("Digite a porta: ");
    numero_porta = scan.nextInt();
    return numero_porta;
 }
 public int getNumero_porta() {
    return this.numero_porta;
 }
 public void iniciarServidor() throws IOException {
    servidor = new ServerSocket(numero_porta);
    System.out.println("Servidor rodando na porta " + pegarPorta());
 }

public class Cliente {
private int porta;
public static void main(String args[]) {
    Scanner scan = new Scanner(System.in);
    Socket socket;
    String nome;
    Servidor server = new Servidor();
    String endereco;
        System.out.print("Digite um endereco IP: ");
        endereco = scan.nextLine();
        socket = new Socket(endereco, server.getNumero_porta());
}
    
asked by anonymous 21.11.2015 / 05:26

3 answers

0

If the user types the port on the server, it can not pass this directly to the client, because it will run in another instance of the program, that is, another process with another JVM that has a completely separate memory area. other.

In any communication program (Whats App, Facebook Messenger, etc.), the server must have a fixed address so that client applications can connect to it.

My suggestion would be to fix the server port. This works unless you have multiple servers, which is very unlikely.

If you need something more advanced, there are techniques for a server to register somewhere, and clients can check the registry to see if one or more servers are available. This can be done, for example, by creating a third program called Registro in which a server program can automatically be added to a list and client programs can query that list to connect to one of the registered servers.

In a local example, it would be possible for the client and server to run within the same program and thus share a common variable. To do this, just put the initialization of the two classes within the same main method. So the correct way to pass a variable would look something like this:

public static void main(String args[]) throws IOException {
    System.out.print("Digite a porta: ");
    Scanner scan = new Scanner(System.in);
    int numeroPorta = scan.nextInt();
    new Servidor(numeroPorta).iniciarServidor();
    Socket clientSocket = new Socket("127.0.0.1", numeroPorta);
}

Note that I passed the port to the server and to the client socket.

The rule is to properly divide responsibilities, that is, a server or client class should not be responsible for retrieving the port or other settings. This should be delegated to another class or method and the necessary values passed at the time of object construction or at server startup.

    
02.05.2016 / 07:15
0

I do not know if I understood very well, but would putting a public method of getPorta in your Server class would not solve your problem?

Ex:

public class Servidor {

   private int porta;

   public int getPorta() {
      return this.porta;
   }

}
    
21.11.2015 / 05:33
0

Your question was not very clear without the code, but I believe that putting a final static variable in your Server class would solve your problem. Note: I'm counting on the idea that Server and Client are in the same program / machine.

Ex on server:

public class Servidor {

   public static final int PORTA = 12345;       

}

So when accessing this variable, you do not need to instantiate the server object. And on the client:

socket = new Socket(endereco, Servidor.PORTA);
    
21.11.2015 / 08:11