2
Once I run a Client the Server connection terminates. The while block of the run() method of the Attendant Class does not run. I did a test and the attribute running is true. I can’t understand what is happening.
Server class.
public class Servidor implements Runnable{
private ServerSocket server;
private boolean inicializado;
private boolean executando;
private Thread thread;
private List<Atendente> atendentes;
public Servidor(int porta) throws Exception{
atendentes = new ArrayList<Atendente>();
inicializado = false;
executando = false;
open(porta);
}
private void open(int porta) throws Exception{
server = new ServerSocket(porta);
inicializado = true;
}
private void close(){
for(Atendente atendente : atendentes){
try{
atendente.stop();
}catch(Exception e){
System.out.println(e);
}
}
try{
server.close();
}catch(Exception e){
System.out.println(e);
}
server = null;
inicializado = false;
executando = false;
thread = null;
}
public void start(){
if(!inicializado || executando){
return;
}
executando = true;
thread = new Thread(this);
thread.start();
}
public void stop() throws Exception{
executando = false;
if(thread != null){
thread.join();
}
}
public void run() {
System.out.println("Aguardando conexão");
while(executando){
try{
server.setSoTimeout(2500);
Socket socket = server.accept();
System.out.println("Conexão estabelecida");
Atendente atendente = new Atendente(socket);
atendente.start();
atendentes.add(atendente);
}catch(SocketTimeoutException e){
//ignorar
}// no caso de qualquer outra execeção, a consideramos fatal, forçando o enecerramento do laço
catch(Exception e){
System.out.println(e);
break;
}
}
close();
}
public static void main(String[] args) throws Exception{
System.out.println("Inciando Servidor");
Servidor servidor = new Servidor(2525);
servidor.start();
System.out.println("Pressione ENTER para encerrar o Servidor");
new Scanner(System.in).nextLine();
System.out.println("Encerrando Servidor");
servidor.stop();
}
}
Class Attendant.
public class Atendente implements Runnable{
private Socket socket;
private BufferedReader in;
private PrintStream out;
private boolean inicizalido;
private boolean executando;
private Thread thread;
public Atendente(Socket socket) throws Exception{
this.socket = socket;
this.inicizalido = false;
this.executando = false;
open();
}
private void open() throws Exception{
try{
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintStream(socket.getOutputStream());
inicizalido = true;
}catch(Exception e){
close();
throw e;
}
}
private void close(){
//tomando cuidado de testar se o atruibuto foi ou não corretamente inicializado no metodo open().
if(in != null){
try{
in.close();
}catch(Exception e){
System.out.println(e);
}
}
if(out != null){
try{
out.close();
}catch(Exception e){
System.out.println(e);
}
}
try{
socket.close();
}catch(Exception e){
System.out.println(e);
}
in = null;
out = null;
socket = null;
inicizalido = false;
executando = false;
thread = null;
}
public void start(){
if(!inicizalido || executando){
return;
}
executando = true;
thread = new Thread(this);
thread.start();
}
public void stop() throws Exception{
executando = false;
if(thread != null){
thread.join();
}
}
@Override
public void run() {
while(executando){
try{
socket.setSoTimeout(2500);
String menssagem = in.readLine();
//Incluiremos a porta de conexaão do cliente para diferenciá-los durante a execução
System.out.println("\nMenssagem Recebida do Cliente [ "
+ socket.getInetAddress().getHostName() +
": " +
socket.getPort() +
"]: " +
menssagem);
if(menssagem.equals("FIM")) break;
out.println(menssagem);
}catch(SocketTimeoutException so){
//ignorar
}catch(Exception e){ // se alguma outra exceção ocorrer, forçamos o encerramento do atendimento
System.out.println(e);
break;
}
System.out.println("Encerrando Conexão");
close();
}
}
}
Client class.
public class Cliente {
public static void main(String[] args) throws Exception{
System.out.println("Inciando Cliente");
System.out.println("Iniciando conexão com o servidor");
Socket socket = new Socket("localhost",2525);
System.out.println("Conexão establecida");
InputStream input = socket.getInputStream();
OutputStream output = socket.getOutputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(input));
PrintStream out = new PrintStream(output);
Scanner scanner = new Scanner(System.in);
while(true){
System.out.print("Digite uma menssagem : ");
String menssagem = scanner.nextLine();
out.println(menssagem);
if("FIM".equals(menssagem)){
break;
}
menssagem = in.readLine();
System.out.println(
"Menssagem recebida do servidor: "
+ menssagem);
}
System.out.println("Encerrando conexão");
in.close();
out.close();
socket.close();
}
}
I haven’t implemented the client threads yet. But that doesn’t matter to run the program. As a result it would have to present different ports of connection of the Customer.
thank you very much. Problem solved. I didn’t even pay attention to my mistake.
– Marcelo Viana