I have a jFrame (server) that by pressing its button does the following and sends the thread1 ...
try {
ss = new ServerSocket(9999);
Hilo1 hilo = new Hilo1(ss,jLabel1,jLabel2,i);
hilo.setDaemon(true);
hilo.start();
// System.out.println(i);
//s = ss.accept();
//JOptionPane.showMessageDialog(null, "Cliente conectado correctamente ");
} catch (IOException ex) {
Logger.getLogger(estacion.class.getName()).log(Level.SEVERE, null, ex);
}
}
Afterwards, in thread 1 I have a while that accepts client connections in this way and sends it to Hilo2, what I want with this is that the variable "i" increases through client connections ...
public void run(){
while(i<3 && i>=0){
try {
sock = ss.accept();
i++;
JOptionPane.showMessageDialog(null, "Cliente"+i+ "conectado correctamente ");
System.out.println("cliente"+i+"conectado");
if(i==1){
j1.setText("ocupado");
}
if(i==2){
j2.setText("ocupado");
}
Hilo2 hilo2 = new Hilo2(i,sock);
hilo2.setDaemon(true);
hilo2.start();
//prueba.entero(i);
} catch (IOException ex) {
Logger.getLogger(Hilo1.class.getName()).log(Level.SEVERE, null, ex);
}
}
//
JOptionPane.showMessageDialog(null, "No hay espacio");
}
In Hilo2 I want the variable "i" to decrease when the jFrame2 button is pressed, which simulates that the client left and there is a place available for another client to connect ...
Button exit from JFrame2 (client):
try {
salida = new DataOutputStream(s.getOutputStream());
salida.writeUTF("salir");
} catch (IOException ex) {
Logger.getLogger(Carro.class.getName()).log(Level.SEVERE, null, ex);
}
Hilo2:
public void run(){
try {
entrada = new DataInputStream(s.getInputStream());
msg = entrada.readUTF();
if(msg.equals("salir")){
i--;
JOptionPane.showMessageDialog(null, "un carro se fue "+i);
prub = new Estacionamiento();
prub.entero(i);
}
} catch (IOException ex) {
Logger.getLogger(Hilo2.class.getName()).log(Level.SEVERE, null, ex);
}
}
The problem is that it does not decrease the i
every I give out, everything else works well. The objective of this is to simulate a parking lot with a sockets that when a car (client) arrives at the parking lot (server) the available places are filled, in this case 2, and when they leave there will be places again available and the parking lot accepts cars again.