1
I created a chat in Java. In the class Servidor
, the user passes as input the port on which the server will run. In order for the chat to work correctly, I need to access this class variable Cliente
, passing it as a parameter so that the client can connect to the server.
But I’m not being able to access it, I try to create a new server, but it doesn’t work. It was the only way I thought to access, but I think it would be wrong, because the server is already created within 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());
}
Add the relevant code to your question. I don’t know anything about Java, so I can’t help you, but I believe that if you add the relevant code and detail what you intend to do better, concrete answers will appear to help you solve your problem. Welcome :)
– Chun